Jump to content

random selection excluding most recent


dachshund

Recommended Posts

I tried posting on here, but to no avail so far.

 

I'm basically trying display 4 random 'feature' articles, apart from the most recent one (the one with the highest id number out of all of the features).

 

so far I had

 


$sql = "SELECT * FROM content WHERE `section` LIKE '%feature%' ORDER BY RAND() LIMIT 1,4";

 

but that fails to exclude the newest feature.

 

someone else suggested

 

$sql = "SELECT * FROM (SELECT * FROM content WHERE `section` LIKE '%feature%' ORDER BY id desc limit 1,4) as id ORDER BY rand() limit 0,4";

 

which is closer, but then the page is blank.

 

any help would be great.

 

thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/
Share on other sites

SELECT * 
  FROM content 
WHERE `section` LIKE '%feature%' 
   AND `id` < ( SELECT MAX(`id`)
                  FROM content 
                 WHERE `section` LIKE '%feature%'
              ) 
ORDER BY RAND() LIMIT 0,4

can't spot anything.

 

this is the whole thing.

 


<?php
$today = date('Y-m-d');
$sql = "SELECT * FROM content WHERE `section` LIKE '%feature%' AND `id` < ( SELECT MAX(`id`) FROM content WHERE `section` LIKE '%feature%' ) ORDER BY RAND() LIMIT 0,4";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$text = $rows['information'];

if (strlen($text) > 200) {
$text = substr($text, 0, 197) . '...';
}
?>

 

well yeah i have all this too

 


<table width="100%" class="columnpad" cellpadding="2" cellspacing="0">
    <tr>
    	<td width="100%" align="center" valign="top">
        <a href="<? echo $rows['section']; ?>/view_<? echo $rows['section']; ?>.php?id=<? echo $rows['id']; ?>"><img src="<? echo $rows['indeximage']; ?>" width="280" border="0"></a>
        </td>
    </tr>
    <tr>
    	<td width="100%" align="left" valign="top" class="greydotted">
        <span class="smallitalicscapitalised">
        <? echo $rows['type']; ?> <? echo $rows['section']; ?>
        </span>
        <br />
        <span class="h4">
        <a href="<? echo $rows['section']; ?>/view_<? echo $rows['section']; ?>.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a>
        </span>
        <br />
        <span class="indexcontent">
	<? echo $text; ?>
        </span>
        <br />
        <span class="readmore">
        <a href="<? echo $rows['section']; ?>/view_<? echo $rows['section']; ?>.php?id=<? echo $rows['id']; ?>">
        Read More...
        </a>
        </span>
        </td>
    </tr>
</table>

<?php
}
mysql_close();
?>

 

but i don't think the problems there. it all works fine when I don't try to exclude the highest id, so I think it's in the new code.

 

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.