0

I have business directory site, i want it to be group all post like Craigslist did.

current my data will present on the site like this

<business name1> <phone number> <jan 13 2012>
<business name2> <phone number> <jan 13 2012>
<business name1> <phone number> <jan 12 2012>

I want to layout it like this, so the posts will group by the date published.

<jan 13 2012>
<business name1> <phone number>
<business name2> <phone number>

<jan 12 2012>
<business name1> <phone number>
<business name2> <phone number>

here is my coding

connection 
$query = "SELECT * FROM `myname` ORDER BY `myname`.`id` DESC LIMIT $offset, $limit ";
$result = mysql_query($query);


    while($row = mysql_fetch_row($result))

    {   

?>

<div class="list">
 <div class="ads_name"><a href="http://www.mydomain.com/category/<?=$row[0]?>.html" target="_blank"><?=$row[4]?> </a></div>
    <div class="location"><?= htmlspecialchars($row[5])?></div>
    <div class="phone"><?=formatPhoneNumber($row[3])?></div>
    <div class="date"><?=$row[9]?></div>
</div>

<?php
    }
    mysql_free_result($result);
?>
1
  • Add a GROUP BY date clause to your query, and check the value of date as you loop the results. Every time the value changes, break the data into a new section. Without knowing more about your table schema and the desired markup output, we can't give you much of a more precise answer. Commented Jan 13, 2012 at 17:56

1 Answer 1

1

First of all you need something like

SELECT * FROM `myname` ORDER BY `myname`.`mydate` DESC, `myname`.`id` DESC LIMIT $offset, $limit

To make sure your rows arrive in the correct order. Next you want to do a classic group-change algorithm:

<?php
...

    $date='__invalid__';
    while($row = mysql_fetch_row($result))

    {   

      if ($row[9]!=$date)
      {
        $date=$row[9];
?>
<div class="date_header"><?=$row[9]?></div>

<?php
      }
?>

<div class="list">
 <div class="ads_name"><a href="http://www.mydomain.com/category/<?=$row[0]?>.html" target="_blank"><?=$row[4]?> </a></div>
    <div class="location"><?= htmlspecialchars($row[5])?></div>
    <div class="phone"><?=formatPhoneNumber($row[3])?></div>
    <div class="date"><?=$row[9]?></div>
</div>

<?php
    }
    mysql_free_result($result);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

it works great!! but can you explain to me what is 'invalid'; for?? is that same use as blank?? thanks :)
The original value of '__invalid__' is there to make sure, that at the very start of the loop the if ($row[9]!=$date) will allways fire, so that you allways start a page with a date. You could use $date='' or $date=null or whatever, I chose '__invalid__' to make the code readabel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.