widget Posted May 12, 2007 Share Posted May 12, 2007 Hi, I need to have the output from the below code placed into a table and have no clue on how to do it. Any help is much appreciated. Using php and mysql <?php $rank_check = 0; include "header.inc.php"; $game=$_GET['game']; print "$openHTML"; ?> <b><font color="#FF3399" size="4" face="Arial, Helvetica, sans-serif">Trophy Case </font></b><br><br> <?php $sql_query = "SELECT * FROM `avatar` WHERE `owner` = $user_id ORDER BY `avatar`.`avatar_id` ASC"; //store the SQL query in the result variable $result = mysql_query($sql_query); if(mysql_num_rows($result)) { echo ("<table cellpadding=2 width=100%><tr>"); //output as long as there are still available fields while($row = mysql_fetch_row($result)) { echo ("<td><img src=$base_url/images/user_images/opg_1/trophy/$row[2].gif></td>"); } } //if no fields exist else { echo "This User has not won any trophies"; } echo ("</tr></table>"); print "$closeHTML"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/51034-solved-basic-display-results-in-a-table-help-please/ Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 It appears to already be inside tables. Remove the () in your echo statements as I believe that causes errors. I may be wrong there, but I NEVER use () with echo. Second, whats the point of echoing plain html? Why not just drop out of the php when you need. That way you can code the HTML properly with "" around your values and such. e.g. <?php $rank_check = 0; include "header.inc.php"; $game=$_GET['game']; print "$openHTML"; ?> <font color="#FF3399" size="4" face="Arial, Helvetica, sans-serif">Trophy Case </font> <?php $sql_query = "SELECT * FROM `avatar` WHERE `owner` = $user_id ORDER BY `avatar`.`avatar_id` ASC"; //store the SQL query in the result variable $result = mysql_query($sql_query); if(mysql_num_rows($result)) { ?> <table cellpadding="2" width="100%"> <?php //output as long as there are still available fields while($row = mysql_fetch_row($result)) { ?> <tr><td><img src="<?=$base_url.'/images/user_images/opg_1/trophy/'.$row[2].'.gif' ?>" ></td></tr> <!-- I assume you want individual rows, rather than one big ass row I moved your <tr></tr> inside the while loop --> <?php } } //if no fields exist else { echo "This User has not won any trophies"; } ?></table><?php print "$closeHTML"; ?> Nate Quote Link to comment https://forums.phpfreaks.com/topic/51034-solved-basic-display-results-in-a-table-help-please/#findComment-251171 Share on other sites More sharing options...
Barand Posted May 12, 2007 Share Posted May 12, 2007 The problem could be with the image src, it needs quotes around it. If there are any spaces in the src string it won't work echo ("<td><img src='$base_url/images/user_images/opg_1/trophy/$row[2].gif'></td>"); Quote Link to comment https://forums.phpfreaks.com/topic/51034-solved-basic-display-results-in-a-table-help-please/#findComment-251216 Share on other sites More sharing options...
widget Posted May 12, 2007 Author Share Posted May 12, 2007 Thank you for your help, although what I need is the table to display say 5 columns and rows added as needed. Currently I can have them all in one big row or all along side each other. eg: * = image * * * * * * * * * * * * * * * Quote Link to comment https://forums.phpfreaks.com/topic/51034-solved-basic-display-results-in-a-table-help-please/#findComment-251421 Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 The easiest way I have found for creating columns and breaking up data is using arrays. I may have made a syntax error in the code here, so beware of that, but it is the general idea of how I split data up into multiple table columns.. this code gives you 5 columns. to add more, just add <td><?=$images[$x++]; ?></td> <?php $rank_check = 0; include "header.inc.php"; $game=$_GET['game']; print "$openHTML"; ?> <font color="#FF3399" size="4" face="Arial, Helvetica, sans-serif">Trophy Case </font> <?php $sql_query = "SELECT * FROM `avatar` WHERE `owner` = $user_id ORDER BY `avatar`.`avatar_id` ASC"; //store the SQL query in the result variable $result = mysql_query($sql_query); if(mysql_num_rows($result)) { ?> <table cellpadding="2" width="100%"> <?php //output as long as there are still available fields while($row = mysql_fetch_row($result)) { $images[]='<img src="'.$base_url.'/images/user_images/opg_1/trophy/'.$row[2].'.gif>' ; } $x=0; while($x < count($images) { ?> <tr> <td><?=$images[$x++]; ?></td> <td><?=$images[$x++]; ?></td> <td><?=$images[$x++]; ?></td> <td><?=$images[$x++]; ?></td> <td><?=$images[$x++]; ?></td> </tr> <?php } } //if no fields exist else { echo "This User has not won any trophies"; } ?></table><?php print "$closeHTML"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/51034-solved-basic-display-results-in-a-table-help-please/#findComment-251430 Share on other sites More sharing options...
Barand Posted May 12, 2007 Share Posted May 12, 2007 or <?php $sql_query = "SELECT * FROM `avatar` WHERE `owner` = $user_id ORDER BY `avatar`.`avatar_id` ASC"; //store the SQL query in the result variable $result = mysql_query($sql_query); $cols = 5; $count = 0; if(mysql_num_rows($result)) { echo '<table cellpadding="2" width="100%">'; //output as long as there are still available fields while($row = mysql_fetch_row($result)) { if ($count % $cols == 0) echo '<tr>'; echo "<td><img src='$base_url/images/user_images/opg_1/trophy/{$row[2]}.gif' ></td>"; ++$count; if ($count % $cols == 0) echo '</tr>'; } if ($count % $cols != 0) echo '</tr>'; echo '</table>'; } else //if no fields exist { echo "This User has not won any trophies"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51034-solved-basic-display-results-in-a-table-help-please/#findComment-251432 Share on other sites More sharing options...
widget Posted May 13, 2007 Author Share Posted May 13, 2007 Thank you guys, your all absolute winners I really appreciate the help and now I have a base to work off for other pages. Thank you :-* Quote Link to comment https://forums.phpfreaks.com/topic/51034-solved-basic-display-results-in-a-table-help-please/#findComment-251967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.