Jump to content

How to echo/print


Xynnem

Recommended Posts

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

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

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

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

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.