Page 1 of 1

php loop update

Posted: Fri Dec 10, 2010 7:10 pm
by ConceptDestiny
I'm trying to loop through the rows once and update the rows to the new corresponding values, but the script seems to be looping and updating the rows for the amount of entries are in that table. Been racking my brain, but just can't figure out where I've gone wrong. -.-

Any help would be greatly appreciated! :)

Code: Select all

$sql="SELECT * FROM royals";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

while($rows=mysql_fetch_array($result))
{

 $id[]=$rows['id'];
  echo "".$rows['nickname']." (id=".$rows['id'].") (potentiallength=".$rows['potentiallength'].") (length=".$rows['length'].") <br>";

      if ($descript3['lifestage'] < 3)//if age is under adolescent add 1/12th of 40% of the growth left in inches if fed well per month
      	{
		$potentiallength = $rows['potentiallength'];
          $growthleft = ($potentiallength-16);	
		  $growth = (($growthleft*.40)/12);
		  echo "(growth=".$growth. ")";
          
          for($i=0;$i<$count;$i++){
            $sql1="UPDATE royals SET length=length+1 WHERE id='$id[$i]'";
            $result1=mysql_query($sql1);
          }//end for
        }

Re: php loop update

Posted: Fri Dec 10, 2010 7:54 pm
by Chris
You're script is confusing me and I don't really understand what you're trying to do.

You're first looping through every row in the database?

Code: Select all

while($rows=mysql_fetch_array($result)) 
Then within that loop you are adding a new key to the $id array?

Code: Select all

$id[]=$rows['id']; 
And also looping through the number of rows in the database?

Code: Select all

for($i=0;$i<$count;$i++) 
which loops through all the rows in the database again and updates length+1?

Re: php loop update

Posted: Fri Dec 10, 2010 8:04 pm
by ConceptDestiny
I'm pretty crap at loops, still digging into the data to learn though. :D

I'm basically trying to fetch rows using the select statement, make a calculation with two fields within each row, then update the fields within those rows with the new value. :/


EDIT:

Fixed it. :D