outchy Posted March 13, 2009 Share Posted March 13, 2009 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']).". </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'> (".stripslashes($rows['maidenname']).") </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']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])." </div> <!-- end citystate -->"; } if (!empty($rows['areacode'])) { echo "<div class='phone'> (".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".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']).". </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'> (".stripslashes($rows['maidenname']).") </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']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])." </div> <!-- end citystate -->"; } if (!empty($rows['areacode'])) { echo "<div class='phone'> (".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149277-solved-display-information-broken-down-by-field/ Share on other sites More sharing options...
MadTechie Posted March 13, 2009 Share Posted March 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149277-solved-display-information-broken-down-by-field/#findComment-783953 Share on other sites More sharing options...
outchy Posted March 13, 2009 Author Share Posted March 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149277-solved-display-information-broken-down-by-field/#findComment-783966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.