Help needed simplifying code.

I’m setting up a gig guide for a band. Now, i’ve got the code working EXACTLY as i want it, no dramas at all, but it is extremely long, and extremely messy!

If someone could point me in the right direction, i would love to learn how to do this the SHORT way! :D

As you will see, i need the months to be seperated by the month heading, and as i said before, this all works great, but pasting the same code 12 times just seems ridiculous, surely there has to be a quicker way

I hope this all makes sense.

Thanks in advance to anyone willing to help out.

Cheers,
Dave

[code] // select January shows in the database
$result = mysql_query(“select show_id, month, day1, day2, year, location, details, venue
from $database_table
where month = ‘1’
order by year, month, day2”,$db)
or die_now(“

Could not select shows

”);
   echo("<table class='guide'>n");
   echo("t<tr>ntt<td width='80'>date</td>ntt<td width='140'>venue</td>ntt<td width='100'>location</td>ntt<td width='200'>details</td>nt</tr>n");
   echo("</table>n");
   echo("<table class='guide'>n");
   echo("t<tr>ntt<td width='80'>January</td>nt</tr>n");
   echo("</table>n");

while($row = mysql_fetch_array($result)){
    $the_id = $row["show_id"];
    $the_month = $row["month"];
    $the_day1 = $row["day1"];
    $the_day2 = $row["day2"];
    $the_year = $row["year"];
    $the_location = $row["location"];
    $the_details = $row["details"];
    $the_venue = $row["venue"];


 // shows
    echo("<table class='guide'>n");
    echo("t<tr>ntt<td width='80'>$the_day1" . " - " . "$the_day2" . "/" . "$the_month" . "</td>n");
    echo("tt<td width='140'>" . "$the_venue" . "</td>n");
    echo("tt<td width='100'>" . "$the_location" . "</td>n");
    echo("tt<td width='200'>" . "$the_details" . "</td>n");
	echo("t</tr>n</table>n");

}

// select February shows in the database
$result = mysql_query(“select show_id, month, day1, day2, year, location, details, venue
from $database_table
where month = ‘2’
order by year, month, day2”,$db)
or die_now(“

Could not select shows

”);
   echo("<table class='guide'>n");
   echo("t<tr>ntt<td width='80'>February</td>nt</tr>n");
   echo("</table>n");

while($row = mysql_fetch_array($result)){
    $the_id = $row["show_id"];
    $the_month = $row["month"];
    $the_day1 = $row["day1"];
    $the_day2 = $row["day2"];
    $the_year = $row["year"];
    $the_location = $row["location"];
    $the_details = $row["details"];
    $the_venue = $row["venue"];


 // shows
    echo("<table class='guide'>n");
    echo("t<tr>ntt<td width='80'>$the_day1" . " - " . "$the_day2" . "/" . "$the_month" . "</td>n");
    echo("tt<td width='140'>" . "$the_venue" . "</td>n");
    echo("tt<td width='100'>" . "$the_location" . "</td>n");
    echo("tt<td width='200'>" . "$the_details" . "</td>n");
	echo("t</tr>n</table>n");

}
[/code]

Well, since you said just point you in the right direction and I don’t have much time right now, I’ll do just that and if you need some more help feel free to ask (I like to make people work for the answer ;)). What you want to use is a for loop and an array of the month names. In pseudo code you want something like this…

$months = array( "January", "February", ... );

for( $i = 0; $i < 12; $i++ ) {
    // do table and mysql  code
}

Let me know if that gets you headed in the right direction.

Rather than using an array with all the names for the months, perhaps this function could help you out:

http://nl.php.net/manual/en/function.date.php (use the ‘F’ character) ;)

Sponsor our Newsletter | Privacy Policy | Terms of Service