Jump to content

Page doesn't show any html after a PHP script.


membot

Recommended Posts

I have a page that shows entries in a guestbook I'm making, and below the entries there is supposed to be a form to write an entry. Except, none of the HTML after the script to show the entries shows up on the page. I have no clue what's wrong.  :confused:

 

Here is the script to show the entries. $n is name, $d is date, $s is site (optional), and $m is message.

$file = fopen("posts.txt", 'rb');
flock($file, LOCK_SH);

while(!feof($file)){

$entry = fgetcsv($file, 0, '|');

if(empty($entry)){exit;}

$d = $entry[1]; $n = $entry[2]; $s = $entry[3]; $m = $entry[4];

echo '
<table style="border: #3399AA 1px solid;"><tr style="background: #3399AA; font: bold 10px verdana,sans-serif; color: #FFFFFF;">
<td width="170">'.$n.'</td>
<td align="right" width="170">'.$d.'</td>
</tr>
';

if($s != 'none'){
echo '
<tr><td colspan="2" style="font: 10px verdana,sans-serif; color: #3399AA;">
<b>Site: </b><a href="'.$s.'">'.$s.'</a>
<center><div style="width: 250; height: 1px; background: #3399AA; margin: 10px;"></div></center></td></tr>
';
}

echo '
<tr>
<td colspan="2" style="font: 10px verdana,sans-serif; color: #3399AA;">'.$m.'</td>
</tr></table><br>
';

}

flock($file, LOCK_UN);
fclose($file);

 

The file it is reading looks like this:

|11:51 am, 3rd Nov 2010|Memoria|none|Hello. 
|11:51 am, 3rd Nov 2010|Memoria|http://sitehere|Hello again.
|11:51 am, 3rd Nov 2010|Memoria|none|How are you doing?

 

Thanks!  :)

Link to comment
Share on other sites

Try this:

 

$file = fopen("posts.txt", 'rb');
flock($file, LOCK_SH);

while(!feof($file)){

$entry = fgetcsv($file, 0, '|');

if(empty($entry)){exit;}

$d = $entry[1]; $n = $entry[2]; $s = $entry[3]; $m = $entry[4];

echo '
<table style="border: #3399AA 1px solid;"><tr style="background: #3399AA; font: bold 10px verdana,sans-serif; color: #FFFFFF;">
<td width="170">'.$n.'</td>
<td align="right" width="170">'.$d.'</td>
</tr>
';

if($s != 'none'){
echo '
<tr><td colspan="2" style="font: 10px verdana,sans-serif; color: #3399AA;">
<b>Site: </b><a href="'.$s.'">'.$s.'</a>
<center><div style="width: 250; height: 1px; background: #3399AA; margin: 10px;"></div></center></td></tr>
';
}

}

echo "
<tr>
<td colspan=\"2\" style=\"font: 10px verdana,sans-serif; color: #3399AA;\">'.$m.'</td>
</tr></table><br>";

flock($file, LOCK_UN);
fclose($file);

Link to comment
Share on other sites

I have a page that shows entries in a guestbook I'm making, and below the entries there is supposed to be a form to write an entry. Except, none of the HTML after the script to show the entries shows up on the page. I have no clue what's wrong.  :confused:

 

Here is the script to show the entries. $n is name, $d is date, $s is site (optional), and $m is message.

$file = fopen("posts.txt", 'rb');
flock($file, LOCK_SH);

while(!feof($file)){

$entry = fgetcsv($file, 0, '|');

if(empty($entry)){exit;}

$d = $entry[1]; $n = $entry[2]; $s = $entry[3]; $m = $entry[4];

echo '
<table style="border: #3399AA 1px solid;"><tr style="background: #3399AA; font: bold 10px verdana,sans-serif; color: #FFFFFF;">
<td width="170">'.$n.'</td>
<td align="right" width="170">'.$d.'</td>
</tr>
';

if($s != 'none'){
echo '
<tr><td colspan="2" style="font: 10px verdana,sans-serif; color: #3399AA;">
<b>Site: </b><a href="'.$s.'">'.$s.'</a>
<center><div style="width: 250; height: 1px; background: #3399AA; margin: 10px;"></div></center></td></tr>
';
}

echo '
<tr>
<td colspan="2" style="font: 10px verdana,sans-serif; color: #3399AA;">'.$m.'</td>
</tr></table><br>
';

}

flock($file, LOCK_UN);
fclose($file);

 

The file it is reading looks like this:

|11:51 am, 3rd Nov 2010|Memoria|none|Hello. 
|11:51 am, 3rd Nov 2010|Memoria|http://sitehere|Hello again.
|11:51 am, 3rd Nov 2010|Memoria|none|How are you doing?

 

Thanks!  :)

 

I'm more confused :)  ... the code that you posted works in the way it is (tested)... but ... you said:

and below the entries there is supposed to be a form to write an entry. Except, none of the HTML after the script to show the entries shows up on the pag

 

and I don't see any form.. so... how do you expect to get help?... show the code that is NOT working

Link to comment
Share on other sites

IMO echo should always be in double quotes

When HTML tags are paired with echo, you should use single quotes

 

Also - you don't NEED any quotes (single or double) for HTML unless your attributes use spaces.

So unless you're using a php variable that defines an attribute, I would take the quotes out altogether in the areas you know for a fact can only be changed by you (ie: the hard styles)

 

Assuming this is what you want your Guest Book Entry to look like:

2i77n94.png

 

Add a stylesheet. (This is just a simple style you could use. It would be better to refine it with divs and padding) Notice that I named each node in your table to correspond with your variable names. "m" is empty because it doesn't need a new style, but I added 4 separate classes in case you wanted to change the style in the future

<style type=text/css>
#GuestBookEntry { 
border: 1px solid #3399AA; 
width: 300px;
font: verdana,sans-serif;
color: #FFFFFF;
}

#GuestBookEntry TR {
background-color: #3399AA; 
}

.n { width: 50%; }
.d { width: 50%; text-align: right; }
.s { padding-bottom: 10px; }
.m { }
</style>

 

Your PHP/HTML should look like this. Notice that the ONLY single quotes are wrapped around the link tag <a href=''> because there is a chance that (for whatever reason) a link could have a space in the URL. Notice that the class attributes do not use single or double quotes, because there is NO WAY a class would ever need a space. If you attempted to name a css class with spaces, it wouldn't work anyway. Colspan is the same way. There is NO WAY you could EVER write colspan=2 2 <== that would be colspan=22, in which case you would slap yourself on the forehead and say "2 2 is not a number, but 22 is"

<?php
...

echo "<table id=GuestBookEntry><tr>";
echo "<td class=n>".$n."</td>";
echo "<td class=d>".$d."</td>";
echo "</tr>";

if($s != "none"){
echo "<tr>";
echo "<td colspan=2 class=s><b>Site: </b><a href='".$s."'>".$s."</a></td>";
echo "</tr>";
}

echo "<tr><td colspan=2 class=m>".$m."</td></tr></table>";

...
?>

 

I made a test page, using only the following, and it works perfectly:

<style type=text/css>
#GuestBookEntry { 
border: 1px solid #3399AA; 
width: 300px;
font: verdana,sans-serif;
color: #FFFFFF;
}

#GuestBookEntry TR {
background-color: #3399AA; 
}

.n { width: 50%; }
.d { width: 50%; text-align: right; }
.s { padding-bottom: 10px; }
.m { }
</style>

<?php

$n = "n";
$d = "d";
$s = "s";
$m = "m";

echo "<table id=GuestBookEntry><tr>";
echo "<td class=n>".$n."</td>";
echo "<td class=d>".$d."</td>";
echo "</tr>";

if($s != "none"){
echo "<tr>";
echo "<td colspan=2 class=s><b>Site: </b><a href='".$s."'>".$s."</a></td>";
echo "</tr>";
}

echo "<tr><td colspan=2 class=m>".$m."</td></tr></table>";

?>

 

Also, every echo could be written on one line, but I wrote it out in multiple lines so you could see the difference

Link to comment
Share on other sites

IMO echo should always be in double quotes

 

Why do you drop out of double quotes in order to splice in a variable to the echo statement.  Double quotes tell the parser to parse that line, so any variables it finds will be populated.  Single quotes are faster in processing as the parser doesn't even look at that line, just spits it out to the browser.

Your example

<?php
...

echo "<table id=GuestBookEntry><tr>"
      . "<td class=n>$n</td>"
      . "<td class=d>$d</td>"
      . "</tr>";

if($s != "none"){
   echo "<tr>"
          . "<td colspan=2 class=s><b>Site: </b><a href=\"$s\">$s</a></td>"
          . "</tr>";
}

echo "<tr><td colspan=2 class=m>$m</td></tr></table>";

...
?>

 

Just sayin~.

Link to comment
Share on other sites

Ok well... here's my whole source for the page. I don't think the quotes are the problem, but I don't know.....

<title>Mem's Guestbook</title>

<style>
body {background: #33DDDD;}
#content {background: #FFFFFF; width: 500px; font: 10px verdana,sans-serif; color: #3399AA; padding: 10px;}
h1 {font: normal small-caps normal 30px georgia,serif; color: #33DDDD; text-shadow: #3399AA 1px 1px 3px}
#cell {font: 10px verdana,sans-serif; color: #3399AA;}

#input {font: 10px verdana,sans-serif; color: #3399AA; width: 150px; border: #3399AA 1px solid;}
#submit {font: 10px verdana,sans-serif; color: #3399AA; border: #3399AA 1px solid; background: #89EBEB;}
</style>

<center>

<div id="content">
<h1>Mem's Guestbook</h1>

<!-- SCRIPT TO SHOW POSTS -->
<?php

$file = fopen("posts.txt", 'rb');
flock($file, LOCK_SH);

while(!feof($file)){

$entry = fgetcsv($file, 0, '|');

if(empty($entry)){exit;}

$d = $entry[1]; $n = $entry[2]; $s = $entry[3]; $m = $entry[4];

echo '
<table style="border: #3399AA 1px solid;"><tr style="background: #3399AA; font: bold 10px verdana,sans-serif; color: #FFFFFF;">
<td width="170">'.$n.'</td>
<td align="right" width="170">'.$d.'</td>
</tr>
';

if($s != 'none'){
echo '
<tr><td colspan="2" style="font: 10px verdana,sans-serif; color: #3399AA;">
<b>Site: </b><a href="'.$s.'">'.$s.'</a>
<center><div style="width: 250; height: 1px; background: #3399AA; margin: 10px;"></div></center></td></tr>
';
}

echo '
<tr>
<td colspan="2" style="font: 10px verdana,sans-serif; color: #3399AA;">'.$m.'</td>
</tr></table><br>
';

}

flock($file, LOCK_UN);
fclose($file);

?>
<!-- END SCRIPT -->

<br><br><div style="width: 400px; height: 1px; background: #3399AA;"></div><br>

You can leave a message in the guestbook with the form below.<br>
Feilds with a "*" are required.<br><br>

<?php
if($_GET['error'] == 1){
echo '<font color="red">You must fill in your name and your message.</font><br><br>';
}

if($_GET['post'] == 1){
echo '<font color="green">Your message has been posted. Thank you!</font><br><br>';
}
?>

<form action="post.php" method="post">

<table>
<tr><td align="right" valign="middle" id="cell">*Name:</td><td><input id="input" type="text" name="name" maxlength="50"></td></tr>
<tr><td align="right" valign="middle" id="cell">Site:</td><td><input id="input" type="text" name="site" maxlength="200"></td></tr>
<tr><td align="right" valign="top" id="cell">*Message:</td><td><textarea id="input" name="msg" rows="5"></textarea></td></tr>
<tr><td></td><td align="center"><input id="submit" type="submit" value="Post"></td></tr>
</table>

</form>

</div>

</center>

Link to comment
Share on other sites

Ummm. If you are in a programming class and are expected to use a specific server and that server does not have the error_reporting/display_errors settings set up or they cannot be setup to give you the most HELP possible while learning, developing, and debugging your code, you have got to ask yourself if you are in the right class.

 

Learning, developing, and debugging php code without the help from php's error_reporting is kind of like a blind guy driving a car on the highway. You might eventually get where you are going (produce some working code), but it is going to take you a very long time (at least 10x longer) and when you get there you will be all bloody and beat up (you will have wasted a lot of effort on just getting your code to run and with not much effort actually going into the coding.)

 

P.S. You can install Apache/PHP/mysql on just about any personal computer/laptop that has a supported operating system.

Link to comment
Share on other sites

@PFMaBiSmAd:

I totally agree, but there is literally one computer class at my school (still in high school) and you get to choose what you learn. So it's not like it's specifically a PHP class, so they don't have it set up for people trying to learn PHP. I've had SO many issues with stuff also because they don't let you change permissions on things! And I could work on stuff at home, but I'd have to keep taking my files back and forth with a flash drive or something. It's a really annoying situation, but it's all I've got until I go to college.

Link to comment
Share on other sites

If you have an empty entry at the end of your file ... that is if the last line has a newline at the end of it, this line of code

if(empty($entry)){exit;}

is terminating your script. It will not exit the while and it will not show any HTML after the loop. An exit is an exit, quit, stop, don't do anything else.

 

You need to break out of the while there.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.