Forum Moderators: coopster

Message Too Old, No Replies

mysql and php issues

         

jtsymbo

12:02 am on Apr 10, 2007 (gmt 0)

10+ Year Member



Bear with me, I'm new at this. I have a query that I want to display as a list on a web page. I call up the following recordset. Name, Color and Ranking are the fields:

Name Color Ranking
Bob Orange 1
Mark Red 1
John Blue 1
Bob Yellow 2
Mark Green 2
John Purple 2

Can I convert the above list to display in a table format using php like this:
Bob Mark John
1 Orange Red Blue
2 Yellow Green Purple

I'm not sure how to approach it. Any ideas?

jatar_k

12:41 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld jtsymbo,

one way is to build an array and then use that array to output your table.

have you got any code so far?

Drunk N Japan

1:30 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Your best bet is to read into an array.

$sql = 'SELECT * FROM `table`';
$result = mysql_query($sql) or die ("Couldn't execute query.");

//initialize table
echo "<table width=\"100%\" style=\"border:1px solid #000000; font-size:x-small;\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">";

echo "<tr style=\"background-color:#802020; color:#FFFFFF;\"> <th>City</th> <th>State</th> <th>Zip</th> </tr>";

//put results into an array one row at a time
while ($row= mysql_fetch_array($result))
{
//sample array with city, state, and zip if I want to use the data for something else later
$tempzip = $row['Zip_Code'];
$tempstate = $row['State'];
$tempcity = $row['City'];
//I can still do any other work I need to do on that line here
// Print out the contents of each row into a table
echo "<tr bgcolor=\"$colour\" class=\"trhover\"><td>";
echo $row['City'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Zip'];
echo "</td><td>";
}
echo "</table>";


The colors etc. are there because I was too lazy to delete them. You would have to set up your table to read the ranking before placing the names in the appropriate row.
ie. if ($ranking = 1){//print in row 1}
else {//print in row2}
I hope this helps. I have received help from several people here and I am just trying to expand the good karma circle. I could be wrong on my observations but it is a close match to what you are asking. Good Luck!

Drunk

jtsymbo

2:41 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Thanks for the prompt replies. I obviously need to learn more about arrays (not an easy subject for the uninitiated). But your advice and coding example points me in the right direction.
jtsymbo

jatar_k

2:43 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



give it a shot and see what you come up with

if you have some trouble we would be happy to help more

jtsymbo

6:02 pm on Apr 17, 2007 (gmt 0)

10+ Year Member



Do I want to convert the query ($result)into an array, inspect the content of the array (print_r?)and then insert specific array elements into rows? How would I do this. Thanks!