473,769 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 2000
digidave
2 New Member
Anyone, Bueller.. Bueller?
Oct 14 '07 #2

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

Similar topics

2
14169
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 Smarty-2.6.7.tar.gz on a system running WindowsXP SP2. Apache and PHP tested out fine. After adding Smarty, I ran the following http://localhost/testphp.php
3
2550
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 on individual product names. I am getting data by calling the same stored procedure multiple times... for the single set of data I use "product like '%'" To get the data for individual products, I am using a cursor to parse
6
2800
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. For each ID I would like to sum all the transactions for the most recent year in which one or more transactions have occurred. Thanks for any help and for all previous help.
7
3430
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, tblEvents.eventcity, tblEvents.eventstate, tblEvents.eventstartdt, tblEvents.eventenddt, tblTrials.trialnbr, tblTrials.trialdt, tblTrialClass.trialclassID, tblClasses.class, tblScores.score, tblScores.level, . & (" "+.+".") & " " & . & (" "+.+".") & " (" & . & ",...
2
3627
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 Object) If (TypeOf obj Is LiteralControl) Then Dim lc As LiteralControl = obj Dim text As String = lc.Text.ToLower()
2
1101
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: Magazines (table header) avg price: 4.50 total sold: 200 left in stock: 130
14
7351
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 writing 1300 asp pages to deal with this doesn't thrill me. The HTML pages are provided by a training company. They seem to be "structured" to some degree, but I'm not sure how easy its going to be to parse the page.
3
4109
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 the data is summed over group. But the data is summed over the second group. When a new group begins MyTextBox value is resetting to zero. I need a running sum over the first group so when another second group begins MyTextBox value will
2
3611
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 the contacts that attended in both 2005 and 2006. Table1 ContactID ContactName 1 John 2 Bob 3 Amy
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10038
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9857
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.