Need help with structure

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Need help with structure

Post by John Cartwright »

Code: Select all

<?php
<?
include("mysql.php"); 
include("header.php"); 

echo "<div align="center">"
	."<form action="{$_SERVER['PHP_SELF']}"\n"
	."<p>Type your Date here: eg. 2004-01-31 (y-m-d)<br/>\n" 
	."<input type="text" name="date" cols="40"><br>\n"
	."<p>Is this Upcoming or Past eg. Upcoming OR Past<br/>\n" 
	."<input type="text" name="uppast" cols="40"><br>\n"	
	."<p>Please enter the filename of the flyer eg. flyer24.jpg<br/>\n" 
	."<input type="text" name="pic" cols="40"><br>\n"
	."<p>Type your Description here here:<br/>\n" 
	."<textarea name="desc" rows="10" cols="40" wrap></textarea><br/>\n" 
	."<input type="submit" name="submitevents" value="SUBMIT" /></p>\n" 
	."</form>\n"
	."</div>";
echo "<div align="center"><strong>NOTE - </strong>To go to the next line type in < br > (without the spaces between < ) </div><br><br>";

//adding to the database
if ($submitevents == "SUBMIT") { 
  $sql = "INSERT INTO events SET date='$date', `desc`='$desc', `location`='$location', `pic`='$pic'";
if (@mysql_query($sql)) { 
    echo("<div align="center">Your Event has been added.<br></div>"); 
  } else { 
    echo ("<div align="center">Error adding submitted Event: " . mysql_error() . "</div>"); 
  } 
}
//deleting from the database
if (isset($deleteevents)) { 
      $sql = "DELETE FROM events WHERE id=$deleteevent"; 
      if (@mysql_query($sql)) { 
        	echo("<div align="center">The event has been deleted.</div>"); 
      }else{ 
        echo("<p>Error deleting event: " . mysql_error() . "</p>"); 
  } 
} 
//preparing data from database
$result = @mysql_query("SELECT * FROM events ORDER BY `date` ");

while ($row = mysql_fetch_array($result)) { 
   
	$newdate = explode("-",$row["date"]); 
	$month = $newdate[1]; // eg. February 
    $day = $newdate[2]; // eg. 04 
    $year = $newdate[0]; // eg. 2004 
    $id =  $row["id"];
	$desc = $row["desc"];
	$pic = $row["pic"];
	$date = $row["date"];
	
   if ($month == "01") {
   		$month2="January"; 
   }elseif ($month == "02") { 
   		$month2="Febuary"; 
   }elseif ($month == "03") { 
   		$month2="March"; 
   }elseif ($month == "04") { 
   		$month2="April"; 		
   }elseif ($month == "05") { 
   		$month2="May"; 		
   }elseif ($month == "06") { 
   		$month2="June"; 
   }elseif ($month == "07") { 
   		$month2="July"; 
   }elseif ($month == "08") { 
   		$month2="August"; 
   }elseif ($month == "09") { 
   		$month2="September"; 
   }elseif ($month == "10") { 
   		$month2="October"; 
   }elseif ($month == "11") { 
   		$month2="November"; 
   }else{ 
   		$month2="December"; 
   }

echo "<table width="668" border="0" cellpadding="0" cellspacing="0">\n"
    ."<tr>\n"
    ."<td width="22%" height="20">Date</td>\n"
    ."<td width="26%">Description</td>\n"
    ."<td width="32%">Location</td>\n"
    ."<td width="20%">Flyer</td>\n"
    ."</tr>\n"
	."<tr>\n"
    ."<td width="22%" height="20">".$month2."-".$year."</td>\n"
    ."<td width="26%">".$desc."</td>\n"
    ."<td width="32%">".$location."</td>\n"
    ."<td width="20%">\n";
	
	if ($pic==""){	
		echo "<img src="http://www.*********.com/events_files/images/flyer_not.jpg" width="100" height="58">\n";
	}else{
		echo "<img src="http://www.*********.com/events_files/images/$pic.jpg" width="100" height="58">\n";
		}

echo "</td>\n"
     ."</tr>\n"
     ."</table>\n"
	 ."<br>\n";
} 

?>
?>
Okay this section is an events page.. it basically looks like this

UPCOMING EVENTS

EVENT 1
DATE - DESCRIPTION - LOCATION - FLYER

EVENT 2
DATE - DESCRIPTION - LOCATION - FLYER

PAST EVENTS

EVENT 1
DATE - DESCRIPTION - LOCATION - FLYER

EVENT 2
DATE - DESCRIPTION - LOCATION - FLYER


My problem is not with the coding, it's figuring out how to set this all up. I have the events outputting properly, but I can't figure out how to make the past and upcoming events seperate while keeping them in order of date.

I was thinking about doing it like this..

in my loop, adding something like

Code: Select all

<?php

$current_date = date('Y-m-d');
if ($current_date > $date) {

echo "Current date";

}else{

echo "Past date";

?>
those being headers, but the problem is taht they will appear infront of every single month, i just want it like I displayed in my little drawing....

*lol* I just thought of a possiblity... I'll post my results soon.. but please post your ideas.. they are most likely better than mine :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It was a failure... anyhelp got ideas?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You're on the right track.

Create another variable called $shownDateHeader or something. Initialize it to false. When you loop through the dates, do the check you are performing now. Also see if $shownDateHeader is false. If it's false, show the header and change the value to true.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

But once it is set True, it will go through the loop, show it, go through the loop, show it again.. etc....... am I correct?

I only want Upcoming and Past Events to be displayed each once
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

No, depending on your conditions it will only display once.

Code: Select all

$showCurrentHeader = true; // I've reversed my thought process
$showPastHeader = true; // I've reversed my thought process
$current_date = date('Y-m-d'); 

if ($current_date < $date && $showCurrentHeader) { 
     echo "Upcoming Events"; // Will only display once.
     $showCurrentHeader = false;
} else if ($current_date > $date && $showPastHeader) { 
     echo "Past Events"; // Will only display once.
     $showPastHeader = false;
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

THat works like a charm

but for upcoming events, is it possible to get them to go from

closest date to furthest day

ie right now its like this

March

January

I want it like

January

March

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


I changed the sql query from ascending to decending but that switch the past and upcoming events around :s
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You might need to do two queries rather than just one.

One gets the upcoming events, ordered as you want.

The other gets the past events, ordered as you want.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

That's what I want, this is fine as it is tho :D

Thanks a lot for your help, as usual. TY :D
Post Reply