4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 PHP: Writing To A Text File
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 5:31 pm (21 years, 2 months ago) Reply with QuoteBack to Top

Hi,

I have some code below, that when I include in one of my pages, it messes the rest of the PHP up, and gives some odd results. Which breaks my news system.

What I wanted to know, is, is there a way that I can get this code to execute, and write the output to a text file, and then include it that way, so it eliminates this problem?

Thanks

Code:

<center>
<img src="news.png" border="0"></A>
</center><P>


<?
$tempdir="admin/";
require("admin/connect.php");
$date=date("Y-m-d");
$time=date("H:i:s");
$pathimages=substr($pathimages,3,999);
$articlenumber=10; // Set to number of articles you need to display
@$outcome=mysql_query("SELECT * FROM articles WHERE adate<='$date' ORDER BY adate DESC, atime DESC LIMIT $articlenumber");
if ($sectionID) @$outcome=mysql_query("SELECT * FROM articles WHERE sectionID='$sectionID' AND adate<='$date' ORDER BY adate DESC, atime DESC LIMIT $articlenumber");
require("admin/failure.php");
while ($access=mysql_fetch_array($outcome))
   {
   $articleID=$access["ID"];
   $title=$access["title"];
   $beginning=$access["beginning"];
   $authorID=$access["authorID"];
   $year=strval(substr($access["adate"],0,4));
   $month=strval(substr($access["adate"],5,2));
   $day=strval(substr($access["adate"],8,2));
   $adate="$day.$month.$year";
   $atime=strval(substr($access["atime"],0,5));
   $imagesetID=$access["imagesetID"];
   $outcome2=$outcome;
@$outcome=mysql_query("SELECT fullname FROM users WHERE ID='$authorID'");
require("admin/failure.php");
$access=mysql_fetch_array($outcome);
$author=$access["fullname"];
unset($file1); unset($thumb1);
if ($imagesetID)
{
   @$outcome=mysql_query("SELECT * FROM images WHERE ID='$imagesetID'");
   require("admin/failure.php");
   $access=mysql_fetch_array($outcome);
   $file1=$access["file1"];
   $thumb1=substr($file1,0,-4)."a".substr($file1,-4,4);
}
@$outcome=mysql_query("SELECT views FROM stats WHERE articleID='$articleID'");
require("admin/failure.php");
$access=mysql_fetch_array($outcome);
$views=$access["views"];
$outcome=$outcome2;
echo '<B><a href="http://www.bluesweb.co.uk/news/showarticle.php?articleID=',$articleID,'">';
echo '<p style="line-height: 150%" align="justify"><span style="font-weight: 700"><font face="Verdana" color="white"> <span style="font-size: 8pt">';

echo $title;
echo '</B></a>';
echo '</font><br>';

}
?>
<P>
<center>
<a href="http://www.newsnow.co.uk"><img src="newsnowfeatured.gif" border="0"></A>


In other words, I want to execute the code, and then include it, rather than including it then executing it.
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Thu Feb 06, 2003 5:43 pm (21 years, 2 months ago) Reply with QuoteBack to Top

I'm really not sure what you want, but this code will enable you to include a file and then output it later (usually it outputs straight away):

Code:
        ob_start();
        include($DOCUMENT_ROOT.'/file.php');
        $variable = ob_get_contents();
        ob_end_clean();


Then use
Code:
echo $variable;
to output.

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 5:51 pm (21 years, 2 months ago) Reply with QuoteBack to Top

Thats not good enough, the PHP code in the include is stopping the PHP Code in the main page working properly, I need the output from the included file written to another file in HTML form, then include it.
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
adam
Forum Moderator & Developer



Joined: 26 Jul 2002
Posts: 704
Location: UK

PostPosted: Thu Feb 06, 2003 6:45 pm (21 years, 2 months ago) Reply with QuoteBack to Top

how about you put your code into a function, then pass the output of the function to fwrite()?

________________________________
It's turtles all the way down...
OfflineView User's ProfileFind all posts by adamSend Personal MessageVisit Poster's Website
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:03 pm (21 years, 2 months ago) Reply with QuoteBack to Top

adam wrote:
how about you put your code into a function, then pass the output of the function to fwrite()?

Tried that, doesn't work still, only solution is to write to a text file and then include, there MUST be a way of doing this.
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:11 pm (21 years, 2 months ago) Reply with QuoteBack to Top

This is the code that it is included with

Code:
<? require ("../includes/meta.html"); ?>

<title><?
$tempdir="admin/";
require("admin/connect.php");
@$outcome=mysql_query("SELECT * FROM articles WHERE ID='$articleID'");
require("admin/failure.php");
$access=mysql_fetch_array($outcome);
$title=$access["title"];
echo 'BluesWeb.co.uk -> Latest News -> ',$title,'';
?></title>

<? require ("../includes/newsheader.html"); ?>
<? require ("showtitles.php"); ?>


   
   
   
    <td width="671" align="left" valign="top" height="14">     
    <p align="center">
        <font face="Verdana"><span style="font-size: 8pt">
       


         
                   
         
         
          </span></font>
        <p align="justify"><font face="Verdana"><span style="font-size: 8pt">

<html>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<?
$tempdir="admin/";
require("admin/connect.php");
@$outcome=mysql_query("SELECT * FROM articles WHERE ID='$articleID'");
require("admin/failure.php");
$access=mysql_fetch_array($outcome);
$title=$access["title"];
$beginning=$access["beginning"];
$text=$access["text"];
$beginning=nl2br($beginning);
$text=nl2br($text);
$authorID=$access["authorID"];
$imagesetID=$access["imagesetID"];
$filesetID=$access["filesetID"];
$year=strval(substr($access["adate"],0,4));
$month=strval(substr($access["adate"],5,2));
$day=strval(substr($access["adate"],8,2));
$adate=$day.'.'.$month.'.'.$year;
$atime=strval(substr($access["atime"],0,5));
if (!$access["ID"]) { echo $textwarning[16]; exit; }
@$outcome=mysql_query("SELECT fullname FROM users WHERE ID='$authorID'");
require("admin/failure.php");
$access=mysql_fetch_array($outcome);
$author=$access["fullname"];
@$outcome=mysql_query("SELECT views FROM stats WHERE articleID='$articleID'");
require("admin/failure.php");
$access=mysql_fetch_array($outcome);
$views=$access["views"];
echo '<p><font face="Verdana, Helvetica, sans-serif" size="4">',$title,'</font><br>';
echo '<font face="Verdana, Helvetica, sans-serif" size="1">',$adate,' ',$atime,' | ',$author,'</b></font></p>';
echo '<p align="justify"><B><font face="Verdana"><span style="font-size: 8pt">',$beginning,'</font></B></p>';
echo '<p align="justify"><font face="Verdana"><span style="font-size: 8pt">',$text,'</font></p>';
@$outcome=mysql_query("UPDATE stats SET views=views+1 WHERE articleID='$articleID'");
require("admin/failure.php");
@$outcome=mysql_query("SELECT * FROM images WHERE ID='$imagesetID'");
while ($access=mysql_fetch_array($outcome))
{
$file1=$access["file1"]; $file2=$access["file2"];
$file3=$access["file3"]; $file4=$access["file4"];
$file5=$access["file5"]; $file6=$access["file6"];
$file7=$access["file7"]; $file8=$access["file8"];
$file9=$access["file9"]; $file10=$access["file10"];
$thumb1=substr($file1,0,-4)."a".substr($file1,-4,4);
$thumb2=substr($file2,0,-4)."a".substr($file2,-4,4);
$thumb3=substr($file3,0,-4)."a".substr($file3,-4,4);
$thumb4=substr($file4,0,-4)."a".substr($file4,-4,4);
$thumb5=substr($file5,0,-4)."a".substr($file5,-4,4);
$thumb6=substr($file6,0,-4)."a".substr($file6,-4,4);
$thumb7=substr($file7,0,-4)."a".substr($file7,-4,4);
$thumb8=substr($file8,0,-4)."a".substr($file8,-4,4);
$thumb9=substr($file9,0,-4)."a".substr($file9,-4,4);
$thumb10=substr($file10,0,-4)."a".substr($file10,-4,4);
$pathimages=substr($pathimages,3,999);
if ($file1) echo '<p><a href="',$pathimages,$file1,'"><img src="',$pathimages,$thumb1,'" border="0"></a>';
if ($file2) echo '<a href="',$pathimages,$file2,'"><img src="',$pathimages,$thumb2,'" border="0"></a>';
if ($file3) echo '<a href="',$pathimages,$file3,'"><img src="',$pathimages,$thumb3,'" border="0"></a></p>';
if ($file4) echo '<p><a href="',$pathimages,$file4,'"><img src="',$pathimages,$thumb4,'" border="0"></a>';
if ($file5) echo '<a href="',$pathimages,$file5,'"><img src="',$pathimages,$thumb5,'" border="0"></a> ';
if ($file6) echo '<a href="',$pathimages,$file6,'"><img src="',$pathimages,$thumb6,'" border="0"></a></p>';
if ($file7) echo '<p><a href="',$pathimages,$file7,'"><img src="',$pathimages,$thumb7,'" border="0"></a> ';
if ($file8) echo '<a href="',$pathimages,$file8,'"><img src="',$pathimages,$thumb8,'" border="0"></a>';
if ($file9) echo '<a href="',$pathimages,$file9,'"><img src="',$pathimages,$thumb9,'" border="0"></a></p>';
if ($file10) echo '<p><a href="',$pathimages,$file10,'"><img src="',$pathimages,$thumb10,'" border="0"></a></p>';
}
@$outcome=mysql_query("SELECT * FROM files WHERE ID='$filesetID'");
while ($access=mysql_fetch_array($outcome))
{
$file1=$access["file1"]; $file2=$access["file2"];
$file3=$access["file3"]; $description=$access["description"];
$pathfiles=substr($pathfiles,3,999);
if ($file1) echo '<p>',$description,'<br>';
if ($file1) echo '<a href="',$pathfiles,$file1,'">',$file1,'</a><br>';
if ($file2) echo '<a href="',$pathfiles,$file2,'">',$file2,'</a><br>';
if ($file3) echo '<a href="',$pathfiles,$file3,'">',$file3,'</a><br></p>';
}
?>
<p align="justify"><font face="Verdana"><span style="font-size: 8pt"><b><? echo $textbasic[20] ?></b><br>
<a href="addcomment.php?articleID=<? echo $articleID ?>"><? echo $textbasic[21] ?></a></font></p><hr>
<?
@$outcome=mysql_query("SELECT * FROM discussions WHERE articleID='$articleID' ORDER BY adate DESC, atime DESC");
require("admin/failure.php");
while ($access=mysql_fetch_array($outcome))
   {
$atime=$access["atime"];
$comment=$access["comment"];
$name=$access["name"];
$email=$access["email"];
echo '<p align="justify"><font face="Verdana"><span style="font-size: 8pt">';
if ($email<>"") echo '<a href="mailto:',$email,'">';
echo $name;
if ($email<>"") echo '</a>';
$year=strval(substr($access["adate"],0,4));
$month=strval(substr($access["adate"],5,2));
$day=strval(substr($access["adate"],8,2));
$adate=$day.'.'.$month.'.'.$year;
echo ' ',$textbasic[22],' ',$adate,' ',$textbasic[23],' ',$atime,':<br>';
echo (nl2br(htmlspecialchars($comment)));
echo "</font></p><hr>";
   }
?>
</td>
</tr>
</table>
</body>
</html>

<? require ("../includes/footer.html"); ?>


Output : http://www.bluesweb.co.uk/news/showarticle.php?articleID=1

showtitles.php is the file that I showed the code for above, when showtitles is included, the same article gets displayed on the right hand side, whichever article is selected
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:13 pm (21 years, 2 months ago) Reply with QuoteBack to Top

when the showtitles.php file is not included, it works fine.
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
adam
Forum Moderator & Developer



Joined: 26 Jul 2002
Posts: 704
Location: UK

PostPosted: Thu Feb 06, 2003 7:22 pm (21 years, 2 months ago) Reply with QuoteBack to Top

what if you opened showtitles.php by calling fopen('http://www.bluesweb.co.uk/news/showtitles.php','r')...wouldn't that make php open the file through a http connection? this would mean the code is executed by apache separately, then given to showarticle.php

________________________________
It's turtles all the way down...
OfflineView User's ProfileFind all posts by adamSend Personal MessageVisit Poster's Website
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:29 pm (21 years, 2 months ago) Reply with QuoteBack to Top

I replaced :

<? require ("showtitles.php"); ?>

with

<? 'http://www.bluesweb.co.uk/news/showtitles.php','r'); ?>

Now it doesn't appear at all, though it stops the problem, but I don't get the list of articles down the left hand side!


Last edited by Justin on Thu Feb 06, 2003 7:31 pm, edited 1 time in total
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
adam
Forum Moderator & Developer



Joined: 26 Jul 2002
Posts: 704
Location: UK

PostPosted: Thu Feb 06, 2003 7:30 pm (21 years, 2 months ago) Reply with QuoteBack to Top

hmm...are you sure that showtitles.php actually returns the list of articles?

________________________________
It's turtles all the way down...
OfflineView User's ProfileFind all posts by adamSend Personal MessageVisit Poster's Website
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:32 pm (21 years, 2 months ago) Reply with QuoteBack to Top

Yep :

http://www.bluesweb.co.uk/news/showtitles.php

And before you say, the text is white I know, but highlight it and you'll find it
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
adam
Forum Moderator & Developer



Joined: 26 Jul 2002
Posts: 704
Location: UK

PostPosted: Thu Feb 06, 2003 7:37 pm (21 years, 2 months ago) Reply with QuoteBack to Top

hmm...this is very odd...sad to say, I have no idea

________________________________
It's turtles all the way down...
OfflineView User's ProfileFind all posts by adamSend Personal MessageVisit Poster's Website
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:40 pm (21 years, 2 months ago) Reply with QuoteBack to Top

Yeah, same here, just doing my head in, as I was going to put the script live today, it was all working fine, and I thought, well nothing can go wrong, all I need to do is to include the headlines on the left hand side, shouldn't be too hard I thought to myself.

How Wrong I was!
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:48 pm (21 years, 2 months ago) Reply with QuoteBack to Top

Tried taking the first DB Connection String out of the showarticle.php file so there is only one in there, still the same result
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
Justin
4WebHelp Addict
4WebHelp Addict


Joined: 07 Jan 2002
Posts: 1060

PostPosted: Thu Feb 06, 2003 7:51 pm (21 years, 2 months ago) Reply with QuoteBack to Top

Strange thing is, it displays titlebar correctly for each article, but it doesn't display the rest correctly - strange
OfflineView User's ProfileFind all posts by JustinSend Personal MessageSend email
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   


Go to page 1, 2  Next

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.434604 seconds :: 19 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme