Log in

View Full Version : foreach and query


Lionel
01-20-2006, 08:37 PM
Why would the below return

name1
name2

name1
name2
name3
name4

name1
name2
name3
name4
name5
name6

repeating the previous values over and over? I only want to do the same query for every valid key and return the values for that key only.

foreach($playerid1 as $playerkey1=>$playervalue1){
if (!empty($playervalue1)){
$getplayers1 = $DB_site->query("SELECT * FROM cclpinternational_players WHERE id = $playerkey1");
while ($getplayer1 = $DB_site->fetch_array($getplayers1)){
$lname1=$getplayer1['lname'];
$fname1=$getplayer1['fname'];
eval('$players1 .= "' . fetch_template('soccer_viewstatsplayer1') . '";');
}
}
}

filburt1
01-20-2006, 09:41 PM
My god, PLEASE INDENT. I will honestly try to help you once you do so. It is completely, 100% impossible to read your code right now.

Lionel
01-20-2006, 09:55 PM
Sorry. Do not know what you mean by indent. Hope this is what you meant.



$playerid1=unserialize($getname['preddata1']);
foreach($playerid1 as $playerkey1=>$playervalue1)
{
if (!empty($playervalue1))
{
$getplayers1 = $DB_site->query("SELECT * FROM cclpinternational_players WHERE id = $playerkey1");
while ($getplayer1 = $DB_site->fetch_array($getplayers1))
{
$lname1=$getplayer1['lname'];
$fname1=$getplayer1['fname'];
eval('$players1 .= "' . fetch_template('soccer_viewstatsplayer1') . '";');
}
}
}

Guest190829
01-20-2006, 09:57 PM
Here's what he means by indent:

$playerid1=unserialize($getname['preddata1']);
foreach($playerid1 as $playerkey1=>$playervalue1)
{
if (!empty($playervalue1))
{
$getplayers1 = $DB_site->query("SELECT * FROM cclpinternational_players WHERE id = $playerkey1");

while ($getplayer1 = $DB_site->fetch_array($getplayers1))
{
$lname1=$getplayer1['lname'];
$fname1=$getplayer1['fname'];

eval('$players1 .= "' . fetch_template('soccer_viewstatsplayer1') . '";');
}
}
}


And for the solution it has to be the foreach loop that's causing that...

peterska2
01-20-2006, 09:58 PM
indent - tab in

so text lokks like this (ok it's gobbledegook but you get the point)

boqscbuocdubocdbjowcbisdvidvysibjocdbsdbdbsuibvius
bocdbouvbouvbousbuosv
bocdqboobdbodwvbouvfbou
ibhdbiwdvbubuivwbui
bwcdbuowcdbucdwbuo
owcdobwdvbo
obuwdvbuowvdbouvbwd
bodbuowdbuovwdbou

nwdvbovwboovwbbdbuovbuovwfbou
obudbuovdobuvsdbuo

Lionel
01-20-2006, 11:15 PM
And for the solution it has to be the foreach loop that's causing that...
thanks for indent lesson. The foreach loop is needed so I repeat that query for every key.

filburt1
01-20-2006, 11:39 PM
Never query in a loop.

Lionel
01-20-2006, 11:42 PM
ok. Then how would I do that query for every key?

I am having the same problem with a non query
$minutes1 returns only one line in the eval.


//get minutes1
$playerid1=unserialize($getname['preddata1']);
foreach($playerid1 as $playerkey1=>$playervalue1)
{
if (!empty($playervalue1))
{
$minutes1=$playervalue1;
}
}

P.S. How do you guys tab in the editor? Nothing happens for me. I had to do spaces.