Xynnem Posted March 16, 2007 Share Posted March 16, 2007 my mysql_query returns a list of names and their country of birth, which I've put into variables: $name = $result['name']; $ctitle = $result['country']; I want to echo/print the names but under headings of each country i.e. Canada Bob Carol USA Dave Eric I'm new to php/mysql so not sure how to script to do what I want! Thanks, Xynnem Link to comment https://forums.phpfreaks.com/topic/42970-how-to-echoprint/ Share on other sites More sharing options...
tauchai83 Posted March 16, 2007 Share Posted March 16, 2007 create a table. //table header: echo ' <table width=100%><tr> <TH>Country</TH> <TH>Name</TH> </tr> //echo data( cont) <tr> <td>$name;</td> <td>$ctitle;</td> <tr> </table> '; Link to comment https://forums.phpfreaks.com/topic/42970-how-to-echoprint/#findComment-208713 Share on other sites More sharing options...
tauchai83 Posted March 16, 2007 Share Posted March 16, 2007 create a table. //table header: echo ' <table width=100%><tr> <TH>Country</TH> <TH>Name</TH> </tr> //echo data( cont) <tr> <td>$ctitle</td> <td>$name</td> <tr> </table> '; does it work? if not i can help you further. Link to comment https://forums.phpfreaks.com/topic/42970-how-to-echoprint/#findComment-208721 Share on other sites More sharing options...
Xynnem Posted March 16, 2007 Author Share Posted March 16, 2007 Thank you so much for replying, but I wondered if you knew how to display it so that the country/ $ctitle only appears once, and not for every instance of a person's name: so instead of Bob Canada Carol Canada Dave USA Eric USA It would appear as: Canada Bob Carol USA Dave Eric I'm thinking: if I do a loop, can I echo the first instance of $ctitle but not again until the value changes? Thanks Again, Xynnem Link to comment https://forums.phpfreaks.com/topic/42970-how-to-echoprint/#findComment-208732 Share on other sites More sharing options...
kenrbnsn Posted March 16, 2007 Share Posted March 16, 2007 If you want to create a display that looks like the one in your original post, you would need to create some temporary arrays to hold the HTML lines while you loop through the database. Something like this: <?php $temp = array(); $q = "your mysql_query"; $rs = mysql_query($q); while ($result = mysql_fetch_assoc($rs)) { if (!is_array($temp[$result['country']]) $temp[$result['country']] = array(); $temp['country'][] = $result['name']; } foreach ($temp as $country => $v) { echo '<span style="font-weight:bold">' . $country . '</span><br>'; foreach($v as $name) echo $name . '<br>'; echo '<br>'; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/42970-how-to-echoprint/#findComment-208736 Share on other sites More sharing options...
Xynnem Posted March 16, 2007 Author Share Posted March 16, 2007 Thank you so much, this works brilliantly (although I had to make a tweak, which I hope is ok, coding-wise : $temp = array(); $q = "select name, country from people order by country, name"; $rs = mysql_query($q); $name = $result['name']; $ctitle = $result['country']; while ($result = mysql_fetch_assoc($rs)) { if (!is_array($temp[$result['country']])) $temp[$result['country']] = array(); $temp[$result['country']][] = $result['name']; } foreach ($temp as $country => $v) { echo '<span style="font-weight:bold">' . $country . '</span><br>'; foreach($v as $name) echo $name . '<br>'; echo '<br>'; } ) I'm not entirely sure what's going on in this but it's going to be so useful to analyse! Can I pester for one more thing? If I wanted to add 'surname' as a field alongside name how could I do this? Thanks again, this is marvellous. Xynnem Link to comment https://forums.phpfreaks.com/topic/42970-how-to-echoprint/#findComment-208833 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.