Jump to content

[SOLVED] PHP Messes with HTML Code


sciencebear

Recommended Posts

I recently implemented a new query and while loop onto my site, but unfortunately it messes with the HTML of the page. My page is set up in tables, and this is happening inside a td tag. With the while loop, the td tag does not close and none of the HTML after the while loop show up.

 

The query has no effect. If I comment it out, the page is still funky. If I comment out the while loop, the page is not. This is my code:

 

<?php
//get updates of user's friends
$sql = "SELECT t2.username,t2.type,t2.path,t3.body,t4.id
FROM (SELECT username AS friendname FROM friends WHERE friendname = '$username' AND active='1' AND friend='1' UNION SELECT friendname AS friendname FROM friends WHERE username = '$username' AND active='1' AND friend='1') t1
LEFT JOIN updates t2 ON t1.friendname=t2.username
LEFT JOIN posts t3 ON t2.date=t3.stamp
LEFT JOIN userpics t4 ON t2.date=t4.date
ORDER BY t2.id DESC";

//run query
$query = mysql_query($sql);
$num_rows = mysql_num_rows($query);
while($result = mysql_fetch_array($query) or die(mysql_error())) {

//display for text statuses	
if($result[type] == 'status') {
	echo '<b>'.$result[username].'</b>'.' ';
	echo '<a href=/test/profile.php?username='.$result[username].'&page=1>'.$result[body].'</a>';
	echo '<br />';
}

//display for pictures
if($result[type] == 'picture') {
	$dim = getimagesize($result['path']);

	//resize pictures over 75 pixels wide
	if($dim[0] < 75){
		echo '<b>'.$result[username].'</b> uploaded a picture!';
		echo '<a href=/test/userimgdisplay.php?picid='.$result['id'].'&username='.$result['username'].'&page=1>';
		echo '<img src='.$result[path].' border=0></a>';
		echo '<br />';
	} else {         
		$height = $dim[1]/($dim[0]/75);
		echo '<b>'.$result[username].'</b> uploaded a picture!';
		echo '<a href=/test/userimgdisplay.php?picid='.$result[id].'&username='.$result['username'].'&page=1>';
		echo '<img src='.$result[path].' width=75 height='.$height.' border=0></a>';
		echo '<br />';
	}

}

}

//something makes the HTML following not show up on the page
?>

 

Any ideas?

Link to comment
Share on other sites

Hi sciencebear,

 

In your final if statement you have:

 

$height = $dim[1]/($dim[0]/75);
echo '<tr><td><b>'.$result[username].'</b> uploaded a picture!';

 

The <tr> and <td> you're opening at the start of that echo is not being closed and probably isn't required, just remove them and you should have a page which renders correctly.

 

Hope this helps.

Link to comment
Share on other sites

 

The <tr> and <td> you're opening at the start of that echo is not being closed and probably isn't required, just remove them and you should have a page which renders correctly.

 

Hope this helps.

Thanks for catching that, but that didn't fix it. I tried to fix the whole problem by putting it in a table rather than just having a <br /> tag after each entry, but that didn't work either. Those tags I just left in when I changed it back. But unfortunately, that's not the problem.

Link to comment
Share on other sites

Hi

 

Can't spot a cause of the problem you are having, but in several places you are referring to fields brought back from the database without using inverted commas around the field names. eg

 

echo '<a href=/test/profile.php?username='.$result['username'].'&page=1>'.$result['body'].'</a>';

 

Given the IF statements I wouldn't expect anything to be put out from that loop.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Can't spot a cause of the problem you are having, but in several places you are referring to fields brought back from the database without using inverted commas around the field names. eg

 

echo '<a href=/test/profile.php?username='.$result['username'].'&page=1>'.$result['body'].'</a>';

 

Given the IF statements I wouldn't expect anything to be put out from that loop.

 

All the best

 

Keith

While it would probably be better coding practice on my part to include those, it hasn't made a difference in the output of the code.

Link to comment
Share on other sites

Hi sciencebear,

 

Are you sure your database output doesn't contain any HTML markup?  For example, does $result['body'] contain purely text? 

 

It's worth checking each row you're outputting to make sure none of that is outputting HTML data.

 

Also, when you view the page in your browser, right-click and "View Source", you should be able to see exactly what is being output and where the page is stopping.

 

Hope this helps.

Link to comment
Share on other sites

Hi sciencebear,

 

Are you sure your database output doesn't contain any HTML markup?  For example, does $result['body'] contain purely text? 

 

It's worth checking each row you're outputting to make sure none of that is outputting HTML data.

 

Also, when you view the page in your browser, right-click and "View Source", you should be able to see exactly what is being output and where the page is stopping.

 

Hope this helps.

 

No, I checked all the entries and none have any HTML markup. The column is a text field as well. The source code ends with a <br /> after the last entry the query gets, so it completes all the code I posted earlier and then stops.

Link to comment
Share on other sites

your HTML is not valid HTML .. do not be lazy, use " within your tags, ie:

 

echo '<a href=/test/userimgdisplay.php?picid='.$result[id].'&username='.$result['username'].'&page=1>';

 

should be:

 

echo '<a href="/test/userimgdisplay.php?picid='.$result[id].'&username='.$result['username'].'&page=1">';

 

and so on for you other <a>'s and <img> tags.

 

here, cleaned it for you:

 

//display for text statuses   
if ($result[type] == 'status')
{
	echo '<b>'.$result[username].'</b> ';
	echo '<a href="/test/profile.php?username='.$result[username].'&page=1">'.$result[body].'</a>';
	echo '<br />';
}

//display for pictures
if ($result[type] == 'picture')
{
	$dim = getimagesize ($result['path']);

	//resize pictures over 75 pixels wide
	if ($dim[0] < 75)
	{
		echo '<b>'.$result[username].'</b> uploaded a picture!';
		echo '<a href="/test/userimgdisplay.php?picid='.$result['id'].'&username='.$result['username'].'&page=1">';
		echo '<img src="'.$result[path].'" border="0" /></a>';
		echo '<br />';
	} else {         
		$height = $dim[1]/($dim[0]/75);
		echo '<b>'.$result[username].'</b> uploaded a picture!';
		echo '<a href="/test/userimgdisplay.php?picid='.$result[id].'&username='.$result['username'].'&page=1">';
		echo '<img src="'.$result[path].'" width="75" height="'.$height.'" border="0" /></a>';
		echo '<br />';
	}	  
}

 

make sure you validate your HTML, CSS, etc.  i recommend getting Web Developer Add-on for Firefox.

Link to comment
Share on other sites

there must be something in the code coming from the database .. view the page source from the browser and post it here .. perhaps '$result[path]' has a " in it?  something like that would 'cause the HTML to break.

 

there has to be something, these things don't just happen.

Link to comment
Share on other sites

Hi

 

If the source that is output ends then it suggests the script has stopped running before if has reached the rest of the HTML.

 

Possibly you are getting a major error that is being suppressed by a setting for how errors are handled.

 

As to the missing inverted commas around field names, I would expect that to give an error or to just bring back a random field. PHP would look for a constant called body for example and not find one. Effectively you are trying to get the value from an undefined member of the array you are retrieving.

 

All the best

 

Keith

Link to comment
Share on other sites

I've added the quotation marks suggested, and checked the database for any other discrepancies with HTML but still the error persists. This is the full code of the page I am working on (which works fine with other PHP loops on other pages)

 

<html>
<body>
<table align="center" width="710" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="images/tabletop.png" width="710" height="31" /></td>
  </tr>
  <tr>
    <td><table width="710" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="15" background="images/tableleft.png"> </td>
        <td bgcolor="#FFFFCC">
<?php
//get updates of user's friends
$sql = "SELECT t2.username,t2.type,t2.path,t3.body,t4.id
FROM (SELECT username AS friendname FROM friends WHERE friendname = '$username' AND active='1' AND friend='1' UNION SELECT friendname AS friendname FROM friends WHERE username = '$username' AND active='1' AND friend='1') t1
LEFT JOIN updates t2 ON t1.friendname=t2.username
LEFT JOIN posts t3 ON t2.date=t3.stamp
LEFT JOIN userpics t4 ON t2.date=t4.date
ORDER BY t2.id DESC";

//run query
$query = mysql_query($sql);
$num_rows = mysql_num_rows($query);
while($result = mysql_fetch_array($query) or die(mysql_error())) {

//display for text statuses	
if($result[type] == 'status') {
	echo '<b>'.$result[username].'</b>'.' ';
	echo '<a href="/test/profile.php?username='.$result[username].'&page=1">'.$result[body].'</a>';
	echo '<br />';
}

//display for pictures
if($result[type] == 'picture') {
	$dim = getimagesize($result['path']);

	//resize pictures over 75 pixels wide
	if($dim[0] < 75){
		echo '<b>'.$result[username].'</b> uploaded a picture!';
		echo '<a href="/test/userimgdisplay.php?picid='.$result['id'].'&username='.$result['username'].'&page=1">';
		echo '<img src="'.$result[path].'" border="0"></a>';
		echo '<br />';
	} else {         
		$height = $dim[1]/($dim[0]/75);
		echo '<b>'.$result[username].'</b> uploaded a picture!';
		echo '<a href="/test/userimgdisplay.php?picid='.$result[id].'&username='.$result['username'].'&page=1">';
		echo '<img src="'.$result[path].'" width="75" height="'.$height.'" border="0"></a>';
		echo '<br />';
	}

}

}

//something makes the HTML following not show up on the page
?>
</td>
        <td width="15" background="images/tableright.png"> </td>
      </tr></table></td></tr>
  <tr>
    <td><img src="images/tablebottom.png" width="710" height="26" /></td>
  </tr>
</table>
</body>
</html>

Link to comment
Share on other sites

as kickstart brought up .. if the outputted source is abruptly brought to an end, that would indicate an error.  do you have error_reporting on?

 

to get this straight, the page just stops (nothing is displayed at all after the while loop is executed)?  could you post the outputted source (if any)?

 

check into a parse error or something along those lines.

Link to comment
Share on other sites

Hi sciencebear,

 

You don't seem to have any database connection information, so the MySQL query will be unable to run because it doesn't know what database to connect to or how.

 

Edit:  kickstart spotted it before me, totally missed that, it's the "or die(mysql_error())" statement causing the problem in this line:

 

while($result = mysql_fetch_array($query) or die(mysql_error())) {

 

Take it out and try again.

 

Hope this helps.

Link to comment
Share on other sites

Hi

 

Spotted the problem:-

 

while($result = mysql_fetch_array($query) or die(mysql_error())) {

 

When it tries to get the next record after processing the last one it will die.

 

Just get rid of the or die(mysql_error()) bit and it will be fine. Put it on the mysql_query line instead.

 

All the best

 

Keith

Link to comment
Share on other sites

i saw that too, and while i knew it looked out of place, my brain didn't make the connection .. still having my morning coffee .. need few to wake up.

 

good catch, Keith.

 

hope that solves the issue (it should).

 

to the OP, the 'OR die...' with the while loop is basically saying, "loop as long as there is a record to display from the db, otherwise, die (stop script)".

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.