PHP while loop included in a do-while loop

as title says

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1983 Views - Last Post: 11 January 2010 - 12:23 PM Rate Topic: -----

#1 dedou  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 07-September 09

PHP while loop included in a do-while loop

Post icon  Posted 05 January 2010 - 04:53 PM

Hello,

I have the following problem. I 'm trying to automatically update the records of a database by refreshing a page.But updating the records isn't that simple in my case because some calculations & algoriths are needed before.

Anyway I'm updating the records with a do-while loop (I don't know if that's the best way to do it. If it isn't please, enlight me). I do that with a do-while loop as i said, by increasing the id (of the user ofc) by 1 each time. For example it updates records for user with id 1, then it updates records for user with id 2 etc till it reaches a limit.

In the do-while statement though I include a while statement because I want to extract some data from the excisting records in the database. With this data I'm doing the required calculations for the update. I told ya, it is quite complicated. :P

Here's the code (it's just a test, not the actual update):
<?php

@$num = 1;
$name = "Alex";

do
{
$num++;
echo @$num. "<br>";

require("connect.php");
$extract = mysql_query("SELECT * FROM users WHERE id='$num'") or die("Couldn't dind user.");
$numrows = mysql_num_rows($extract);

while ($row = mysql_fetch_assoc($extract))
{
$id = $row['id'];
$name = $row['name'];
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
$points = $row['points'];
$date = $row['date'];
$slot1 = $row['slot1'];
$slot2 = $row['slot2'];
$slot3 = $row['slot3'];
$slot4 = $row['slot4'];
$slot5 = $row['slot5'];
$slot6 = $row['slot6'];
$slot7 = $row['slot7'];
$slot8 = $row['slot8'];
$slot9 = $row['slot9'];
$slot10 = $row['slot10'];
$bag1 = $row['bag1'];
$bag2 = $row['bag2'];
$bag3 = $row['bag3'];
$bag4 = $row['bag4'];
$bag5 = $row['bag5'];
$bag6 = $row['bag6'];
$bag7 = $row['bag7'];
$bag8 = $row['bag8'];
$bag9 = $row['bag9'];
$bag10 = $row['bag10'];
}

echo "user id: " .$id. " has a number of " .$points. " points";
			



if (@$num>=8)
$name = "billy";
}
while ($name == "Alex")
?>


When I'm running the script I get this:

2
user id: 2 has a number of 90 points

Instead of getting

user id: 1 has a number of 100 points
user id: 2 has a number of 90 points
user id: 3 has a number of 85 points
user id: 4 has a number of 64 points
user id: 5 has a number of 72 points
user id: 6 has a number of 50 points
user id: 7 has a number of 100 points
user id: 8 has a number of 96 points

as I have 8 users.

But User 2 has 90 points so the values are correct.

Thanks in advnace.

This post has been edited by dedou: 05 January 2010 - 04:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP while loop included in a do-while loop

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: PHP while loop included in a do-while loop

Posted 05 January 2010 - 08:00 PM

You need your echo statement inside the loop.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: PHP while loop included in a do-while loop

Posted 05 January 2010 - 08:03 PM

Quote

if (@$num>=8)
$name = "billy";
}
while ($name == "Alex")
?>


This is the end of your file, the while statement is ending the initial "do" command. You are missing the semi-colon that indicates the end of the line.

Your lack of indenting the code makes reading this rather difficult. If you cleaned it up, you may see what you are missing, as it's easier to read, & follow the logic when it's clean.
Was This Post Helpful? 0
  • +
  • -

#4 dedou  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 07-September 09

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 01:07 AM

View PostBenignDesign, on 5 Jan, 2010 - 07:00 PM, said:

You need your echo statement inside the loop.


Actually it's already inside the do loop.
Check this echo:
echo "user id: " .$id. " has a number of " .$points. " points";

It's written before the do loop ends.

I did not understand you therefore. Sorry. :)


View Postno2pencil, on 5 Jan, 2010 - 07:03 PM, said:

Quote

if (@$num>=8)
$name = "billy";
}
while ($name == "Alex")
?>


This is the end of your file, the while statement is ending the initial "do" command. You are missing the semi-colon that indicates the end of the line.

Your lack of indenting the code makes reading this rather difficult. If you cleaned it up, you may see what you are missing, as it's easier to read, & follow the logic when it's clean.


I put the semi-colon at the end of the last while statement, but after running the script the results were excactly the same. I don't think it is needed because there is no code after the last while loop. :)

Anyway thanks for your answers. I still encounter this problem. I'd appreciate any help. ;)

This post has been edited by dedou: 06 January 2010 - 01:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 02:18 AM

@$num = 1; //here you set $num to 1
$name = "Alex";

do
{
$num++; //this sets it to 2
echo @$num. "<br>"; //this outputs 2, and a HTML linebreak



$num is not set to 1, so the record for 1 is not outputted. Increments happen at the end.
Was This Post Helpful? 0
  • +
  • -

#6 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 04:31 AM

Edit: Nevermind. I need to pay more attention to what I'm reading.

This post has been edited by BenignDesign: 06 January 2010 - 04:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 dedou  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 07-September 09

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 05:33 AM

View Postrelic5.2, on 6 Jan, 2010 - 01:18 AM, said:

@$num = 1; //here you set $num to 1
$name = "Alex";

do
{
$num++; //this sets it to 2
echo @$num. "<br>"; //this outputs 2, and a HTML linebreak



$num is not set to 1, so the record for 1 is not outputted. Increments happen at the end.


Lol, have you read the code I posted? I've written all this you posted, in my 1st post (check the code frame). I set the $num=1 before the loop starts, then do loop starts and afterwards $num++ increases the variable Inside the do-loop. It's exactly what I've done.

Thanks for your post anyway.
As I previosuly mentioned, I'd appreciate further help. :)

This post has been edited by dedou: 06 January 2010 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 05:45 AM

I did read the code you posted, and I hoped that by explaining it line by line you'd see what was wrong with it.

remove

Quote

echo @$num. "<br>";

move

Quote

$num++;
to before your if statement.
[quote]echo "user id: " .$id. " has a number of " .$points. " points";

Quote

add in a newline(\n) and linebreak (<br />)

change the if to if(@$num > 8) since you have eight users.
Was This Post Helpful? 0
  • +
  • -

#9 dedou  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 07-September 09

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 06:13 AM

hmm, made some changes:
<?php

@$num = 1;
$name = "Alex";

do
{
require("connect.php");
$extract = mysql_query("SELECT * FROM users WHERE id='$num'") or die("Couldn't dind user.");
$numrows = mysql_num_rows($extract);

while ($row = mysql_fetch_assoc($extract))
{
$id = $row['id'];
$name = $row['name'];
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
$points = $row['points'];
$date = $row['date'];
$slot1 = $row['slot1'];
$slot2 = $row['slot2'];
$slot3 = $row['slot3'];
$slot4 = $row['slot4'];
$slot5 = $row['slot5'];
$slot6 = $row['slot6'];
$slot7 = $row['slot7'];
$slot8 = $row['slot8'];
$slot9 = $row['slot9'];
$slot10 = $row['slot10'];
$bag1 = $row['bag1'];
$bag2 = $row['bag2'];
$bag3 = $row['bag3'];
$bag4 = $row['bag4'];
$bag5 = $row['bag5'];
$bag6 = $row['bag6'];
$bag7 = $row['bag7'];
$bag8 = $row['bag8'];
$bag9 = $row['bag9'];
$bag10 = $row['bag10'];
}
echo "user id: " .$id. " has a number of " .$points. " points<br>";
$num++;

if (@$num<=13)
$name = "billy";
}
while ($name == "Alex");

?>


What I get now is

user id: 1 has a number of 100 points

The do loop isn't repeating itself for an unknown reason. I'm suspecting that the while loop (for DB extraction) included in the do loop causes the problem.
Was This Post Helpful? 0
  • +
  • -

#10 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 06:24 AM

A do...while loop will execute once, even if the condition is not met. At the end of the loop, you set the name to billy if it less than/equal to 13. i presume you meant to type more than/equal to 13.
Was This Post Helpful? 0
  • +
  • -

#11 dedou  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 07-September 09

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 06:31 AM

View Postrelic5.2, on 6 Jan, 2010 - 05:24 AM, said:

A do...while loop will execute once, even if the condition is not met. At the end of the loop, you set the name to billy if it less than/equal to 13. i presume you meant to type more than/equal to 13.

Thanks
True, I changed it.
But I get the same result.
Was This Post Helpful? 0
  • +
  • -

#12 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 06:32 AM

Post your updated code, I changed it and i'm getting it to output 12 results
Was This Post Helpful? 0
  • +
  • -

#13 dedou  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 07-September 09

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 06:35 AM

View Postrelic5.2, on 6 Jan, 2010 - 05:32 AM, said:

Post your updated code, I changed it and i'm getting it to output 12 results


<?php

@$num = 1;
$name = "Alex";

do
{
require("connect.php");
$extract = mysql_query("SELECT * FROM users WHERE id='$num'") or die("Couldn't dind user.");
$numrows = mysql_num_rows($extract);

while ($row = mysql_fetch_assoc($extract))
{
$id = $row['id'];
$name = $row['name'];
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
$points = $row['points'];
$date = $row['date'];
$slot1 = $row['slot1'];
$slot2 = $row['slot2'];
$slot3 = $row['slot3'];
$slot4 = $row['slot4'];
$slot5 = $row['slot5'];
$slot6 = $row['slot6'];
$slot7 = $row['slot7'];
$slot8 = $row['slot8'];
$slot9 = $row['slot9'];
$slot10 = $row['slot10'];
$bag1 = $row['bag1'];
$bag2 = $row['bag2'];
$bag3 = $row['bag3'];
$bag4 = $row['bag4'];
$bag5 = $row['bag5'];
$bag6 = $row['bag6'];
$bag7 = $row['bag7'];
$bag8 = $row['bag8'];
$bag9 = $row['bag9'];
$bag10 = $row['bag10'];
echo "user id: " .$id. " has a number of " .$points. " points<br>";
$num++;
}


if (@$num>=13)
$name = "billy";
}
while ($name == "Alex");

?>

Was This Post Helpful? 0
  • +
  • -

#14 relic5.2  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 50
  • Joined: 26-November 08

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 06:53 AM

You need to change: $name = $row['name']; to something not $name, it's breaking the loop.

If the variable $name = "Alex";{ and at the $name = "billy"} isn't important, you do:

$doWhile = true;
do {
... //insert loop until here
if($num >= 13) $doWhile = false;
} while($doWhile);


This post has been edited by relic5.2: 06 January 2010 - 06:56 AM

Was This Post Helpful? 1
  • +
  • -

#15 dedou  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 07-September 09

Re: PHP while loop included in a do-while loop

Posted 06 January 2010 - 06:57 AM

View Postrelic5.2, on 6 Jan, 2010 - 05:53 AM, said:

You need to change: $name = $row['name']; to something not $name, it's breaking the loop.


THANKS THANKS THANKS. SOLVED! :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2