Jump to content

Recommended Posts

Hey guys, I have a MySQL database comprised of high school alumni members and I'm trying to display the information on a page broken down by one particular field (year) and having it repeat by year down the page.  I would like the page to look something like this:

 

CLASS of 1936

 

John Smith

Karen Smith

Mike Thomas

Jane Warner

 

CLASS of 1940

 

Tim Jones

Mandy Minkle

Rose Neelan

Ralph Potter, etc.

 

But I only know how to display all the information at once in one big list (see code below).  Can anyone help guide me on how to achieve this?  Thanks!

 

<?php

include('connection.php');

$result = mysql_query("SELECT * FROM `database` order by year asc, lastname asc");

// Define $color=1 
$color="1";

echo '<head>';
echo '<title>WHS Alumni Association Members Database</title>';
echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '<script type="text/javascript" src="autoclear.js"></script>';
echo '</head>';
echo '<body>';

echo "<div id='container'>";

echo "<div class='pagename'>
<a class='header' href='index.php'>WHS Alumni Association Members Database</a><br /><br />
<form method='post' action='search.php'>
<input type='text' name='searchterm' id='search' value='Search' size='32' maxlength='32' onfocus='clearDefault(this)'>
</form>
</div> <!-- end pagename -->";

while($rows=mysql_fetch_array($result)){

if($color==1){

echo "<div class='bg1'>";

echo "<div class='pad'>";

echo "<span class='fullname'>";

if (!empty($rows['firstname'])) {
echo "<span class='firstname'>
".stripslashes($rows['firstname'])."
</span> <!-- end firstname -->";
}

if (!empty($rows['middlename'])) {
echo "<span class='middlename'>
".stripslashes($rows['middlename'])."&#46;
</span> <!-- end middlename -->";
}

if (!empty($rows['nickname'])) {
echo "<span class='nickname'>
"".stripslashes($rows['nickname']).""
</span> <!-- end nickname -->";
}

if (!empty($rows['maidenname'])) {
echo "<span class='maidenname'>
&#040;".stripslashes($rows['maidenname'])."&#041;
</span> <!-- end maidenname -->";
}

if (!empty($rows['lastname'])) {
echo "<span class='lastname'>
".stripslashes($rows['lastname'])."
</span> <!-- end lastname -->";
}

echo "</span> <!-- end fullname -->";

if (!empty($rows['year'])) {
echo "<div class='year'>
<a href='".stripslashes($rows['year']).".php'>Class of ".stripslashes($rows['year'])."</a>
</div> <!-- end year -->";
}

if (!empty($rows['streetaddress'])) {
echo "<div class='saddress'>
".stripslashes($rows['streetaddress'])."
</div> <!-- end saddress -->";
}

if (!empty($rows['mailingaddress'])) {
echo "<div class='maddress'>
".stripslashes($rows['mailingaddress'])."
</div> <!-- end maddress -->";
}

if (!empty($rows['state'])) {
echo "<div class='citystate'>
".stripslashes($rows['city'])."&#44; ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div> <!-- end citystate -->";
}

if (!empty($rows['areacode'])) {
echo "<div class='phone'>
&#40;".stripslashes($rows['areacode'])."&#41; ".stripslashes($rows['prefix'])."&#45;".stripslashes($rows['suffix'])."
</div> <!-- end phone -->";
}

if (!empty($rows['email'])) {
echo "<div class='email'>
<a href='mailto:".stripslashes($rows['email'])."'>".stripslashes($rows['email'])."</a>
</div> <!-- end email -->";
}

echo "

</div> <!-- end pad -->

</div> <!-- end bg1 --><br clear='all' />";

// Set $color==2, for switching to other color 

$color="2";
}

else {

echo "<div class='bg2'>";

echo "<div class='pad'>";

echo "<span class='fullname'>";

if (!empty($rows['firstname'])) {
echo "<span class='firstname'>
".stripslashes($rows['firstname'])."
</span> <!-- end firstname -->";
}

if (!empty($rows['middlename'])) {
echo "<span class='middlename'>
".stripslashes($rows['middlename'])."&#46;
</span> <!-- end middlename -->";
}

if (!empty($rows['nickname'])) {
echo "<span class='nickname'>
"".stripslashes($rows['nickname']).""
</span> <!-- end nickname -->";
}

if (!empty($rows['maidenname'])) {
echo "<span class='maidenname'>
&#040;".stripslashes($rows['maidenname'])."&#041;
</span> <!-- end maidenname -->";
}

if (!empty($rows['lastname'])) {
echo "<span class='lastname'>
".stripslashes($rows['lastname'])."
</span> <!-- end lastname -->";
}

echo "</span> <!-- end fullname -->";

if (!empty($rows['year'])) {
echo "<div class='year'>
<a href='".stripslashes($rows['year']).".php'>Class of ".stripslashes($rows['year'])."</a>
</div> <!-- end year -->";
}

if (!empty($rows['streetaddress'])) {
echo "<div class='saddress'>
".stripslashes($rows['streetaddress'])."
</div> <!-- end saddress -->";
}

if (!empty($rows['mailingaddress'])) {
echo "<div class='maddress'>
".stripslashes($rows['mailingaddress'])."
</div> <!-- end maddress -->";
}

if (!empty($rows['state'])) {
echo "<div class='citystate'>
".stripslashes($rows['city'])."&#44; ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div> <!-- end citystate -->";
}

if (!empty($rows['areacode'])) {
echo "<div class='phone'>
&#40;".stripslashes($rows['areacode'])."&#41; ".stripslashes($rows['prefix'])."&#45;".stripslashes($rows['suffix'])."
</div> <!-- end phone -->";
}

if (!empty($rows['email'])) {
echo "<div class='email'>
<a href='mailto:".stripslashes($rows['email'])."'>".stripslashes($rows['email'])."</a>
</div> <!-- end email -->";
}

echo "

</div> <!-- end pad -->

</div> <!-- end bg2 --><br clear='all' />";

// Set $color back to 1 
$color="1";
}

}

echo "</div> <!-- end container -->";

echo '</body>';
mysql_close($connection);
?>

you should be able to add the subtitle text with a simple if statment ie

$cYear = "";
while($rows=mysql_fetch_array($result)){
if($cYear != $rows['year']) //if year changed
{
$cYear = $rows['year'];//store new year
echo "class of $cYear<br>";//display subtitle (this is the next year data)
}
//..continue with code

 

Hope that helps

Sweet, that worked!  Thank you very much.

 

you should be able to add the subtitle text with a simple if statment ie

$cYear = "";
while($rows=mysql_fetch_array($result)){
if($cYear != $rows['year']) //if year changed
{
$cYear = $rows['year'];//store new year
echo "class of $cYear<br>";//display subtitle (this is the next year data)
}
//..continue with code

 

Hope that helps

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.