Jump to content

[SOLVED] Basic Display Results in a Table - Help Please


widget

Recommended Posts

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

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

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

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

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.