Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP] array rand zamiast order by rand
tenloginjestzaje...
post 22.11.2015, 06:23:20
Post #1





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


Witam
Chcę pobrać ID rekordów z bazy do array następnie jedno ID wylosować. Niestety bezskutecznie. Pokazuje wszystkie ID.
  1. $query0 = mysql_query("SELECT `id` FROM `comments` WHERE `tag` LIKE '% $t %' LIMIT 10000");
  2. while($row0 = mysql_fetch_assoc($query0)){
  3. $array = $row0['id'].', ';
  4. $numbers = array($array);
  5. shuffle($numbers);
  6. foreach ($numbers as $number) {
  7. echo $number;
  8. }
  9. }


Pozdrawiam

Ten post edytował tenloginjestzajety 22.11.2015, 06:30:50


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post
markuz
post 22.11.2015, 11:55:39
Post #2





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


$array to string, następnie $numbers to 1 elementowa tablica składająca się z tego stringa - jak sobie wyobrażasz losowanie elementu z 1 elementowej tablicy?
Lepszym sposobem będzie zrobienie tego na poziomie SQL
  1. ORDER BY RAND() LIMIT 1


--------------------
Go to the top of the page
+Quote Post
Damonsson
post 22.11.2015, 12:37:53
Post #3





Grupa: Zarejestrowani
Postów: 2 355
Pomógł: 533
Dołączył: 15.01.2010
Skąd: Bydgoszcz

Ostrzeżenie: (0%)
-----


Cytat(markuz @ 22.11.2015, 11:55:39 ) *
  1. ORDER BY RAND() LIMIT 1

Pamiętaj, że to strasznie spowolni wykonanie zapytania!
Go to the top of the page
+Quote Post
tenloginjestzaje...
post 22.11.2015, 15:18:46
Post #4





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


Wiem, dlatego chcę zastosować tablice array i z niej wybrać losowo id. Używam tego kodu jednak wyświetla wszystko.
$array = '1, 2, 3, 4, 5,';
$numbers = array($array);
shuffle($numbers);
foreach ($numbers as $number) {
echo $number;
}


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post
virnik
post 22.11.2015, 15:26:22
Post #5





Grupa: Zarejestrowani
Postów: 5
Pomógł: 2
Dołączył: 19.11.2015

Ostrzeżenie: (0%)
-----


Bo wszystko wyświetlasz. Skoro potrzebujesz jedno Id to po zamieszaniu tablicą wybierz któryś np. pierwszy.
  1. $array = '1, 2, 3, 4, 5,';
  2. $numbers = array($array);
  3. shuffle($numbers);
  4. echo $numbers[1];
  5. }
Go to the top of the page
+Quote Post
tenloginjestzaje...
post 22.11.2015, 15:33:54
Post #6





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


  1. $array = '1, 2, 3, 4, 5';
  2. $numbers = array($array);
  3. shuffle($numbers);
  4. echo $numbers[0];


pokazuje: 1, 2, 3, 4, 51, 1, 2, 3, 4, 5

  1. $array = '1, 2, 3, 4, 5';
  2. $numbers = array($array);
  3. shuffle($numbers);
  4. echo $numbers[1];


nic nie pokazuje.


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post
virnik
post 22.11.2015, 15:37:40
Post #7





Grupa: Zarejestrowani
Postów: 5
Pomógł: 2
Dołączył: 19.11.2015

Ostrzeżenie: (0%)
-----


Spróbuj tak:
  1. $numbers = array('1','2','3','4','5','6');
  2. shuffle($numbers);
  3. echo $numbers[1];
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.05.2024 - 09:42