Feek
Feek

Reputation: 9

I want to take my users from my table and display them with a html table

I want to take my users from my table and display them with a html table.i want their Name Avatar displayed. All of this information is in the table. I want to display this in a table with 4 cols by as many rows needed.

This is my full code as asked for:

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = '';
$db_user = '';
$database = '';
$db_pwd = '';
$table = '';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table} WHERE livedj=1");
if (!$result) {
die("Query to show fields from table failed");
}
$max_rows = 6;
$max_cols = 4;
echo "<table>";
$rows = 1;
while ($rows <= $max_rows)
{
echo "<tr>";
$cols = 1; // reset columns to 1 for each row
while ($cols <= $max_cols)
{
     echo "<td>". $row['name'] . "<br />
     <img src=\"http://myurl/path/to/pics/" . $row['avatar'] . "\" height=\"150\" width=\"150\" /></td>";
     $cols++; // column counter
}
echo "</tr>";
$rows++;  //row counter
}
echo "</table>";
?>
</body></html>

This only give out a page with the correct table made but it is not displaying any information from mysql...

and yes I have deleted the database and passwords settings here and my copy has the correct settings.

Upvotes: 0

Views: 235

Answers (6)

Nicolas Stamm
Nicolas Stamm

Reputation: 123

I hope you meant to say your SQL table has 6 data fields (7 with the 'livedj' you seem to be using in your WHERE clause), and as many data entries as needed.

I am also assuming you want your output to look something like this:

+------+--------+-----------------------------+
| Name | Avatar | Other stuff (email,fb,...)  |
+------+--------+-----------------------------+
| John | :-)    | blah                        |
| ...  |  ...   | ...                         |
+------+--------+-----------------------------+

Then that code would be:

<?php
$result = mysql_query("SELECT name, address, twitter, email, facebook, avatar FROM {$table} WHERE livedj=1 ");
if (!$result) {
die("Query to show fields from table failed");
}
echo "<table border='1'>";

while($row = mysql_fetch_array($result)){
  echo "<tr><td>".$result["name"]."</td><td>"
  ."<img alt='avatar' src='path/to/img/folder/".$result["avatar"]." /></td><td>"
  .$result["address"]."</td><td>".$result["email"]."</td><td>"
  .$result["twitter"]."</td><td>".$result["facebook"]"</td></tr>\n";
}
echo "</table>
?>

Upvotes: 1

mlishn
mlishn

Reputation: 1667

echo '<table>';

while($row = mysql_fetch_array($result))   {   
  echo "<tr>";
  echo "<td>".$row['name']."</td>";
  echo "<td>".$row['facebook']."</td>";
  echo "<td>".$row['twitter']."</td>";    
  echo "</tr>";   
} 

echo '</table>';

Output would be :

name1 Facebook1 Twitter1

name2 Facebook2 Twitter2

name3 Facebook3 Twitter3

Upvotes: 0

Brock B.
Brock B.

Reputation: 377

You need to add more table data tags in order to get multiple columns.

For instance

<table>
 <tr>
   <td>Column 1</td>
   <td>Column 2</td>
   <td>Column 3</td>
 <tr>
</table>

Upvotes: 0

voodoo417
voodoo417

Reputation: 12111

while ($row = mysql_fetch_array($result))
echo "<tr><td><img src=\"http://myurl.com/images/" . $row['avatar'] . "\" height=\"150\" width=\"150\" /><br />" . "<span style=\"text-align:center;\">" . $row['name'] . "</span></td></tr>";

Upvotes: 0

Isaac Gonzalez
Isaac Gonzalez

Reputation: 1734

Aren't you missing </table> at the end of the for loop ?

Upvotes: 0

paulsm4
paulsm4

Reputation: 121869

You need to iterate through each row in the result set. For example:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
  }

mysql_close($con);
?> 

PS: You're also probably better off using one of the newer API's, rather than using mysql_connect()/mysql_query() and friends:

http://php.net/manual/en/function.mysql-query.php

Upvotes: 1

Related Questions