sonam Sharma
sonam Sharma

Reputation: 558

how to fetch and show the results randomly with/without loop php

hello all i am having a site where i need to show random users/ members in this format This is the format in which i want the user images

i want images of the user in the format as above randomely i have more than 9000 members and need to show the random users in this format the design repeat itself by three

i dont know how to use the loop or use the query to show the images in the above format.

my query is like

  $getimages=($con,"select image,id,somethingelse from tablename where id !='' order by rand() limit 21"); 
 // i want 21 images to show here 7*3 (the image here repets it self by three)
 while($theimagelink=mysqli_fetch_array($getimages)){
   // i think array_push may help but dont know how to implement..
 }

please give me some hints or any llink i am stuck over here .....

numbering of the image can be changed .... i need all the three things of select query to display image

Upvotes: 1

Views: 89

Answers (1)

zanderwar
zanderwar

Reputation: 3730

First, you want to get all members, and pull them into an array using your preferred method.

SELECT MemberID,ImageLink,Field4,Etc FROM Members

Then you can randomly select a MemberID by using array_rand():

<?php
$input = array(array('MemberID' => 1, 'ImageLink' => 'someimage.jpg'),array('MemberID' => 2, 'ImageLink' => 'someimage.jpg'),array('MemberID' => 3, 'ImageLink' => 'someimage.jpg'));
$rand_keys = array_rand($input, 2);
var_dump($input[$rand_keys[0]]) . "\n";
var_dump($input[$rand_keys[1]]) . "\n";
?>

Reference: http://php.net/manual/en/function.array-rand.php

To solve your updated question

$getimages=mysqli_query($con,"select image,id,somethingelse from tablename where id !='' order by rand() limit 21"); 
// i want 21 images to show here 7*3 (the image here repets it self by three)
$Results = array();
while($tmp=mysqli_fetch_assoc($getimages)){
    $Results[] = array('image' => $tmp['image'], 'id' => $tmp['id'], 'somethingelse' => $tmp['somethingelse']);
}
var_dump($Results);

Then you can just iterate through the array

for ($i=0; $i < count($Results); $i++) {
    echo "<img src='" . $Results[$i]['image'] . "' alt=''><br/>"
    echo $Results[$i]['id'] . '<br/>'
    echo $Results[$i]['somethingelse'] . '<br/><br/>'
}

Upvotes: 2

Related Questions