Jump to content

[SOLVED] Printing a heading above info from a SELECT statment.


Siggles

Recommended Posts

Hi, I have a web page that takes gigs from a MySQL dbase and using a SELECT statement and $row prints them to a page in date order. I would like to know if it is possible to print the month above dates in December then January and so forth. For example...

 

December - heading

 

The Jazz Club

1st December 2007

 

January

 

The Jazz Club

1st January 2008

 

etc

 

the code I have so far is...

 

$result = mysql_query("SELECT *

FROM Gigs

WHERE date(Eventdate) >= CURRENT_DATE()

ORDER BY Eventdate ASC");

while($row = mysql_fetch_array($result))

  {

echo "<table width=\"100%\" >";

echo "<tr >

<td><strong><span class=\"main_text\">".date('l jS F Y',strtotime($row['Eventdate']))."</span></strong></td>

</tr>";

echo "<tr >

<td><span class=\"main_text\">".$row['Venue']."</span></td>

</tr>";

echo "<tr >

<td><span class=\"main_text\">".$row['Address1']."</span></td>

</tr>";

echo "<tr >

<td><span class=\"main_text\">".$row['Address2']."</span></td>

</tr>";

 

and so forth, which works fine.

 

I thought of an IF statement in their somewhere. Maybe  if ($row[eventdate']) ==  but not sure how to write it or include it.

 

Can you help (again)?

 

Thank you

Link to comment
Share on other sites

I found a solution from this site :-)

 

http://www.phpfreaks.com/forums/index.php/topic,88379.0.html

 

I re-wrote the code a bit to match the PHP from that page. Great stuff. This site is al ife saver.

 

$today = date("Y-m-d");

$result = mysql_query("SELECT * FROM Gigs WHERE Eventdate >= '$today' ORDER BY Eventdate ASC");

$month=1;

    while($myrow = mysql_fetch_assoc($result))

    {

    $date = $myrow['Eventdate'];

    list($year, $month, $day) = split("-", $date);

    $monthname=date('M', mktime(0, 0, 0, $month, $day, $year));

    $date = date('d', mktime(0, 0, 0, $month, $day, $year));

    if ($year != $lastyear) {

        echo("<B>Year $year</B><Br>");

    }

    if ($month != $lastmonth) {

        echo("<span class=\"main_text\"><B>$monthname</B></span><Br>");

    }

echo "<table width=\"100%\" >";

echo "<tr >

<td><strong><span class=\"main_text\">".date('l jS F Y',strtotime($myrow['Eventdate']))."</span></strong></td>

</tr>";

echo "<tr >

<td><span class=\"main_text\">".$myrow['Venue']."</span></td>

</tr>";

echo "<tr >

<td><span class=\"main_text\">".$myrow['Address1']."</span></td>

</tr>";

echo "<tr >

<td><span class=\"main_text\">".$myrow['Address2']."</span></td>

</tr>";

 

if (!empty($myrow['Address3'])) {

echo "<tr >

<td><span class=\"main_text\">".$myrow['Address3']."</span></td>

</tr>";

}

 

echo "<tr >

<td><span class=\"main_text\">".$myrow['Postcode']."</span></td>

</tr>";

echo "<tr >

<td><span class=\"main_text\">".$myrow['Phone']."</span></td>

</tr>";

 

if (!empty($myrow['url'])) {

echo "<tr >

<td><span class=\"main_text\"><a href=".$myrow['url']." target=\"blank\">More Info</a></span></td>

</tr>";

}

 

echo "</table><br>";

$lastmonth=$month;

    $lastyear=$year;

}

 

 

echo "<br>";

echo "<strong><span class=\"main_text\"><a href=\"http://forthcominggigs.php\">Click here</a> for a full listing.</span></strong>";

echo "<br><br>";

mysql_close($con);

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.