Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

php - Random Switch Statement Improvements?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...