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
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");
}
}
?>

Link to comment
Share on other sites

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");
}

?>

 

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.