denhamd2 Posted April 6, 2007 Share Posted April 6, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/ Share on other sites More sharing options...
jitesh Posted April 6, 2007 Share Posted April 6, 2007 Load all dates saved in datbase to array like this $dates_of_db = ("01022007","02022007",................); if(!in_array($mydate,$dates_of_db)){ echo "Print date"; }else{ } Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222847 Share on other sites More sharing options...
denhamd2 Posted April 6, 2007 Author Share Posted April 6, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222850 Share on other sites More sharing options...
jitesh Posted April 6, 2007 Share Posted April 6, 2007 $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{ } Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222852 Share on other sites More sharing options...
denhamd2 Posted April 6, 2007 Author Share Posted April 6, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222857 Share on other sites More sharing options...
jitesh Posted April 6, 2007 Share Posted April 6, 2007 What is the problem ? Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222858 Share on other sites More sharing options...
denhamd2 Posted April 6, 2007 Author Share Posted April 6, 2007 please see my updated post above. as you can see its echoing out all the dates in the month as $mydate, but basically I'm looking for it not to echo if that date exists as a row "date" in the table "bookings" Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222859 Share on other sites More sharing options...
jitesh Posted April 6, 2007 Share Posted April 6, 2007 $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{ .................. } Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222862 Share on other sites More sharing options...
denhamd2 Posted April 6, 2007 Author Share Posted April 6, 2007 thanks mate finally quick question, I have the date in the format 01012007 stored in the variable $mydate is there any way I can echo this in the format Monday, 1st January 2007? Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222865 Share on other sites More sharing options...
jitesh Posted April 6, 2007 Share Posted April 6, 2007 using mkdir get timestamp for this 01012007 Then use date function to change date format date(" define type",mkdir( .. .. . )); Quote Link to comment https://forums.phpfreaks.com/topic/45876-not-echoing-a-variable-if-it-exists-in-a-mysql-table/#findComment-222866 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.