Jump to content

WHILE loop using wrong value


blinks

Recommended Posts

The first WHILE loop in the code below is executing correctly on the first go-round, and successfully updating the trade_offers table at the end of the loop. In the 2nd go round, it should use the updated values from trade_offers, but instead is using the original values.  Would appreciate any help in locating the problem. There are no errors being output.

 

$getdeclined="SELECT *
      FROM trade_offers
      WHERE pokemon_trade_id=$id";
$declined=mysql_query($getdeclined);
while($row = mysql_fetch_array($declined)){
    $getpostrade="SELECT *
      FROM trade_offers
      WHERE pokemon_trainer = '$row[pokemon_trainer]'";
    $postrade=mysql_query($getpostrade) or die(mysql_error());; 
    while ($row2 = mysql_fetch_array($postrade)){
        $tradepos = $row2['pokemon_pos'];}
    if ($tradepos==NULL){
    $tradepos=0;}
    $getpostrainer="SELECT *
      FROM pokemon_trainer
      WHERE pokemon_trainer = '$row[pokemon_trainer]'";
    $postrainer=mysql_query($getpostrainer) or die(mysql_error());;
    while ($row3 = mysql_fetch_array($postrainer)){
        $trainerpos = $row3['pokemon_pos'];}
    if ($tradepos>$trainerpos){
        $newpos = $tradepos;}
    else if ($trainerpos>$tradepos){
        $newpos = $trainerpos;}
    $newpos +=1;
    $update="UPDATE trade_offers
	 SET pokemon_pos = $newpos
	 WHERE pokemon_id = $row[pokemon_id]";
    $result=mysql_query($update) or die(mysql_error());;
    $newpos = 0;
}

Link to comment
Share on other sites

To answer your question, your values are gotten from pokemon_trade_id and thats when it equals $id. You never change either value so your loop just repeats.

 

Additional help => most of your queries have a double ; at the end. Don't know if this hurts anything, but it can't be good.

 

Go buy a gun and take the guy that thought you to use 'SELECT *' out and shoot him. Your over taxing your database and it soon will ask for a raise for doing all the unnecessary work or it will go out on strike.

 

Your main query only uses two columns. $row[pokemon_id] and $row[pokemon_trainer] why grab everything?

 

$getdeclined="SELECT pokemon_id, pokemon_trainer FROM trade_offers WHERE pokemon_trade_id = $id";
$declined=mysql_query($getdeclined);

while($row = mysql_fetch_array($declined){
$first = $row['pokemon_trainer'];
$second = $row['pokemon_id'];
......

 

You can do this with all your queries. The next one only uses the last row of data:

 

    $getpostrainer="SELECT * FROM pokemon_trainer WHERE pokemon_trainer = '$row[pokemon_trainer]'";

    $postrainer=mysql_query($getpostrainer) or die(mysql_error());;

 

    while ($row3 = mysql_fetch_array($postrainer))

{

        $trainerpos = $row3['pokemon_pos'];

}

 

Your while loop here goes through all rows and ends with the value of $trainerpos set to the value of last row.. Use

$getpostrainer="SELECT pokemon_pos FROM pokemon_trainer WHERE pokemon_trainer = '$first' ASC limit 1";

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.