Jump to content

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.