Jump to content

[SOLVED] Help needed simplifying code.


stormcloud

Recommended Posts

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

 

// 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("<h2>Could not select shows</h2>");

      echo("<table class='guide'>\n");
       echo("\t<tr>\n\t\t<td width='80'>date</td>\n\t\t<td width='140'>venue</td>\n\t\t<td width='100'>location</td>\n\t\t<td width='200'>details</td>\n\t</tr>\n");
      echo("</table>\n");
      echo("<table class='guide'>\n");
      echo("\t<tr>\n\t\t<td width='80'>January</td>\n\t</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>\n\t\t<td width='80'>$the_day1" . " - " . "$the_day2" . "/" . "$the_month" . "</td>\n");
        echo("\t\t<td width='140'>" . "$the_venue" . "</td>\n");
        echo("\t\t<td width='100'>" . "$the_location" . "</td>\n");
        echo("\t\t<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("<h2>Could not select shows</h2>");

      echo("<table class='guide'>\n");
      echo("\t<tr>\n\t\t<td width='80'>February</td>\n\t</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>\n\t\t<td width='80'>$the_day1" . " - " . "$the_day2" . "/" . "$the_month" . "</td>\n");
        echo("\t\t<td width='140'>" . "$the_venue" . "</td>\n");
        echo("\t\t<td width='100'>" . "$the_location" . "</td>\n");
        echo("\t\t<td width='200'>" . "$the_details" . "</td>\n");
      echo("\t</tr>\n</table>\n");
}

Link to comment
https://forums.phpfreaks.com/topic/86745-solved-help-needed-simplifying-code/
Share on other sites

try

<?php
// change $mnt from 1 to 12
for ($mnt = 1; $mnt <= 12; $mnt++){
$result = mysql_query("select show_id, month, day1, day2, year, location, details, venue
        from $database_table
        where month = '$mnt'
        order by year, month, day2",$db)
or die_now("<h2>Could not select shows</h2>");

echo("<table class='guide'>\n");
echo("\t<tr>\n\t\t<td width='80'>date</td>\n\t\t<td width='140'>venue</td>\n\t\t<td width='100'>location</td>\n\t\t<td width='200'>details</td>\n\t</tr>\n");
echo("</table>\n");
echo("<table class='guide'>\n");
echo("\t<tr>\n\t\t<td width='80'>".date('F', mktime(1,1,1,$mnt,1,2000))."</td>\n\t</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>\n\t\t<td width='80'>$the_day1" . " - " . "$the_day2" . "/" . "$the_month" . "</td>\n");
	echo("\t\t<td width='140'>" . "$the_venue" . "</td>\n");
	echo("\t\t<td width='100'>" . "$the_location" . "</td>\n");
	echo("\t\t<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("<h2>Could not select shows</h2>");

echo("<table class='guide'>\n");
echo("\t<tr>\n\t\t<td width='80'>February</td>\n\t</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>\n\t\t<td width='80'>$the_day1" . " - " . "$the_day2" . "/" . "$the_month" . "</td>\n");
	echo("\t\t<td width='140'>" . "$the_venue" . "</td>\n");
	echo("\t\t<td width='100'>" . "$the_location" . "</td>\n");
	echo("\t\t<td width='200'>" . "$the_details" . "</td>\n");
	echo("\t</tr>\n</table>\n");
}
}
?>

Take out the "where" clause but leave the "order by", then just check when the month has changed. When it's changed do whatever it is you need to do (i.e. close any tables and start new table or headings for next month's data).

 

Example:

<?php

$result = mysql_query("select show_id, month, day1, day2, year, location, details, venue
        from $database_table
        order by year, month, day2",$db)
        or die_now("<h2>Could not select shows</h2>");

$currentMonth = 0;
while($row = mysql_fetch_array($result)){

    if ($currentMonth != $row["month"]) {

       $currentMonth = $row["month"];

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

      $displayMonth = date("F", mktime(0, 0, 0, $currentMonth, 1, 2000));

      echo("\t<tr>\n\t\t<td width='80'>$displayMonth</td>\n\t</tr>\n");
      echo("</table>\n");

    }

    $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>\n\t\t<td width='80'>$the_day1" . " - " . "$the_day2" . "/" . "$the_month" . "</td>\n");
     echo("\t\t<td width='140'>" . "$the_venue" . "</td>\n");
     echo("\t\t<td width='100'>" . "$the_location" . "</td>\n");
     echo("\t\t<td width='200'>" . "$the_details" . "</td>\n");
     echo("\t</tr>\n</table>\n");
}

?>

 

There are many ways to do it, you could do something like:

<?php
// 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('<h2>Could not select shows</h2>');
echo "<table class='guide'>\n";
echo "\t<tr>\n\t\t<td width='80'>date</td>\n\t\t<td width='140'>venue</td>\n\t\t<td width='100'>location</td>\n\t\t<td width='200'>details</td>\n\t</tr>\n";
echo "</table>\n";
echo "<table class='guide'>\n";
echo "\t<tr>\n\t\t<td width='80'>January</td>\n\t</tr>\n";
echo "</table>\n";

while($row = mysql_fetch_array($result)) {
// shows
echo "<table class='guide'>\n";
echo "\t<tr>\n\t\t<td width='80'>{$row['day1']} - {$row['day2']}/{$row['month']}</td>\n";
echo "\t\t<td width='140'>{$row['venue']}</td>\n";
echo "\t\t<td width='100'>{$row['location']}</td>\n";
echo "\t\t<td width='200'>{$row['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('<h2>Could not select shows</h2>');
echo "<table class='guide'>\n";
echo "\t<tr>\n\t\t<td width='80'>February</td>\n\t</tr>\n";
echo "</table>\n";

while($row = mysql_fetch_array($result)){
// shows
echo "<table class='guide'>\n";
echo "\t<tr>\n\t\t<td width='80'>{$row['day1']} - {$row['day2']}/{$row['month']}</td>\n";
echo "\t\t<td width='140'>{$row['venue']}</td>\n";
echo "\t\t<td width='100'>{$row['location']}</td>\n";
echo "\t\t<td width='200'>{$row['details']}</td>\n";
echo "\t</tr>\n</table>\n";
}
?>

 

Personally I prefer to do it more like this:

<?php
// 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('<h2>Could not select shows</h2>');
echo '<table class="guide">
  <tr><td width="80">date</td><td width="140">venue</td><td width="100">location</td><td width="200">details</td></tr>
  </table>
  <table class="guide">
  <tr><td width="80">January</td></tr>
  </table>';

while($row = mysql_fetch_array($result)) {
// shows
echo '<table class="guide">
	  <tr><td width="80">'.$row['day1'].' - '.$row['day2'].'/'.$row['month'].'</td>
	  <td width="140">'.$row['venue'].'</td>
	  <td width="100">'.$row['location'].'</td>
	  <td width="200">'.$row['details'].'</td>
	  </tr></table>';
}

// 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('<h2>Could not select shows</h2>');
echo '<table class="guide">
  <tr><td width="80">February</td></tr>
  </table>';

while($row = mysql_fetch_array($result)){
// shows
echo '<table class="guide">
	  <tr><td width="80">'.$row['day1'].' - '.$row['day2'].'/'.$row['month'].'</td>
	  <td width="140">'.$row['venue'].'</td>
	  <td width="100">'.$row['location'].'</td>
	  <td width="200">'.$row['details'].'</td>
	  </tr></table>';
}
?>

Take out the "where" clause but leave the "order by", then just check when the month has changed. When it's changed do whatever it is you need to do (i.e. close any tables and start new table or headings for next month's data).

That is EXACTLY what i was after.

 

Now i just need to read through it and learn how the hell you did that!  ???

 

Thanks mate, you rule.

Dave

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.