Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One php code for all seasons? 1

Status
Not open for further replies.

kupe

Technical User
Sep 23, 2002
376
Click on the leftnav for whichever month’s news you want on the website. The php coding for each of the 12 pages is almost identical.

The only change is the heading - ‘Our news for January’ or ‘Our news for April’ etc – and of course the name of the table, JanNews, FebNews, MarchNews etc.

Is there a way, please, for just the one page to handle the news for all of the year, for all of each of those 12 months?

So whichever month is chosen, the code automatically changes the code in those two or three places - for whichever month's news is selected?
 
Of course there is, that's the beauty of PHP. Why write 12 scripts when one can do it all???

Here's a quick & dirty script...
Code:
<?
for ($i=1;$i<13;$i++){
   $mnt = date('F',strtotime($i.'/1/2005')); 
   $month_array[$mnt] = $mnt . 'News'; }

?>
<html>
<head></head>
<body>
<div class=lhcol>
<?
// set up left hand nav area
foreach ($month_array as $k=>$v) {
   echo '<a href=" . $_SERVER['PHP_SELF'] . '?mnt='.$k.'">News for '.$k.'>'."<br>\n"; }
?>
</div>
<div class=mainarea>
<?
if (isset($_GET['mnt'])) {
    echo '<h1>News for '. $_GET['mnt'] . "</h1>\n";
    $q = 'select * from '. $month_array[$_GET['mnt']];
    $rs = @mysql_query($q);
    while ($rw = mysql_fetch_assoc($rs)) {
// I assumed your new was stored in a MySQL db
    }
}
?>
</body>
</html>
This is a very simple code sample. No error checking (or not much). Expand as necessary.

Ken
 
Thanks very much, Ken. I'll study it over the weekend. Very much obliged to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top