Jump to content

HELP using PHP to display MYSQL database based on date


kasitzboym

Recommended Posts

Hello, I hope someone can help me out with this as i am out of ideas.  :confused: 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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);
?>

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.