kasitzboym Posted July 1, 2011 Share Posted July 1, 2011 Hello, I hope someone can help me out with this as i am out of ideas. I was trying to use today's date to display a message based on the information pulled from a MySQL database. As of right now i have it displaying absolutely everything in the database which is not what i want. Below is what i have so far to display at least something. <?php require("connect.php"); $sql="select * from yearly_anouncements"; $result=mysql_query($sql, $connect); $i=0; $month = date("m"); if($month = "07"){ while ($row=(mysql_fetch_array($result))) { $id=mysql_result($result,$i,"id"); $event=mysql_result($result,$i,"event"); $month=mysql_result($result,$i,"month"); $day=mysql_result($result,$i,"day"); $display=mysql_result($result,$i,"display"); echo "".$row['display'].""; $i++; } } ?> Right now i want it to pull in anything to do with the month of JULY which right now is just the phrase for INDEPENDENCE DAY! could anyone help me figure out where i am going wrong? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
sunfighter Posted July 1, 2011 Share Posted July 1, 2011 Go here to see how to use date function. Look here for an example <?php $today = date("F j, Y, g:i a"); echo $today; ?> And do your query like this $sql="select * from yearly_anouncements" WHERE day = 'your day here' AND month = 'your month here'; It depends on how you have saved the day, month. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 1, 2011 Share Posted July 1, 2011 how is the date stored in your database? timestamp: YYYY-MM-DD HH:MM:SS? seperate date and time? seperate day, month and year? post name of fields and format please. Quote Link to comment Share on other sites More sharing options...
kasitzboym Posted July 1, 2011 Author Share Posted July 1, 2011 While working hard at trying to figure this out, I DID! Just for anyone who wants future reference here is what I did. I had my table separated into id--INT---auto Increment--just a unique id number event--TEXT--- title of the event to be displayed month--INT--- month event happens in day--INT---- day event happens in display--TEXT--- actual text to display on the site I figured it would be easier to read out the individual cells instead of an actual formatted date because im not yet familiar with it. I then looped through all of the rows to put it into an array and while inside the While loop i created an if statement that compared the month of the browser to the month colume of the table in MySQL. I then echoed the text in the display cell in the table into the website. <?php require("connect.php"); $sql="select * from yearly_anouncements"; $result=mysql_query($sql, $connect); $i=0; $monthd = date("n"); // if($monthd = "$month"){ while ($row=(mysql_fetch_array($result))) { $id=mysql_result($result,$i,"id"); $event=mysql_result($result,$i,"event"); $month=mysql_result($result,$i,"month"); $day=mysql_result($result,$i,"day"); $display=mysql_result($result,$i,"display"); $i++; if($monthd == "$month"){ echo "".$row['display'].""; } } ?> Thanks for the help anyway everyone!! Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 1, 2011 Share Posted July 1, 2011 well... glad you got the result you wanted, but you're extracting EVERYTHING from the database and then only showing a few... waste of resources. <?php require("connect.php"); $currentMonth = date("n"); $q=mysql_query("select * from `yearly_anouncements` where `month` = '$currentMonth'", $connect); while ($r=mysql_fetch_assoc($rq)){ echo 'Event: '.$r['event'].' (Id: '.$r['id'].')'; echo '<br />Date: 2011-'.$r['month'].'-'.$r['day']; echo '<br /><b>'.$r['display'].'</b>'; } @mysql_close($connect); ?> Quote Link to comment 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.