...

View Full Version : Using ForEach help.



ptmuldoon
12-24-2007, 07:32 PM
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?


$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++;
}

Fumigator
12-24-2007, 07:56 PM
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?

ptmuldoon
12-24-2007, 08:50 PM
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.



$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>";
}
}

Inigoesdr
12-24-2007, 08:57 PM
But how do you change the query to use "SELECT * ..." instead of just one field?
Replace the "id" after "SELECT" with "*":

$sql = 'SELECT * FROM `games` WHERE `id` > 0';

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.

ptmuldoon
12-24-2007, 09:29 PM
Replace the "id" after "SELECT" with "*":

$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 * ..... " >



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.

Inigoesdr
12-25-2007, 03:48 AM
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.


for($i = 0; $i < 5; $i++)
echo "Each Game field is: " . $game[$i] . "<BR>";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum