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

Recommended Answers

All 6 Replies

what exactly is the problem. is there any errors or is the table just not showing up or displaying improperly.

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>";
}

you are not returning values from the text file. right now you have it set up to only show the information that someone submits.

Ok, but im a newbie to php, could you show me ? :D

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.