Jump to content

[SOLVED] How do i create a table from returned Results.


Wildhalf

Recommended Posts

Hi,

I need help. Been racking my brain all night trying to come up with a way to return all records from my table with a certain ID and then dispplay all colums in a table.

My problem is i haven't worked with PHP and arrays yet well i assume i have to use arrays. What im looking for is the SQL command simmilar to  $sql_num_rows (in code below) that returns all the members referrals and also how do i display it...

Thanks in advance for any help given...

[code]

echo "<table border="0" align="center">";
echo " <tr bgcolor="#FFFFFF">";
echo "  <td width="160"><div align="center" class="style4">First Name</div></td>";
echo " <td width="160"><div align="center" class="style4">Last Name</div></td>";
echo " <td width="160"><div align="center" class="style4">eMail</div></td>";
echo "</tr>";
 
$member_id = BBBQ1090
 
$sql_num_rows = mysql_query("SELECT * FROM tblmembers WHERE referred_by='$member_id'");
 
$num_rows = mysql_num_rows($sql_num_rows);
 
for ( $counter = 0; $counter <= $num_rows ; $counter += 1) {
  echo " <tr bgcolor="#FFFFCC">";
echo " <td width="160" height="20" bgcolor="#eeeeee">
<div align="center" class="style5">
<? php echo"$firstname";?>
<div>
</td>";
echo " <td width="160" height="20" bgcolor="#eeeeee">
<div align="center" class="style5">
<? php echo"$lastname";?>
</div>
</td>";
echo "<td width="160" height="20" bgcolor="#FFFFCC" scope="row">
<div align="center" class="style5">
<? php echo"$email";?>
</div>
</td>";
 echo "</tr>";
}

echo "</table>";

[/code]
Ok I am not going to write the whole thing for you but basically what you want is [url=http://us2.php.net/manual/en/function.mysql-fetch-assoc.php]mysql_fetch_assoc()[/url] or [url=http://us2.php.net/manual/en/function.mysql-fetch-array.php]mysql_fetch_array()[/url]. Both can be used in the same manner. Here is a simple example of what I mean..

[code=php:0]
$sql = mysql_query("SELECT `something` FROM `somewhere` WHERE `something_else` = '$whatever'") or die(mysql_error());

//now you could do something like this to make sure that you don't loop
//through the results if there aren't any to be looped through
if (mysql_num_rows($sql) < 1) {
    die("There are not matching results");
}

//now we loop through the results..

while($row = mysql_fetch_array($sql)) {
        echo "<table>
                    <tr>
                      <td>{$row['your_row_name']}</td>
                        <td>{$row['your_row_name2']}</td>
                        <td>{$row['your_row_name3']}</td>
                  </tr>
                  </table>";
}
[/code]


Hope that helps,
Tom
You had lots and lots of errors. You should have been getting an error when you first tested the script. Anyways, this is what I fixed:

1) You need to escape your quotation marks with slashes (\)
2) You kept opening and closing the PHP tag when it didn't even need to be closed/opened. In some places you opened it up again when it was already open.
3) To display records you need to use mysql_fetch_assoc() - You can see it in the script.

[code]
<?php

echo '<table border="0" align="center">';
echo ' <tr bgcolor="#FFFFFF">';
echo '  <td width="160"><div align="center" class="style4">First Name</div></td>';
echo ' <td width="160"><div align="center" class="style4">Last Name</div></td>';
echo ' <td width="160"><div align="center" class="style4">eMail</div></td>';
echo '</tr>';
 
$member_id = "BBBQ1090";
 
$sql_num_rows = mysql_query("SELECT * FROM tblmembers WHERE referred_by='$member_id'");
$row = mysql_fetch_assoc($sql_num_rows);
$num_rows = mysql_num_rows($sql_num_rows);
 
for ( $counter = 0; $counter <= $num_rows ; $counter++ ) {
  echo ' <tr bgcolor="#FFFFCC">';
echo " <td width=\"160\" height=\"20\" bgcolor=\"#eeeeee\">
<div align=\"center\" class=\"style5\">
  {$row['firstname']}
</div></td>";
echo " <td width=\"160\" height=\"20\" bgcolor=\"#eeeeee\">
<div align=\"center\" class=\"style5\">
{$row['lastname']}
</div>
</td>";
echo "<td width=\"160\" height=\"20\" bgcolor=\"#FFFFCC\" scope=\"row\">
<div align=\"center\" class=\"style5\">
{$row['email']}
</div>
</td>";
  echo "</tr>";
}

echo "</table>";


?>[/code]
Thank you both for your help

tomfmason

I modified your code a little but got it working..

[code]while($row = mysql_fetch_array($sql)) {

    include 'html_1.htm;';
    echo $row['firstname'];
    include 'html_2.htm;';
    echo  $row['lastname'];
    include 'html_3.htm;';
    echo  $row['email'];
    include 'html_4.htm;';
           
}[/code]

The include statments are just segments of my Table...

pocobueno1388

I didn't get your message till i had sorted the problem took me a while to figure it out... But i know your way would have been easier.. than what i did..


Thanks All.


Kieron
Here is the code i used to sort problem...


[code]
$member_id = $refer_id;
 
$sql = mysql_query("SELECT * FROM tblmembers WHERE referred_by='$member_id' ORDER BY no_referrals DESC") or
die(mysql_error());
 
//$num_rows = mysql_num_rows($sql_num_rows);

//now you could do something like this to make sure that you don't loop
//through the results if there aren't any to be looped through
if (mysql_num_rows($sql) < 1) {
    die("There are not matching results");

 
//now we loop through the results..
//for ( $counter = 0; $counter <= $num_rows ; $counter += 1) {
while($row = mysql_fetch_array($sql)) {

echo '<tr bgcolor="#FFFFCC">
<td  align="center" width="160" height="20" bgcolor="#eeeeee">
<div align="center" class="style5">';
echo $row['firstname'];
echo ' ' ;
echo $row['lastname'];
echo '<div>
</td>
<td width="160" height="20" bgcolor="#eeeeee">
<div align="center" class="style5"> ';
echo $row['no_referrals'];
echo '</div>
</td>
<td width="160" height="20" bgcolor="#FFFFCC" scope="row">
<div align="center" class="style5"> ';
echo '<a href="mailto:';
echo $row['email'];
echo '">';
echo $row['email'];
echo '</a>';
echo '</div>
</td>
</tr> ';

}

[/code]

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.