473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Group results by year then parse using respective year as header for each group

I am keenly aware that my coding skills are extremely noob but please indulge me a second.. Take a look at these queries..

Expand|Select|Wrap|Line Numbers
  1.     $sql = "SELECT DISTINCT year FROM _current_floats_config WHERE active = 'yes' ORDER BY year DESC LIMIT 2, 1";
  2.     $result = mysql_query($sql);
  3.         while($fetched = mysql_fetch_array($result)) {
  4.     $ceiling = $fetched["year"];
  5.     }
  6.     $sql = "SELECT * FROM _current_floats WHERE active = 'yes' AND yearID <= $ceiling ORDER BY yearID DESC, name ASC";
As you can see in the first $sql query I'm ordering the query DESC and skipping the first two distinct results. This is because my client wants to features the first two years in the site's navigation. So we pick up the query at result 2 and assign the var $ceiling to the result.

I'm then creating another $sql query selecting all results in DESC order less than or equal to $ceiling.

What I need to do now is group these results by years, like this below:

2006

result, result, result, result, result, result, result, result, result

2005

result, result, result, result, result, result, result, result, result

2004

result, result, result

2003

result, result, result, result, result, result

etc, etc..

Here's the snippet that returns all the results without grouping them by year..

Expand|Select|Wrap|Line Numbers
  1.     $display = 3;
  2.     $cols = 0;
  3.     $sql = "SELECT DISTINCT year FROM _current_floats_config WHERE active = 'yes' ORDER BY year DESC LIMIT 2, 1";
  4.     $result = mysql_query($sql);
  5.         while($fetched = mysql_fetch_array($result)) {
  6.     $ceiling = $fetched["year"];
  7.     }
  8.     $sql = "SELECT * FROM _current_floats WHERE active = 'yes' AND yearID <= $ceiling ORDER BY yearID DESC, name ASC";
  9.     echo $sql."<br /><br />";;
  10.     $result = mysql_query($sql); 
  11.     if (mysql_num_rows($result) > 0 ) {
  12.  
  13. /*        while($fetched = mysql_fetch_array($result)) {
  14.         echo $fetched["yearID"]."<br />";
  15.         } // end WHILE statement
  16.     } else { echo "Ooops!"; } // end IF statement
  17.  
  18. */
  19.     echo "<table width='100%' border='0' cellspacing='10' cellpadding='0' style='padding:10px;'>";
  20.  
  21. //$num = 1;
  22. while($fetched = mysql_fetch_array($result)){
  23. $num = $fetched["ID"];
  24.     if($cols == 0){
  25.         echo "<tr>\n";
  26.     }
  27.     // put what you would like to display within each cell here
  28.  
  29. if (((!empty($fetched["rendering"])) AND (!empty($fetched["rendering2"]))) OR ((!empty($fetched["rendering"])) AND (!empty($fetched["parade_pic"]))) OR ((!empty($fetched["parade_pic"])) AND (!empty($fetched["parade_pic2"])))) {
  30.  
  31.     echo "<td valign=\"top\" align=\"center\" width=\"33%\"><h1 style=\"font-size:96%;\"><a title=\"$fetched[name]\" href=\"current.php?year=$fetched[yearID]&id=$fetched[ID]\">$fetched[name]</a></h1><br /><script type=\"text/javascript\">new fadeshow(fadeimages".$num.", 150, 150, 0, 5000, 0)</script></td>\n";
  32.     $cols++;
  33.     } elseif (!empty($fetched["rendering"])) {
  34.     echo "<td valign=\"top\" align=\"center\" width=\"33%\"><h1 style=\"font-size:96%;\"><a title=\"$fetched[name]\" href=\"current.php?year=$fetched[yearID]&id=$fetched[ID]&viewing=rendering\">$fetched[name]</a></h1><img class=\"reflect ropacity25 rheight15\" alt=\"$fetched[name]\" src=\"dynamic/images/show_image.php?filename=$fetched[rendering]&width=150&height=150\"></td>\n";
  35.     $cols++;
  36.     } elseif (!empty($fetched["parade_pic"])) {
  37.     echo "<td valign=\"top\" align=\"center\" width=\"33%\"><h1 style=\"font-size:96%;\"><a title=\"$fetched[name]\" href=\"current.php?year=$fetched[yearID]&id=$fetched[ID]&viewing=parade_pic\">$fetched[name]</a></h1><img class=\"reflect ropacity25 rheight15\" alt=\"$fetched[name]\" src=\"dynamic/images/show_image.php?filename=$fetched[parade_pic]&width=150&height=150\"></td>\n";
  38.     $cols++;
  39.     } else {
  40.     echo "<td valign=\"top\" align=\"center\" width=\"33%\"><h1 style=\"font-size:96%;\"><a title=\"$fetched[name]\" href=\"current.php?year=$fetched[yearID]&id=$fetched[ID]\">$fetched[name]</a></h1><img class=\"reflect ropacity25 rheight15\" alt=\"$fetched[name]\" src=\"dynamic/images/show_image.php?filename=none.jpg&width=150&height=150\"></td>\n";
  41.     $cols++;
  42.     }
  43.     if($cols == $display){
  44.         echo "</tr>\n\n";
  45.         $cols = 0;
  46.     }
  47.  
  48. } // end IF statement
  49.  
  50. // added the following so it would display the correct html
  51. if($cols != $display && $cols != 0){
  52.     $neededtds = $display - $cols;
  53.     for($i=0;$i<$neededtds;$i++){
  54.         echo "<td>&nbsp;</td>\n";
  55.     }
  56.      echo "</tr>\n</table>\n"; 
  57.     } else {
  58.     echo "</table>\n";
  59.  
  60. } // end WHILE statement
  61.  
  62.         //
  63.         // THERE ARE NO RESULTS FOR THE GIVEN YEAR
  64.         //
  65.  
  66.  
  67. } else { echo "<p style=\"font-size:2.4em;color:#FF0C12;margin-bottom:-25px;\">Oops!</p><p>We couldn't find any floats in our database beyond the past two years.</p><p>You can check back later or select a year under the <strong>Our Floats</strong> heading on the left to continue.</p>"; }
  68.  
  69. }
Can someone steer me in the right direction? Thanks!
Oct 11 '07 #1
1 1958
Anyone, Bueller.. Bueller?
Oct 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
3
by: wxbuff | last post by:
I have a report based on our product names that consists of two parts. Both insert data into a temporary table. 1. A single grouped set of results based on all products 2. Multiple tables based...
6
by: Terri | last post by:
I have a table called Transactions with 3 fields: ID, Date, and Amount. Each ID can have multiple transactions in one particular year. An ID might not have had any transactions in recent years. ...
7
by: SueB | last post by:
Greetings. I have a report based on the following query (hang in there ... it's quite long): SELECT Year(.) AS Yr, tblEvents.eventID, tblEvents.eventname, tblEvents.eventhost,...
2
by: name | last post by:
The piece of code is for a Web Form Page. Who can tell me why? Thanks a lot! ------------------------------------------------ VB.Net Code: Protected Overrides Sub AddParsedSubObject(ByVal obj As...
2
by: Steve Franks | last post by:
Can someone please recommend a good way in ASP.NET 1.1 to dynamically create a HTML table to display some results I have in local variables? For example, I want my HTML output to look like this:...
14
by: Rob Meade | last post by:
Hi all, I'm working on a project where there are just under 1300 course files, these are HTML files - my problem is that I need to do more with the content of these pages - and the thought of...
3
by: Sebastian | last post by:
Hello all I have a report where I have two nested groups. I know there are only three standard options for running sum: None, Over Group and Over All. I have a MyTextBox in detail section where...
2
by: KTosser | last post by:
I have two tables, one contains contacts and the other has all the events and years that the contacts participated in. What I want is to be able to choose the years, say 2005 and 2006, and get all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.