Random selection prob with PHP & mysql

Hello,
I am trying to random selection from my database. Suppose i have 1000 posts but i want to select last 20 posts and from those 20 posts ORDER BY postTime i will select 4 posts from that 20 posts and echo them.

My code is:

<?php
$qryRecent = "SELECT * FROM allnewsdetails ORDER BY publishDate DESC LIMIT 20";
                $resultRecent = @mysql_query($qryRecent);
while($getCateNews = mysql_fetch_array($resultRecent )){

?>
                <article class="item_left">
                  <div class="pic"> <a href="post-standart.html" class="w_hover img-link img-wrap"> <img src="images_post/13-170x126.jpg" alt="" ></a> </div>
                  <h3><a href="post-standart.html">Curabitur enim dui, euismod convallis.</a></h3>
                  <div class="post-info"> <a href="post-standart.html" class="post_date">May 30, 2013</a> <a href="post-standart.html" class="comments_count">2</a> </div>
                </article>
<?php } ?>

But i am able to echo those 20 posts but i am unable to echo only 4 posts from those 20 posts randomly. If you know then please help me.
Thanks in advance :slight_smile:

This is untested…
Build data array with 20 records.
Use array_rand($data, 4) to get 4 random keys
Loop through these keys as you display data.

<?php
$qryRecent = "SELECT * FROM allnewsdetails ORDER BY publishDate DESC LIMIT 20";
    $resultRecent = @mysql_query($qryRecent);
    $data = array();
while($getCateNews = mysql_fetch_array($resultRecent )){
    $data[] = $getCateNews;
}
$rand_keys = array_rand($data, 4); 
foreach($rand_keys as $key){

//row fields are then grabbed by using $data[$key] example $data[$key]['id']
?>
    <article class="item_left">
        <div class="pic"> <a href="post-standart.html" class="w_hover img-link img-wrap"> <img src="images_post/13-170x126.jpg" alt="" ></a> </div>
        <h3><a href="post-standart.html"><?php echo $data[$key]['id'];?> Curabitur enim dui, euismod convallis.</a></h3>
        <div class="post-info"> <a href="post-standart.html" class="post_date">May 30, 2013</a> <a href="post-standart.html" class="comments_count">2</a> </div>
    </article>
<?php } ?>

Again untested.

I’m a PHP noobie but shouldn’t this be in mysqli instead of mysql, since it will be removed very soon (and is deprecated?)

This goes for the OP too.

1 Like

Thank you @Drummin
This is working but you closed the while so how i can fetch data from the while ? When that is closed.
Please let me know.
Thanks.

Ya it will be. I will convert it but before that i am working on it now.
Thanks.

Agreed but I tend to help with the OP issue, not preach. There are plenty who will. If I were, I would suggest PDO.

[quote=“anturj, post:4, topic:111061”]
This is working but you closed the while so how i can fetch data from the while ? When that is closed.
[/quote]You are building $data array with the rows in the WHILE. As the comment in code shows you use data then key then field name to grab data.

$data[$key]['id']
$data[$key]['name']

etc.

[quote=“anturj, post:5, topic:111061”]
Ya it will be. I will convert it but before that i am working on it now.
[/quote]It is much easier to learn for example PDO and write your code this way then to convert a bunch of code. I’ve done a few conversion projects and they’re a pain. Much easier to write it correctly in the first place.

2 Likes

Thank you @Drummin so much :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.