Jump to content

Not echoing a variable if it exists in a MySQL table


denhamd2

Recommended Posts

Hi,

 

I have a variable $mydate which loops for all the days of a month (in a while loop), so therefore for April its:

01042007

02042007

03042007, etc...

 

I also have a MySQL table "bookings", which contains the field "date".

 

This field is populated with rows of dates, with the same format as those in $mydate (eg. 01042007)

 

Is there any way to not echo $mydate if it exists as one of the entries in the "date" field in the database?

 

Many thanks in advance

thanks, sounds like a good way of doing it. just wondering how I might be able to put the dates in the array? basically I know I want to select all the rows from the "date" field in the "bookings" table and insert them into the array but I haven't got a clue as for how I'd code this. Any help would be much appreciated...

$sql = "SELECT date FROM bookings";

$result = mysql_query($sql);

 

$dates_in_db = array();

while(list($date) = mysql_fetch_row($result)){

         $dates_in_db[] = $date;

}

 

if(!in_array($mydate,$dates_in_db)){

          echo "Print date";

}else{

 

}

doesn't seem to be working? here's my existing code:

 

//sets the first day of the month to 1
$day_num = 1;

//count up the days, untill we've done all of them in the month

while 
( $day_num <= $days_in_month 
) 
{ 
$myday_num = str_pad($day_num, 2, "0", STR_PAD_LEFT);




$mydate = $myday_num.$month.$year;



echo $mydate; 
$day_num++; 
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)


{
echo "</tr><tr>";
$day_count = 1;
}

} 

 

as you can see, $mydate is in a while loop, maybe putting your code in the while loop is what's knocking it out?

$mydate = $myday_num.$month.$year;

 

$sql = "SELECT COUNT(*) AS tot FROM booking WHERE date = '".$mydate."'";

$result = mysql_query($sql);

 

if(mysql_result($result,0,'tot') == 0){

          echo "Print date";

}else{

      ..................

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.