Sarah_au Posted June 20, 2010 Share Posted June 20, 2010 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 More sharing options...
ram4nd Posted June 20, 2010 Share Posted June 20, 2010 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. Link to comment https://forums.phpfreaks.com/topic/205341-query-results-in-4-column-html-table/#findComment-1074693 Share on other sites More sharing options...
kratsg Posted June 20, 2010 Share Posted June 20, 2010 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. Link to comment https://forums.phpfreaks.com/topic/205341-query-results-in-4-column-html-table/#findComment-1074695 Share on other sites More sharing options...
Sarah_au Posted June 21, 2010 Author Share Posted June 21, 2010 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. Link to comment https://forums.phpfreaks.com/topic/205341-query-results-in-4-column-html-table/#findComment-1074856 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.