Arrange rows retrieved from database

Discussion in 'PHP' started by rrn, May 3, 2009.

  1. #1
    in my website ( php , sql ) . i am retrieving certain rows from database table in the descending order of the date .

    for eg : my table will look like this .

    title date

    aaaa 16/3/2009
    bbbb 15/3/2009
    cccc 6/2/2009
    dddd 2/12/2008
    eeee 1/12/2007

    what i want to do is

    i want to leave a space after each group of years.
    ie the table should become as follows

    title date

    aaaa 16/3/2009
    bbbb 15/3/2009
    cccc 6/2/2009

    dddd 2/12/2008

    eeee 1/12/2007

    how can i do that? somebody pls give a solution...
     
    rrn, May 3, 2009 IP
  2. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #2
    hmm, i may be getting this wrongly, but do you actually want to fetch data with spaces in between groups? or just want to display them like that?
     
    creativeGenius, May 3, 2009 IP
  3. Karthik

    Karthik Peon

    Messages:
    76
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Just do a compare of the year part of the date and see if its different, if it is, echo a new line.
    If you could post your code here, I can suggest the changes that you need to do.
     
    Karthik, May 3, 2009 IP
  4. rrn

    rrn Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    actually i want to display it on the website .

    code is as follows .
    $sql = $db->sql_query("SELECT * FROM ".REPORTS_TABLE." WHERE fin_publish='Yes' $WHERE ORDER BY fin_date DESC");
    
    
    while ($res = $db->sql_fetchrow($sql)) {
    	 *********retrieving fields *****
    	));
    PHP:
     
    rrn, May 3, 2009 IP
  5. Karthik

    Karthik Peon

    Messages:
    76
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That SQL query is constructed wrong, I'm not sure how its working for you.
    Also, don't use "*" to retrieve all the fields in a table, always use the field names.
     
    Karthik, May 4, 2009 IP
  6. bluebenz

    bluebenz Well-Known Member

    Messages:
    876
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    138
    #6
    a stupid solution, keep check the years, if it is same as above, then echo a blank space..

    
    
    $cdate="";
    while ($res = $db->sql_fetchrow($sql)) {
      if ($cdate != $res["date"]) { 
         echo "<br>";
      }
      echo $res["title"] ." " .$res["date"] ."<br>";
      $cdate=$res["date"];
      
    };
    
    
    Code (markup):
     
    bluebenz, May 4, 2009 IP