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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

$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{

 

}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

$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{

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

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.