...

View Full Version : how do i write a for each?



jarv
08-06-2008, 04:52 PM
I want to write something out in my HTML part of code to say FOR EACH country looping

PHP


//SQL to get country count
$result = mysql_query("SELECT country.country, country.countryID AS id, COUNT(country.countryID) As country_count FROM headertbl LEFT JOIN country ON headertbl.countryID=country.countryID GROUP BY country ORDER BY country ASC");

$country_select_options = '<option value="">Search country</option>';
while ($row = mysql_fetch_assoc($result))
{
$countries[$row['id']]['country'] = $row['country'];
$countries[$row['id']]['country_count'] = $row['country_count'];
$country_select_options .= '<option value="display.php?country_id='.$row['id'].'">'.$row['country'].' (x'.$row['country_count'].')</option>'."\n";
}


HTML


<img src="http://chart.apis.google.com/chart?cht=p3&chd=t:<?php foreach echo $countryloop; ?>&chs=280x100&chl=<?php echo $countryloop; ?>" />

derzok
08-06-2008, 04:59 PM
I'd love to help you but I'm having a difficult time understanding exactly what you're trying to do. I think you need to explain your code more thoroughly - as well as what it is that you're trying to do.

Are you trying to select additional information from your database while inside of your while loop? Or do you already have that data extracted from the database and you're just trying to loop through it? What is this $countries array? What's it full of?

Edit: Since you asked, the basic syntax for a foreach is this:


foreach($array as $index=>$value) {
print "The value of the array index $index is $value\n";
}

You can omit the $value if you want:


foreach($array as $value) {
print "The value is $value\n";
}

And even the $value:


foreach($array) {
print "In perl the current array value would be in the $_ variable - I don't know why you'd even do this in php. Use a for or while.\n";
}

JohnDubya
08-06-2008, 05:07 PM
And jarv, as always, check the PHP manual first. It has all the information you need on how to use PHP code.

http://us3.php.net/foreach

If you have a specific question, then you can post it here. But all of these general issues are already covered in a million different places. Use Google! :)

jarv
08-06-2008, 05:21 PM
thanks for your help, I tried writing it out, what have i done wrong to get the error: Parse error: syntax error, unexpected T_LOGICAL_AND in /Users/avertstaff/Sites/johns/headers/display.php on line 273



<?php foreach($row['country'] as $country) and foreach ($row['country_count'] as $countrycount) {?>
<img src="http://chart.apis.google.com/chart?cht=p3&chd=t:<?php echo $country ?>&chs=280x100&chl=<?php echo $countrycount; ?>" />
<? }?>

derzok
08-06-2008, 05:26 PM
A foreach will not loop on a single array value like that ($row['country']) you need to supply it with an array. Furthermore, you certainly can't have two foreach's next to each other connected by an AND like that. Explain in words what you are trying to do.

jarv
08-06-2008, 07:19 PM
i'm just tring to loop through records

jarv
08-07-2008, 09:21 AM
can anyone help on this please>?

jaap
08-09-2008, 01:43 PM
Still, it's not clear to me what you want. But:


thanks for your help, I tried writing it out, what have i done wrong to get the error: Parse error: syntax error, unexpected T_LOGICAL_AND in /Users/avertstaff/Sites/johns/headers/display.php on line 273



<?php foreach($row['country'] as $country) and foreach ($row['country_count'] as $countrycount) {?>
<img src="http://chart.apis.google.com/chart?cht=p3&chd=t:<?php echo $country ?>&chs=280x100&chl=<?php echo $countrycount; ?>" />
<? }?>

You are using two foreach constructions in one, that is your error.

Try this, it might get you in the right direction:


<?php
$html = '';
while($row = mysql_fetch_assoc()){
$html.= '<img src="http://chart.apis.google.com/chart?cht=p3&chd=t:'.$row['country'].'&chs=280x100&chl='.$row['country_count'];
}
echo $html;


Don't forget: in the while-loop, the variable $row gets overwritten everytime. So after the loop, $row only contains the values of the last iteration. Thats why you want to use the variable $html to concat the html.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum