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.

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.

 

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

 

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

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.