sciencebear Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/ Share on other sites More sharing options...
Bricktop Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940411 Share on other sites More sharing options...
sciencebear Posted October 20, 2009 Author Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940414 Share on other sites More sharing options...
kickstart Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940422 Share on other sites More sharing options...
sciencebear Posted October 20, 2009 Author Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940426 Share on other sites More sharing options...
Bricktop Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940429 Share on other sites More sharing options...
sciencebear Posted October 20, 2009 Author Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940435 Share on other sites More sharing options...
mrMarcus Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940437 Share on other sites More sharing options...
sciencebear Posted October 20, 2009 Author Share Posted October 20, 2009 your HTML is not valid HTML .. do not be lazy, use " within your tags Thanks for catching that, although that is still not the problem. I also will install the addon you recommended. Link to comment https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940447 Share on other sites More sharing options...
mrMarcus Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940451 Share on other sites More sharing options...
kickstart Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940452 Share on other sites More sharing options...
Bricktop Posted October 20, 2009 Share Posted October 20, 2009 Hi sciencebear, Are you able to post your FULL page code? Link to comment https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940455 Share on other sites More sharing options...
sciencebear Posted October 20, 2009 Author Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940460 Share on other sites More sharing options...
mrMarcus Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940468 Share on other sites More sharing options...
Bricktop Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940472 Share on other sites More sharing options...
kickstart Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940474 Share on other sites More sharing options...
mrMarcus Posted October 20, 2009 Share Posted October 20, 2009 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 https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940476 Share on other sites More sharing options...
kickstart Posted October 20, 2009 Share Posted October 20, 2009 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. Took me long enough to spot it and I don't even have the excuse of it being early morning. All the best Keith Link to comment https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940478 Share on other sites More sharing options...
Dorky Posted October 20, 2009 Share Posted October 20, 2009 i have had the same exp with while loops rearranging the html. $resultu = $result[username]; $resultb = $result[body] echo "<b>$resultu</b><br /> <a href=/test/profile.php?username=$resultu&page=1>$resultb</a><br />"; Link to comment https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-940493 Share on other sites More sharing options...
sciencebear Posted October 21, 2009 Author Share Posted October 21, 2009 Yep, it was the or die. I had put that in there when I was writing the PHP to try and figure out what was wrong with my query and had forgotten to take it out. Thank you all. Link to comment https://forums.phpfreaks.com/topic/178349-solved-php-messes-with-html-code/#findComment-941086 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.