Page 1 of 1

[SOLVED]While Loop Ending Early

Posted: Sat Aug 22, 2009 6:11 am
by vrslogicunit
Hi there,

I'm using two while loops nested, and have a problem. The parent while ends after the nested while loop returns false. However I'd like it to keep looping through the mysql table rows. Any ideas?

Code: Select all

 
    $sql = mysql_query("SELECT * FROM db.bookings WHERE id != '17' ORDER BY id DESC");
    while($lastavail = mysql_fetch_array($sql))
    {
 ...nested while loop here...
    }
 
Thanks

Re: While Loop Ending Early

Posted: Sat Aug 22, 2009 7:35 am
by vrslogicunit
Here's the rest of the code...probably should've done this in the first post :)

Code: Select all

 
<?php
//Connect to the db
$con = mysql_connect("server","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("dbname", $con);
 
//Get todays date
$date = date(Ymd);
 
//Remove the day so it checks only for bookings this month
$datenoday = substr($date,0,6);    // Gives you just the day (numerical) from the date
 
//Get the bookings info from the db
$sql = mysql_query("SELECT * FROM bookings WHERE id != '17' ORDER BY id DESC");
while($lastavail = mysql_fetch_array($sql))
{
 
    //Check the date in booking db is this month
    $from = $lastavail['fromt'];
    $to = $lastavail['tot'];
 
    //Reduce the from and to dates to just the days
    $fromnoday = substr($from,0,6);
    $tonoday = substr($to,0,6);
    
    //If the month and year match, then go ahead.
    if ($fromnoday == $datenoday)
        {
    
        // If the booking is this month get the day of the booking and id of the property
        $fromonlyday = substr($from, -2);
        $toonlyday = substr($to, -2);
            
            //Increment the 'from' to the 'to' day and insert 0 into those days in the last minute db
            while ($fromonlyday != $toonlyday+1)
                {
                $lastavailid = $lastavail['p_id'];
                
                print ('<hr />');
                print ('Property id:');
                print $lastavail['p_id'];
                print ('<br />Booking id:');
                print $lastavail['id'];
                print (' <br />Guest:');
                print $lastavail['guest'];
                print (' <br />Day:');
                print $fromonlyday;
                
                $fromdrate = "d".$fromonlyday."";// Appends 'd' to the beginning to give the last minute database value name
                $sql = "UPDATE lastminute SET $fromdrate='0' WHERE prid=$lastavailid";
                $q = @mysql_query($sql);
                if(!$q) 
                {
                print('<br/>Data updating was not successful! Please try again!<br /><hr />');
                }
                else
                {
                print('<br />Update is completed.<br /><hr />');
                }
                
                //Increment day
                $fromonlyday++;
                
                
                }
        
        }
    else
        {
        print ('<br />Booking id:');
        print $lastavail['id'];
        print ' - this booking doesn\'t match this month and year';
        }
 
    
    }
print '<br />Finished checking';
mysql_close($con);
?>
 
 
 

Re: While Loop Ending Early

Posted: Sat Aug 22, 2009 8:57 am
by vrslogicunit
No prizes for you lot then...

i'd used $sql twice for different things.