Group By Date?

Discussion in 'PHP' started by IGiveMoney, Feb 14, 2009.

  1. #1
    Ok so I have a very basic setup for an articles page.

    The database stores the date format like this:

    Feb 13th, 2009


    What I am trying to pull off is rather simple (i think)
    but I can't seem to get it right to do what I would like.

    When the articles are pulled to the news.php page I would
    like them to be formatted like this:


    Feb 13th, 2009
    > Article #1
    > Article #2

    Feb 18th, 2009
    > Article #1
    > Article #2
    > Article #3
    > Article #4

    Feb 22nd, 2009
    > Article #1


    I am not really sure where I am going wrong.

    Here is my code and any assistance is appreciated!

    
    
    <?php
    
    require('connector/db.inc.php');
    
    
    $sql = "SELECT date, title FROM articles GROUP BY date";
    $res = mysql_query($sql);
    
    	while($row = mysql_fetch_array($res)){
    		$date = $row['date'];
    		$title = $row['title'];
    			
    		echo '<h4>' . $date . '</h4>';
    			
    			$sql_AC = "SELECT id, review, status, source, web FROM articles WHERE title='{$title}', date = '{$date}' AND status='1'";
    			$res_AC = mysql_query($sql_AC);
    			$row_AC = mysql_fetch_assoc($res_AC);
    
    			$title = $row['title'];
    			$id = $row['id'];
    
    
    			echo '<ul>
    				<li>
    				<a href="article.php?id=' . $id . '">' . $title . '</a>
    				</li>
    				</ul><br><br>';
    	}
    
    ?>
    
    
    
    
    
    PHP:

    PS: Presently it IS pulling the dates properly, but I
    now need it to get to loop thru the articles that match that date.
     
    IGiveMoney, Feb 14, 2009 IP
  2. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    can anyone offer any suggestions?
     
    IGiveMoney, Feb 14, 2009 IP
  3. bwp58

    bwp58 Peon

    Messages:
    485
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you need another while statement for the article output
     
    bwp58, Feb 14, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Try changing this in the second query:

    WHERE title='{$title}', date = '{$date}'

    to this:

    WHERE date = '{$date}'

    With the code you posted above, your output will end up looking like this:
    Feb 13th, 2009
    > Article #1

    Feb 18th, 2009
    > Article #1

    Feb 22nd, 2009
    > Article #1

    You need to remove the title from the second query to get all of the results.
     
    jestep, Feb 15, 2009 IP