随机切换语句的改进? [英] Random Switch Statement Improvements?

查看:94
本文介绍了随机切换语句的改进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试改善我的switch语句,使其更加随机.目前,我正在尝试使配置文件随机化.一次同时显示两个配置文件.这些配置文件显示在幻灯片上,每2.5秒淡入和淡出一次.加载网页时,我不希望在同一时间(在顶部和底部)同时显示相同的配置文件.预先感谢您可能提供的任何意见.我创建了两个switch语句,如下所示:

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

推荐答案

您可以在不使用php开关的情况下使用MySQL ORDER BY RAND().

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

添加LIMIT以获得所需的行数.

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

最好尽量减少sql查询(并遍​​历行),而不是一次又一次地查询.

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

这篇关于随机切换语句的改进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆