shuffle();
بصورت تصادفی
ORDER BY rand()
تعداد 5
LIMIT 5
SELECT * FROM `table_name` WHERE 1 ORDER BY rand() LIMIT 5
سلام
این تابع به صورت تصادفی یه ارایه رو سورت میکنه! میتونید یه بار ارایه مربوط به اطلاعات بازکشی شده از دیتابیس رو بهش بدید بعدPHP:shuffle();
هر چندتاش رو میخواید نمایش بدید.
البته ممکنه تابع مشابه ای هم تو خود mysql موجود باشه که برای فهمیدنش میتونید به http://www.mysql.com سر بزنید.
موفق باشید
$myArr[0]['name'] = "ali";
$myArr[0]['tel'] = "2323";
$myArr[1]['name'] = "mamali";
$myArr[1]['tel'] = "2321";
SortByField($myArr, "tel")
نمونه:
کد:SELECT * FROM `table_name` WHERE 1 ORDER BY rand() LIMIT 5
If you need to sort a multi-demension array, for example, an array such as
$TeamInfo[$TeamID]["WinRecord"]
$TeamInfo[$TeamID]["LossRecord"]
$TeamInfo[$TeamID]["TieRecord"]
$TeamInfo[$TeamID]["GoalDiff"]
$TeamInfo[$TeamID]["TeamPoints"]
and you have say, 100 teams here, and want to sort by "TeamPoints":
first, create your multi-dimensional array. Now, create another, single dimension array populated with the scores from the first array, and with indexes of corresponding team_id... ie
$foo[25] = 14
$foo[47] = 42
or whatever.
Now, asort or arsort the second array.
Since the array is now sorted by score or wins/losses or whatever you put in it, the indices are all hoopajooped.
If you just walk through the array, grabbing the index of each entry, (look at the asort example. that for loop does just that) then the index you get will point right back to one of the values of the multi-dimensional array.
Not sure if that's clear, but mail me if it isn't...
-mo
Here's my fixed up msort array. What it does is goes through a multidimensional array, and sorts it by the desired key (defaulting to 'id').
So, for example, if you have an array like:
array[0]['value'] = "statement 2"
array[0]['id'] = "2"
array[1]['value'] = "statement 3"
array[1]['id'] = "3"
array[2]['value'] = "statement 1"
array[2]['id'] = "1"
it would rearrange and return the array to be like:
array[0]['value'] = "statement 1"
array[0]['id'] = "1"
array[1]['value'] = "statement 2"
array[1]['id'] = "2"
array[2]['value'] = "statement 3"
array[2]['id'] = "3"
The 'id' index can start at any point, and any array item missing the id index will be added to the end.
<?php
function msort($array, $id="id") {
$temp_array = array();
while(count($array)>0) {
$lowest_id = 0;
$index=0;
foreach ($array as $item) {
if (isset($item[$id]) && $array[$lowest_id][$id]) {
if ($item[$id]<$array[$lowest_id][$id]) {
$lowest_id = $index;
}
}
$index++;
}
$temp_array[] = $array[$lowest_id];
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
}
return $temp_array;
}
?>
Ex:
<?php
//oh no, this is not in the ordered by id!!
$data[] = array("item"=>"item 4");
$data[] = array("item"=>"item 1", "id"=>1);
$data[] = array("item"=>"item 3", "id"=>3);
$data[] = array("item"=>"item 2", "id"=>2);
var_dump( msort($data) ); //just msort it!
/* outputs
array
0 =>
array
'item' => 'item 1' (length=6)
'id' => 1
1 =>
array
'item' => 'item 2' (length=6)
'id' => 2
2 =>
array
'item' => 'item 3' (length=6)
'id' => 3
3 =>
array
'item' => 'item 4' (length=6)
*/
?>
علیرضا جون از جوابت ممنونم و از بقیه دوستان هم سپاسگزاری میکنم .
فقط یه سوال!!
توی دستور بالا بعداز کلمه WHERE نوشتی 1 . آیا اون معنی خاصی داره یا بجای پارامتر خاصی نوشتیش؟
ممنونم
سلام
خواهش بابک جان! قابلی نداشت!
والا جلال جابهترین چیزی که در این مورد برات \یدا کردم با استفاده از asort(); هست! که همونطور که میدونی این تابع برای مرتب سازی ارایه ها با حفظ تناظر بین اندیسها و مقادیر هست.
تو منوال php برای مرتب سازی ارایه های چند بعدی با استفاده از یکی از خونه ها پیشنهادی که ارایه شده این هست:
SortByField($myArr, "tel")
یعنی واقعاً php تابع (دست نویس نه!) درست درمونی برای این کار نداره؟