Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to improve my switch statement to make it more random. Currently I am trying to randomize profiles. Two profiles are displayed at a single time one above the other. These profiles are on a slideshow and fade in and out every 2.5 seconds. I do not want the same profile to show up at the same time (both on top and bottom) when the webpage is loaded. Thank you in advance for any input you might have. I have created the two switch statements as follows:

<div id="Slider">

<?php

$getSliderInfoQuery = "SELECT f_name, l_name, city, zipcode, pst.name as state_id, book_types, profile_photo, profile_url, prt.rating  FROM book_readers ps left join book_states pst on pst.state_id = ps.state_id left join book_reviews prt on prt.user_id = ps.user_id  WHERE promoted_reader = 1 ORDER BY ";

$pickRow = mt_rand(1, 6);

$pickRow = mt_rand(1, 6);

switch($pickRow) {

case 1:
$getSliderInfoQuery .= "l_name";
break;

case 2:                             
$getSliderInfoQuery .= "f_name";
break;

case 3:
$getSliderInfoQuery .= "city";
break;

case 4:
$getSliderInfoQuery .= "profile_photo";
break;

case 5:
$getSliderInfoQuery .= "l_name DESC";
break;

case 6:
$getSliderInfoQuery .= "city DESC";
break;

<div id="Slider2">

<?php

$getSliderInfoQuery = "SELECT f_name, l_name, city, zipcode, pst.name as state_id, book_types, profile_photo, profile_url, prt.rating  FROM book_readers ps left join book_states pst on pst.state_id = ps.state_id left join book_reviews prt on prt.user_id = ps.user_id  WHERE promoted_reader = 1 ORDER BY ";

$pickRow = mt_rand(1, 6);

switch($pickRow) {

case 1:
$getSliderInfoQuery .= "f_name";
break;

case 2:
$getSliderInfoQuery .= "l_name";
break;

case 3:
$getSliderInfoQuery .= "city";
break;

case 4:
$getSliderInfoQuery .= "profile_photo";
break;

case 5:
$getSliderInfoQuery .= "city DESC";
break;

case 6:
$getSliderInfoQuery .= "l_name DESC";
break;
}

$sliderResult = mysql_query($getSliderInfoQuery);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
330 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use the MySQL ORDER BY RAND() without your php switch.

$getSliderInfoQuery = "SELECT f_name, l_name, city, zipcode, pst.name as state_id, book_types, profile_photo, profile_url, prt.rating
FROM book_readers ps
left join book_states pst on pst.state_id = ps.state_id
left join book_reviews prt on prt.user_id = ps.user_id
WHERE promoted_reader = 1
ORDER BY rand()
LIMIT 2";

Add a LIMIT to get the number of rows you want.

Prefer to make the minimum of sql queries as you can (and iterate over the rows) instead of making a query again and again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...