dachshund Posted August 2, 2009 Share Posted August 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/ Share on other sites More sharing options...
Mark Baker Posted August 2, 2009 Share Posted August 2, 2009 SELECT * FROM content WHERE `section` LIKE '%feature%' AND `id` < ( SELECT MAX(`id`) FROM content WHERE `section` LIKE '%feature%' ) ORDER BY RAND() LIMIT 0,4 Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889092 Share on other sites More sharing options...
dachshund Posted August 2, 2009 Author Share Posted August 2, 2009 this seems pretty perfect but the page still comes up blank. hmmm. am i missing something? Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889099 Share on other sites More sharing options...
Mark Baker Posted August 2, 2009 Share Posted August 2, 2009 If the page comes up blank, then the problem may not be in your query, but elsewhere in your php Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889102 Share on other sites More sharing options...
dachshund Posted August 2, 2009 Author Share Posted August 2, 2009 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) . '...'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889111 Share on other sites More sharing options...
Philip Posted August 2, 2009 Share Posted August 2, 2009 Maybe it is because you're not printing anything to the output? You need to echo/print what you want the user to see. Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889115 Share on other sites More sharing options...
dachshund Posted August 2, 2009 Author Share Posted August 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889117 Share on other sites More sharing options...
dachshund Posted August 3, 2009 Author Share Posted August 3, 2009 any one spot anything? Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889398 Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 Add this right after the $result variable echo mysql_num_rows($result); and tell me what the output is. Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889402 Share on other sites More sharing options...
dachshund Posted August 3, 2009 Author Share Posted August 3, 2009 still comes up blank. this is so annoying. Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889415 Share on other sites More sharing options...
dachshund Posted August 3, 2009 Author Share Posted August 3, 2009 but if i do it with $sql = "SELECT * FROM content WHERE `section` LIKE '%feature%' ORDER BY RAND() DESC LIMIT 1,4"; then the output is 4. Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889416 Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 Comes up blank cause the query is wrong try SELECT * FROM `content` WHERE `section` 'feature%' AND `id` < (MAX(`id)) ORDER BY RAND() Limit 4 Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889426 Share on other sites More sharing options...
dachshund Posted August 3, 2009 Author Share Posted August 3, 2009 still comes up blank. tried this too. $sql = "SELECT * FROM `content` WHERE `section` LIKE '%feature%' AND `id` < (MAX(`id`)) ORDER BY RAND() LIMIT 4"; Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889443 Share on other sites More sharing options...
Mark Baker Posted August 3, 2009 Share Posted August 3, 2009 Have you tried executing the SQL query in phpMyAdmin? Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889453 Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 Have you tried executing the SQL query in phpMyAdmin? I suggest you do that OP. PM me your database table for content, a screenshot so i can take a look at it Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-889459 Share on other sites More sharing options...
dachshund Posted August 5, 2009 Author Share Posted August 5, 2009 Here's a screenshot. Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-891309 Share on other sites More sharing options...
dachshund Posted August 5, 2009 Author Share Posted August 5, 2009 any ideas anyone? i'd love to sort out this problem. it's giving me the itch! Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-891690 Share on other sites More sharing options...
dachshund Posted August 6, 2009 Author Share Posted August 6, 2009 ok, seems not, for now is there a way i can choose an id number i want to be excluded? $sql = "SELECT * FROM content WHERE `section` LIKE '%feature%' ORDER BY RAND() LIMIT 1,4"; Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-892320 Share on other sites More sharing options...
Philip Posted August 7, 2009 Share Posted August 7, 2009 Simply add a condition in the where clause: AND id != # Quote Link to comment https://forums.phpfreaks.com/topic/168547-random-selection-excluding-most-recent/#findComment-892946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.