Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question how do i write a for each?

    I want to write something out in my HTML part of code to say FOR EACH country looping

    PHP
    Code:
    //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
    Code:
    <img src="http://chart.apis.google.com/chart?cht=p3&chd=t:<?php foreach echo $countryloop; ?>&chs=280x100&chl=<?php echo $countryloop; ?>" />

  • #2
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    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:
    PHP Code:
    foreach($array as $index=>$value) {
        print 
    "The value of the array index $index is $value\n";

    You can omit the $value if you want:
    PHP Code:
    foreach($array as $value) {
        print 
    "The value is $value\n";

    And even the $value:
    PHP Code:
    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";

    Last edited by derzok; 08-06-2008 at 04:04 PM.
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #3
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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!

  • #4
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    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

    Code:
    <?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; ?>" />
    	<? }?>

  • #5
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    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.
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #6
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    i'm just tring to loop through records

  • #7
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    can anyone help on this please>?

  • #8
    New Coder
    Join Date
    Aug 2005
    Location
    Groningen, Netherlands
    Posts
    57
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Netscape

    Still, it's not clear to me what you want. But:

    Quote Originally Posted by jarv View Post
    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

    Code:
    <?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:
    Code:
    <?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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •