Random Switch Statement Improvements?

大城市里の小女人 提交于 2019-12-13 09:14:24

问题


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);

回答1:


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.




回答2:


Using mt_rand(1, 6) there's 1/6 chance you'll end up with the same $pickRow. Here's a piece of code that will make sure you will never get the same value:

$range_values = range(1, 6);
shuffle($range_values);
$pickRow = array_pop($range_values);

// some code using $pickRow

$pickRow = array_pop($range_values);

// some code using $pickRow

Add a check for the array size so you don't end up popping from an empty array, and you're good to go.

Cheers.



来源:https://stackoverflow.com/questions/23214561/random-switch-statement-improvements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!