Page 1 of 1
Display Mysql data in a table
Posted: Sat Jun 25, 2005 8:36 pm
by jackie111
hello
i need to display some data from mysql in a table, the table has 4 cols, looks like:
1------2------3------4
5------6------7------8
.
.
.
how to do that? thanks again for the great help from this group!
jackie
Posted: Sat Jun 25, 2005 10:19 pm
by Burrito
Code: Select all
$query = "select * from myTable where blah = 'blah'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo $row['1'];
//etc
}
notice the quotes around the 1 in the $row[] array. I put those there because of the sample you gave of your table structure. What that does is actually use the key value of '1' which could be anywhere in the array depending on where it is in the table. if you use $row[1] however, it's going to echo the second value in the array (arrays are 0 based).
Posted: Mon Jun 27, 2005 7:10 am
by jackie111
i am sorry, but the code is wrong, i meant, there are serval records(rows) in the table, let's suppose there are 6 lines, such as
1-----tom----26----male
2-----tt-----34-----female
3-----yy-----25-----male
4-----uu-----12-----male
5-----ee-----45-----female
6-----cc-----34-----male
i need to display in a table like this:
---------------------
tom-----tt-----yy
uu-----ee-----cc
--------------------
Do you understand now? i hope someone could help me.
jackie
Posted: Mon Jun 27, 2005 7:20 am
by Chris Corbyn
Burrito's code was correct
It may have been a bit more generic than the specific case you're using here but you should be able to change the query and the output to match your needs...
Ah sod it... I'll do but you'll have to kinda try modifying code to suit your needs
Code: Select all
<table>
<?php
$query = "SELECT * FROM `myTable` WHERE `gender` = 'female'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
foreach ($row as $v) {
echo '<td>'.$v.'</td>';
}
echo '</tr>';
}
?>
</table>
Posted: Mon Jun 27, 2005 7:35 am
by jackie111
but i only need 3 cols in the table.
hope this make sense.
Jackie
Posted: Mon Jun 27, 2005 8:59 am
by Chris Corbyn
jackie111 wrote:but i only need 3 cols in the table.
hope this make sense.
Jackie
So change the query to only pull out the columns you need instead of pulling out *.
Are you planning on learning PHP/MySQL or just getting somebody to do it for you?
http://www.mysql.com/
http://www.php.net/
Posted: Mon Jun 27, 2005 10:33 am
by Trenchant
Heres like a bit of a working tutorial. I would suggest you learn more PHP as it will help you in the long run.
Code: Select all
<?php
// First set the query to find the information in the database. *the map*
$sql = mysql_query("take all the infromation here") or DIE(MYSQL_ERROR());
// Now set $count to 0 (just to make sure everything works okay
$count = '0';
// So thats done. Now repeat through all rows of the database and output the information
echo "<table border=1><tr>";
while($sel = mysql_fetch_array($sql)) {
// Print out aall the information
echo "<td>".$sel[row name]."</td>";
// Now add 1 to count.
$count = $count + '1';
// This if statement decides whether or not to add a new <tr>
// Change the value to how many columns you want. ex. 3 or 4
if ($count == '3') {
echo "</tr>";
$count = '0';
}
}
// Now that the information is there finish off the table.
if ($count != '0') {
echo "</tr>";
}
echo "</table>";
Posted: Mon Jun 27, 2005 9:05 pm
by jackie111
WOW, thanks, it works just fun! thanks sooooo omuch!!
