Jump to content

query results in 4 column html table


Sarah_au

Recommended Posts

Hi,

I have a table that contains the states with the suburbs.  An easy enough query to pull out all the suburbs in one state but I need to display them in two columns.

 

I also have a table that holds the details of certain people, lets say nurses, living in the suburbs but the state isn't available in that table.

 

So what I would need is a 2 column html table split into 2 and then displayed side by side with the number of nurses displayed alongside the suburb.

suburb No. Nurses  suburb nurses

 

I know the first query is going to be

select * from table_suburbs where 'state'=ohio

but I am perplexed when it comes to calculating the number

Link to comment
https://forums.phpfreaks.com/topic/205341-query-results-in-4-column-html-table/
Share on other sites

He's going to need quotes for states like "New York" that have a space in them. Here's the appropriate query:

 

<?php
$query = "SELECT * FROM `table_suburbs` WHERE `state`='$state' ";

if(mysql_num_rows($query)==0){
     die('No results returned!');
}

echo "<table><tr><td>Suburb</td><td># Nurses</td><td>Suburb</td><td># Nurses</td></tr>";
$i = 0;//indexing variable

while($row = mysql_fetch_array($query)){
     if($i%2){echo "</tr>";}else{echo "<tr>";}
     echo "<td>{$row['suburb']}</td><td>{$row['nurses']}</td>";
     $i++;
}

if($i%2){echo "<td> </td><td> </td>";}
echo "</tr></table>";
?>

 

I have an indexing variable which keeps track of which column you are in (left column = even; $i%2=0.... right column = odd; $i%2=1). See if it makes sense?

No qutoes!

select * from table_suburbs where state=ohio

 

You can do this by putting foreach in foreach. Please explain it so it would be understandable, I don't get what do you want.

 

He's going to need quotes

I don't know how many men you know that use the name Sarah but I ain't one of them lol.

 

<?php
$query = "SELECT * FROM `table_suburbs` WHERE `state`='$state' ";

if(mysql_num_rows($query)==0){
     die('No results returned!');
}

echo "<table><tr><td>Suburb</td><td># Nurses</td><td>Suburb</td><td># Nurses</td></tr>";
$i = 0;//indexing variable

while($row = mysql_fetch_array($query)){
     if($i%2){echo "</tr>";}else{echo "<tr>";}
     echo "<td>{$row['suburb']}</td><td>{$row['nurses']}</td>";
     $i++;
}

if($i%2){echo "<td> </td><td> </td>";}
echo "</tr></table>";
?>

 

Yes that would work if the nurses info was in the same table but unfortunately it isn't.  The nurses info is in another table so a second query needs to be performed and the result set alongside the suburb.  I might have to think a about it a bit more.  Damn flu is fuddling the grey matter,

thanks.

 

I have an indexing variable which keeps track of which column you are in (left column = even; $i%2=0.... right column = odd; $i%2=1). See if it makes sense?

No qutoes!

select * from table_suburbs where state=ohio

 

You can do this by putting foreach in foreach. Please explain it so it would be understandable, I don't get what do you want.

 

 

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.