PHP: & ldquo; while & rdquo; loop returns the same value for each iteration of the variable

advertisements

Thanks in advance for any help offered.

I'm performing a query that returns something like this:

route   |   busnumber
  2            300
  4            123
  2            455
 12            934

My goal is to print/echo a list of all "busnumber" were "route" = 2. I know I can do a query on the DB to do this but I don't want to run this query for each and every route. The result set is actually part of one master, complex query so I need to accomplish my goal using the array.

The query works correctly and does display the appropriate info. I also return the results into an array. i.e ...

    $query=("SELECT * from foo")
    $result=mysql_fetch_assoc($query);

My current code looks like this (note my comments):

    mysql_data_seek( $result,0);  // because I'm previously iterating through $result
while ($i2=mysql_fetch_assoc($result)) {
$busnumber=$i2['busnumber'];
$busroute=$i2['route'];
echo "<div class='businfo'>";
if ($busroute='2'){           // I only want this to happen if $busroute=2
echo "Current Route = $busroute</br>";
echo "Bus Number : $busnumber </br>";
}
echo "</div>";
}

What I'm getting, however, it is echoing the same '$busroute' (2) but different '$busnumber' for each row as such:

Current Route = 2
Bus Number : 194
Current Route = 2
Bus Number : 196
Current Route = 2
Bus Number : 2002
Current Route = 2
Bus Number : 2010
Current Route = 2
Bus Number : 2013

The problem is that all of those bus numbers are not part of route 2. It is a listing of bus numbers from all routes. I only want it to perform the foreach loop if $routenumber=2.

Thanks again for the help :)


if ($busroute='2'){

The problem is that you're not checking whether it's equal to 2 here, you're assigning it to equal 2. Change it to this and you'll be fine:

if ($busroute=='2'){