1

I've created a PHP script which is for an online radio station:

<?php
$username = "root";
$password = "PASSWORD";
$hostname = "localhost";
$database = "myradio1";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select $database");
$query= 'SELECT * FROM showmon';
$result = mysql_query($query) or die ('Error in query');
echo '<table width=100% border=1>';
while ($row=mysql_fetch_row($result))
{
  echo '<tr>';
  echo '<td><img src="\ '.$row['image'].' \"></td>';
  echo '<td>'.$row['presenter'].'</td>';
  echo '<td>'.$row['showinfo'].'</td>';
  echo '</tr>';
}
echo '</table>';
mysql_free_result($result);
mysql_close($dbhandle);
?>

I have several other pages, namely 1, 3, 4, 5, 6, 7.php for the rest of the days of the week but is it better to change the script so it looks at the date and pre-selects data from the tables which are:

showmon

showtue

showwed

showthu

showfri

showsat

showsun

All have the same table format as in the script above; the data's different.

It displays the data well, no issues there; but how do I make dynamic pages which will revert back to pre-set data if the database hasn't been changed etc.

http://www.capitalfm.com/northeast/on-air/schedule/ is the model I worked from; as an example http: capitalfm.com/northeast/on-air/schedule/?date=2012-02-08 is a dynamic page which reverts to pre-set data in a database as far as I know - AFAIK, that's how most radio sites work.

(//www. removed since I can't post links as new user)

I did look up on Google, but haven't found much for beginners on this.

http: www.capitalfm.com/northeast/on-air/schedule/?date=2012-02-08 (well, in URL terms) is what I'm trying to achieve.

My site's domain is http: radio.localhost/scheduleX (php extension hidden by mod_rewrite) with X representing number, but how could I improve this script better?

Any advice is appreciated; I'm fairly new to dynamic PHP, can do the basics of PHP and MySQL, and GET/POST for forms, but that's about it.

If anyone could help me I'd much appreciate it.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    What exactly are you asking? It seems unclear to me whether you're looking for a specific problem or just general advice. If the latter, that's not really what SO does. Try narrowing down the question to a specific area and you're likely to get more help. – Hecksa Feb 07 '12 at 13:52
  • i strongly feel you should first start learning the basic of PHP below are the few links to get you started [PHP Tutorial - w3schools](http://www.w3schools.com/php/default.asp) [php-101-php-for-the-absolute-beginner](http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/) – Ibrahim Azhar Armar Feb 07 '12 at 13:51

5 Answers5

0

You can get the date from ?date=2012-02-08 using $_GET:

echo $_GET['date'];

To use this in your query:

$query= "SELECT * FROM showmon WHERE date = '".mysql_real_escape_string($_GET['date'])."'";

You should read a php tutorial to get started.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
0

IF you just need to look at the date and select the appropriate field, then:

$day = date("l");
$field = NULL;

switch($day) {
    case "Monday":
        $field = "showmon";
        break;
    case "Tuesday":
        $field = "showtue";
        break;
    case "Wednesday":
        $field = "showwed";
        break;
    case "Thursday":
        $field = "showthu";
        break;
    case "Friday":
        $field = "showfri";
        break;
    case "Saturday":
        $field = "showsat";
        break;
    case "Sunday":
        $field = "showsun";
        break;
}

$query= 'SELECT * FROM '.$field;
... // and so on
jn1kk
  • 5,012
  • 2
  • 45
  • 72
0

Although I agree with the other comments about needing to look at some PHP basics in order to clean up that code, here is an example that creates the query you want.

$query = 'SELECT * FROM ' . mysql_real_escape('show' . strtolower(date('D')));

It takes date('D'), which is Mon, Tue, Wed and so on. It lower cases it and joins it to the word 'show', i.e. showmon.

If you want to use a user, supplied date, you can add that into the date method:

date('D', mktime(0, 0, 0, 2, 8, 2012))

The last three numbers in mktime are month, day, year.

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

The greatest thing to do is to surf this site, there are tons of question similar to yours.

but here is a tip it's not that efficient, and i'm not telling its a best practice, but its worth trying, create a mainpage for example

    mainpage.php

inside it you can call all your header,footer,body

    <?php include('path/header.php');?>
    <?php include('path/dynamic_body.php');?>
    <?php include('path/footer.php');?>

everytime you have a page your header and footer is the same, only the body changes. its up to you what datas you want to pass.

But its not a standard practice, if you would like one, I advise you to read the basics and practice frameworking.

tomexsans
  • 4,454
  • 4
  • 33
  • 49
0

Your table setup is wrong.
It should be just one table. With the field to hold a date in the 2012-02-10 format.

Now you can make your query like this

if (isset($_GET['date'])){
   $date = mysql_real_escape_sring($_GET['date']);
} else {
   $date = date("Y-m-d");
}
$query = 'SELECT * FROM `show` WHERE `date`='$date'";

it will select the content depends on the selected date or today date

To create a list of links you will need a code like this (not tested):

$monday = strtotime("monday"); // getting monday unix timestamp
for ($i=0,$i<7,$i++){
  $stamp = $monday + $i * 86400;
  $date  = date("Y-m-d",$stamp);
  $day   = date("D",$stamp);
  echo "<a href='?date=$date'>$day</a> ";
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345