Forum Moderators: coopster

Message Too Old, No Replies

Variable in While Loop only Updating Once

Variable inside nested While Loop only sets once

         

JohnPorier

6:18 am on Jul 17, 2007 (gmt 0)

10+ Year Member



I am trying to pull a record with a MySQL statement and put it into a variable. I have it working, but it seems to only be working once. The process needs to happen over and over again because this code resides within a while statement that is producing a table with a lot of entries.

Some background: $useraccount is the currently logged in user. I basically want to see if a user logged in has said they are attending a particular event. Depending on this, I want to show different code with my if statement. This is only working for one entry, basically the value of $userpar, never changes so the if argument end up being the same and all events below this one show the same chunk of code.

Is there anyway to pull a record without a while statement? I think this might help. I want to see if a user has a record with a particular ID. If I can keep checking this against different ID's I would be able to do what I am trying to accomplish. Any help is appreciated.


$result3 = mysql_query("SELECT DISTINCT gp_event_user FROM gp_event_users WHERE gp_event_id='" . $row['gp_id'] . "' AND gp_event_user='$useraccount'");

while($check_row = mysql_fetch_array($result3))
{

$userpar = $check_row['gp_event_user'];

}

if ($userpar==$useraccount)

echo "<td class='events'><center><img src='stop.gif' onmousedown='ajaxRemovePar(" . $row['gp_id'] . ")' /></center></td>";

else

echo "<td class='events'><center><img src='go.gif' onmousedown='ajaxPar(" . $row['gp_id'] . ")' /></center></td>";

phparion

6:30 am on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you are missing one point only, put your IF within WHILE body, because your while loop ends and it leaves out the last value fetched in the userpar.

put the IF code in WHILE body so that it can check for each value fetched.

Habtom

7:02 am on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There seems to be a bit of a logic error there.

In the query, you are getting the distinct events of a user account. Your if condition will always be true, so I don't see the reason of having it. If you want to list the events by a certain user, you might need to select the events from the table. You seem to be doing what is already true in the sql statement again by the if and the loop.

Habtom

JohnPorier

12:45 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



I tried placing the if statement in the while body, this doesn't work, nothing displays in the column that normally has the graphics / JS.

As far as the logic... With

gp_event_id='" . $row['gp_id'] . "' 

In this portion of the code $row['gp_id'] is an array within the body of a parent while statement, all the code I posted is in an while loop. The code checks if a users name is in a record for each $row['gp_id'], which is different for each row.

The below is an outline, don't need to check syntax of this, I'm just trying to give an idea of whats going on.


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

--------------- ------------------ ---------------
¦....event ID....¦....event title....¦...participate?....¦
--------------- ------------------ ---------------
¦ $row['gp_id'] ¦ $row['gp_title'] ¦code in 1st post ¦
¦ $row['gp_id'] ¦ $row['gp_title'] ¦code in 1st post ¦


}

[edited by: JohnPorier at 12:51 pm (utc) on July 17, 2007]

Duskrider

2:38 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



If I understand what you're doing correctly, the logic error falls here:
AND gp_event_user='$useraccount'");
//skip code
if ($userpar==$useraccount)

If you're getting all the records from the database that are associated with this user wouldn't all the records you get meet the if condition? You're already filtering your results via SQL... no need to re-filter them with an if statement. Unless, of course, you're doing something we don't see here.

As for your original problem, the while statement you have as it is will never return more than one $userpar. The $userpar variable is overwritten with each loop of the while, and you're not doing anything with it before it gets overwritten. That's why the suggestion was made to put your if statement within the while... that way you're doing something with each new $userpar value before it is overwritten.

while($check_row = mysql_fetch_array($result3))
{
$userpar = $check_row['gp_event_user'];
// You need to do something with $userpar here.
}

If the query you've got is only supposed to return one result, you should do this:

$check_row = mysql_fetch_array($result3);
if(mysql_num_rows($check_row) < 1)
$userpar = '0'; // Or whatever you want for no participation
else
$userpar = $check_row['gp_event_user'];

Then your if statement will evaluate correctly (providing none of your accounts are number 0).