Jump to content

Looping & seperating data


graham23s

Recommended Posts

Hi Guys,

 

In a search query on my site i have the basic code setup like this:

 

<?php
   $s = mysql_query("SELECT * FROM `cpa` WHERE `raw_search` LIKE '%$search%' ORDER BY `raw_search` DESC");
   
   while ($r = mysql_fetch_array($s))
   {
   
     print $r['link'] . "<br />";
    
   }
?>

 

This prints all the search data in a row, the only thing that groups the data together is the time stamp it was inserted in to the database with.

 

search1 (timestamp1)

search1 (timestamp1)

search2 (timestamp2)

search2 (timestamp2)

search2 (timestamp2)

 

what i am trying to do is put a <hr> under the last result to seperate them up a bit on page like:

 

search1 (timestamp1)

search1 (timestamp1)

_________________

search2 (timestamp2)

search2 (timestamp2)

search2 (timestamp2)

 

i can't think of way to do it, any help would  be appreciated.

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/190491-looping-seperating-data/
Share on other sites

Hi Guys

 

The output would be like:

 

site1.com/page1 added on 2010-01-31 23:17:53

site1.com/page2 added on 2010-01-31 23:17:53

site2.com/page1 added on 2010-01-31 23:17:56

site2.com/page2 added on 2010-01-31 23:17:56

site2.com/page3 added on 2010-01-31 23:17:56

 

they output one after each other, to make them a little better i was trying to seperate them like:

 

site1.com/page1 added on 2010-01-31 23:17:53

site1.com/page2 added on 2010-01-31 23:17:53

-------------------------------------------------------------- <- seperator

site2.com/page1 added on 2010-01-31 23:17:56

site2.com/page2 added on 2010-01-31 23:17:56

site2.com/page3 added on 2010-01-31 23:17:56

 

thanks guys

 

Graham

<?php

$s = mysql_query("SELECT * FROM `cpa` WHERE `raw_search` LIKE '%$search%' ORDER BY `raw_search` DESC");

$last_timestamp = '';
while ($r = mysql_fetch_array($s)) {

print $r['link']."<br />";
    preg_match('added on \K.*',$r['link'], $out);
    if($out[0] != $last_timestamp && $last_timestamp != '')
        echo '<hr />';
    $last_timestamp = $out[0];

}

?>

Try that

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.