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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Using ForEach help.

    It was mentioned previously by someone (forget who, but thanks ) that it is generally not a good idea to run a query inside a loop. So in continuing to try and understand the foreach function better, how would restructure this code?
    PHP Code:
    $sql "SELECT * FROM games WHERE id > 0";
    $result mysql_query($sql);

    $i 0;
    while((
    $game mysql_fetch_assoc($result))){
        
    $sql "UPDATE games SET timelimit = '10' WHERE id = ".$game['id']." ";
        
    $query mysql_query($sql);
        echo 
    $game['id'] . "<BR>";
    $i++;


  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You are selecting all rows with an id > 0, then updating each row one at a time. Why not just use the WHERE clause WHERE id > 0 in one update query and forego the select query?

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Your right, I could do that in the above example. I'm merely just trying to understand the foreach loop better, and thus just putting together examples and trying things out to learn.

    I do have this foreach loop working now, which will echo out the ID of each game nicely. But how do you change the query to use "SELECT * ..." instead of just one field?

    I'm also unsure if I put the below foreach loop together correctly, as it is inside a while loop, and unsure if that is necessary, but it was the only way I could get it work.

    PHP Code:
    $sql "SELECT id FROM games WHERE id > 0";
    $result mysql_query($sql);

    while(
    $game mysql_fetch_assoc($result)){
        foreach (
    $game as $value){
            echo 
    "Each Game ID is: " $value "<BR>";        
        }


  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by ptmuldoon View Post
    But how do you change the query to use "SELECT * ..." instead of just one field?
    Replace the "id" after "SELECT" with "*":
    PHP Code:
    $sql 'SELECT * FROM `games` WHERE `id` > 0'
    Quote Originally Posted by ptmuldoon View Post
    I'm also unsure if I put the below foreach loop together correctly, as it is inside a while loop, and unsure if that is necessary, but it was the only way I could get it work.
    Yes, the while loop is necessary. It will loop through each row, and the foreach will loop through each field of the row.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by Inigoesdr View Post
    Replace the "id" after "SELECT" with "*":
    PHP Code:
    $sql 'SELECT * FROM `games` WHERE `id` > 0'
    Thanks. I quess I should have rephrased that. How do change the foreach loop to only echo 1 or 2 fields when you using "SELECT * ..... " >

    Quote Originally Posted by Inigoesdr View Post
    Yes, the while loop is necessary. It will loop through each row, and the foreach will loop through each field of the row.
    Thanks. That actually makes perfect sense to me, and makes it alot clearer as to how the two work together.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by ptmuldoon View Post
    Thanks. I quess I should have rephrased that. How do change the foreach loop to only echo 1 or 2 fields when you using "SELECT * ..... " >
    Well, you should name the specific fields in your query. You could also use a for() loop instead of foreach() to limit the number of fields with the iterator, but this is a waste of resources.
    PHP Code:
    for($i 0$i 5$i++)
        echo 
    "Each Game field is: " $game[$i] . "<BR>"


  •  

    Posting Permissions

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