Page 1 of 1

[Help] PHP While Loop Included In A Do-While Loop

Posted: Wed Jan 06, 2010 2:49 am
by dedou
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.

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

Code: Select all

<?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
1
2
3
4
5
6
7
8
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.

Re: [Help] PHP While Loop Included In A Do-While Loop

Posted: Wed Jan 06, 2010 3:45 am
by requinix
1. You set $num=1, then you increment, and then you use the value. The first time, $num will be 2.
2. Your outer loop is set to print the user number and print his/her $points. If you want it to print all user numbers and then all $points you'll need an array - holding onto the $points until you've reached all the users. But I don't think you need this.
3. Using a loop with user ID numbers is scary. What work do you need to do? Because if it's simple then you can run one SQL query to do everything.

Re: [Help] PHP While Loop Included In A Do-While Loop

Posted: Wed Jan 06, 2010 7:21 am
by dedou
tasairis wrote:1. You set $num=1, then you increment, and then you use the value. The first time, $num will be 2.
You are right, I corrected it. Check the current code:

Code: Select all

<?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 when I run the script is the following:

user id: 1 has a number of 100 points

This means that the do loop doesn't repeat itself for each user.
I'm suspecting that the while loop (for DB extraction) included in the do loop causes the problem.
tasairis wrote:2. Your outer loop is set to print the user number and print his/her $points. If you want it to print all user numbers and then all $points you'll need an array - holding onto the $points until you've reached all the users. But I don't think you need this.
I'm using a while loop and mysql_fetch_assoc array in order to prin ALL user numbers and points. Do you see anything wrong there?
tasairis wrote:3. Using a loop with user ID numbers is scary. What work do you need to do? Because if it's simple then you can run one SQL query to do everything.
Could you please explain me why is it scary?
I want to make an update in my DB.
Each user has several slots as you can see in the code. Those slots have values. Depending on the values of those slots the user will get points.

Thanks for your time. :wink:

Re: [Help] PHP While Loop Included In A Do-While Loop

Posted: Wed Jan 06, 2010 7:59 am
by dedou
OK I solved it. But I would love to listen to your answers in my last questions.

Re: [Help] PHP While Loop Included In A Do-While Loop

Posted: Wed Jan 06, 2010 8:11 am
by jason
The do() loop only runs once because of this:

Code: Select all

while ($name == "Alex");
I imagine the first name isn't Alex. This means that the do() will run, and then when it checks the while, it will finish. Remember, do() does something while something is true, but it always does it at least once.

As for what you are doing: I assume you'll be doing something inside the while() loop with all that data?

And the data, slot8, slot9, and bag2, bag3, etc. That's wrong. Instead of doing that, you should put the slots and bags into another table.

table_slots
  • slot_id
  • user_id
  • slot_contains
In this case, slot_id is the PK, user_id is the FK to the users table. slot_contains is whatever the users table had originally. While this might seem like a bit more work, it's actually a much more efficient way to handle this type of data. If you want to add more slots, you can easily do this. You'd want to create an Index on user_id to make querying efficient.

Furthermore, if you want to eventually add meta data to the slots, for example, something like 'disable','enable', you can do that easily with this table setup by adding one field. In your method, you'd have to add several more fields, or do something cumbersome with a SET.

Anyways, just some thoughts.

Re: [Help] PHP While Loop Included In A Do-While Loop

Posted: Thu Jan 07, 2010 3:45 am
by dedou
OK, I've extended the program a little bit more and now I'm facing new problems:

Inside the do-while loop, I compare a variable with some other variables with the If statement and the or thing (||).
If the statement is true, script should continue running. But it seems that the script coninues to run even if the IF statement (with the ||s) isn't true.

Here's the code:

Code: Select all

<?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'];
$name1   = $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: " .$num. "has a number of" .$points. "<br>";
 
            $matchid1 = 1; //1
            $matchid2 = 2; //x
            $matchid3 = 3; //2
            $matchid4 = 4; //1(MOTD)
            $matchid5 = 5; //2
            $matchid6 = 6; //x
            $matchid7 = 7; //2
            $matchid8 = 8; //1
            $matchid9 = 9; //x
            $matchid10 = 10; //1 (MOTD)
            $matchid11 = 11; //2
            $matchid12 = 12; //x(MOTD)
            $matchid13 = 13; //1
            $matchid14 = 14; //2 {MOTD)
            $matchid15 = 15; //2
            $motd = 22 / 10;
            $proper = 4 / 10;
            $action = 4 / 10;
 
            //Decrypt 1st slot
 
            if (@$slot1<16&&$slot1>0)
            {
            $slot1;
             If ($slot1=$matchid2||$slot1=$matchid4||$slot1=$matchid8||$slot1=$matchid10||$slot1=$matchid13)
             {
             echo"<b>" .$slot1. "</b>";
             $bonus1 = $bag1 * $action + $bag1;
             $whole1 = $points + $bonus1;
             mysql_query("UPDATE users SET points='$whole1' WHERE id='$num'");
             mysql_query("UPDATE users SET bag1='0' WHERE id='$num'");
             mysql_query("UPDATE users SET slot1='0' WHERE id='$num'");
             echo "Done";
 
             }
             else
             echo "Fail";
            }
 
$num++;
 
 
 
if (@$num>=14)
$name = "billy";
}
while ($name == "Alex");
 
?>
As you can see I'm comparing $slot1 with $matchid2, $matchid4, $matchid8, $matchid10 and $matchid13. You can see the values of the matchid variables in the code.

slot1 is a colum in the database. $slot1 is the value of the cell of the colum for each id.
As you can see, I echo out $slot1 for testing purposes.

When I set the $slot1 to 15 for user with id 3, I get the folowing result:

user id: 1has a number of100
user id: 2has a number of146
1Doneuser id: 3has a number of114
user id: 4has a number of160
user id: 5has a number of104
user id: 6has a number of103
user id: 7has a number of122
user id: 8has a number of100
user id: 9has a number of100
user id: 10has a number of100
user id: 11has a number of100
user id: 12has a number of100
user id: 13has a number of122

Focus on user id3:
1Doneuser id: 3has a number of114
The value of $slot1 is 1 even though I have set it 15 in my Database. 8O 8O
I don't know why it has changed. And as the value of $slot1 is 15, 'Done' should have not appeared because $slot1 was not equal to $matchid2 or $matchid4 or $matchid or $matchid8 or $matchid10 or $match13.

The script acted like I had never done those comparisons.
Really weird.

Thanks in avdance. :)

PS: This page is just for updating the database! I've written some echo functions for testing puproses only.