php loop update

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

php loop update

Post 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
        }
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: php loop update

Post 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?
Fighting for peace is declaring war on war. If you want peace be peaceful.
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: php loop update

Post 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
Post Reply

Return to “Advanced Help and Support”