User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,970 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,773 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1472 | Replies: 6
Reply
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Help PHP blog from flat file

  #1  
Nov 25th, 2007
Hi im having some trouble with the code bellow. I would like for each time the form containing the name and message to be echoed out but in a table. So naturally i have made a for loop that checks the amount in the array and echo out seperate portions of the data in a table.

Please .. any help would be so apreciative



<?
//host form values in variables
$name = $HTTP_POST_VARS["name"];
$message = $HTTP_POST_VARS["message"];

$write = $name."|".$message."|";


//open the blog.txt
$fp = fopen("blogfile.txt","a+") or die("Counld not open file");

//writing to the file and closing
fwrite($fp, $write);
fclose($fp);

//split the variable $write at | making an array
$finished = explode('|',$write);

//count the entries in the blog
$amount = count($finished);
?>
<table bgcolor="#47607C" border="2" cellpadding="5" bordercolor="#003333" style="color:#003366;" width="500px">

<tr>
<td width=40><font color="#000000">Name</font></td>
<td width=200><font color="#000000">Message</font></td>
</tr>

<?php

for($i=0; $i<$amount ; $i = $i+2)
{

if(empty($finished[$i]) || $finished[$i] == null)
continue;

echo "<tr>";

echo "<td>".$finished[$i]."</td><td>".$finished[$i+1]."</td>";

echo "</tr>";
}

?>



</table>


The code in Bold is where i think the problem is.
Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: PHP blog from flat file

  #2  
Nov 25th, 2007
what exactly is the problem. is there any errors or is the table just not showing up or displaying improperly.
Reply With Quote  
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Re: PHP blog from flat file

  #3  
Nov 25th, 2007
yes thats it, it does not repeat the table for the amount of data entries. Which makes me think that the following is the problem . But i cant see what.


for($i=0; $i<$amount ; $i = $i+2)
{

if(empty($finished[$i]) || $finished[$i] == null)
continue;

echo "<tr>";

echo "<td>".$finished[$i]."</td><td>".$finished[$i+1]."</td>";

echo "</tr>";
}
Reply With Quote  
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Re: PHP blog from flat file

  #4  
Nov 25th, 2007
i have just uploaded it to my server

http://www.reecesplace.co.uk/Reecesplace/blog/blog.php

if u fill it in once on the next page it echoes into the table but if you try it again it just echoes the new data and doesnt repeat what i wanted in the for loop. Please help
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: PHP blog from flat file

  #5  
Nov 25th, 2007
you are not returning values from the text file. right now you have it set up to only show the information that someone submits.
Reply With Quote  
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Re: PHP blog from flat file

  #6  
Nov 25th, 2007
Ok, but im a newbie to php, could you show me ?
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: PHP blog from flat file

  #7  
Nov 25th, 2007
here, i wrote this:

<?php

$name = $_REQUEST['name'];
$message = $_REQUEST['message'];

$file = 'blogfile.txt';

$error = 0;

if ($name == '') {
$error++;
}
if ($message == '') {
$error++;
}
if ($error > 0) {
echo 'No fields can be left blank';
}
if ($error == 0) {

$open = fopen($file, 'a');
$data = "--" . $name . "|" . $message . "\n";
$write = fwrite($open, $data);
fclose($open);

}

$open = fopen($file, 'r');
$read = fread($open, filesize($file));
fclose($open);

$entries = explode('--', $read);

$delete = array_shift($entries);

echo '<table bgcolor="#47607C" border="2" cellpadding="5" bordercolor="#003333" style="color:#003366;" width="500px">';
echo '<tr>';
echo '<td width=40><font color="#000000">Name</font></td>';
echo '<td width=200><font color="#000000">Message</font></td>';
echo '</tr>';

$i = 0;
while ($i < count($entries)) {

$info = explode('|', $entries[$i]);
echo '<tr>';
echo '<td>' . $info[0] . '</td><td>' . $info[1] . '</td>';
echo '</tr>';

$i++;
}

echo '</table>';

?>

it works perfectly for me. make sure you delete everything in your blogfile.txt file first though. it won't work other wise. if you have any questions, just let me know and i will answer them
Last edited by kkeith29 : Nov 25th, 2007 at 4:49 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC