1

I am trying to shuffle an array in PHP. I know there are several ways to do this, but I don't achieve it.

I have a PHP file which makes three queries to the DataBase. Every one of the query get elements and at the end I want to shuffle the array to watch the elements in a different order.

I show you my code.

 <?php

 // Parametros para conectar a la base de datos
 $username = "****";
 $password = "****";
 $hostname = "localhost"; 

 // Conectando, seleccionando la base de datos
 $link =  mysql_connect($hostname, $username, $password) or die('No se pudo conectar: ' . mysql_error());
 //echo 'Conectado satisfactoriamente';
 mysql_select_db('Agenda Juvenil') or die('No se pudo seleccionar la base de datos');

  mysql_query('SET CHARACTER SET utf8');

 // HACEMOS LA CONSULTA PARA EL PRIMER TEMA PREFERIDO Y SACAMOS HASTA 30 EVENTOS
 if (isset($_GET['temas_smultiple0'])) {
 $tema0 = $_GET['temas_smultiple0'];

 $query = "SELECT id, title, barrio_smultiple, coordenadas_p_0_coordinate,   coordenadas_p_1_coordinate, gratuita_b, temas_smultiple FROM eventosDiarios WHERE    temas_smultiple IN ('$tema0') limit 0,15";        

$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["eventos"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $evento = array();
        $evento["id"] = $row["id"];
        $evento["title"] = $row["title"];
        $evento["barrio_smultiple"] = $row["barrio_smultiple"];

        $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"];
        $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"];
        $evento["gratuita_b"] = $row["gratuita_b"];
        $evento["temas_smultiple"] = $row["temas_smultiple"];

        // push single product into final response array
        array_push($response["eventos"], $evento);
    }
   }
 }

 //HACEMOS LA CONSULTA PARA EL SEGUNDO TEMA FAVORITO EN CASO DE QUE EXISTA, Y SACAMOS 20 EVENTOS
 if (isset($_GET['temas_smultiple1'])) {
 $tema1 = $_GET['temas_smultiple1'];

$query = "SELECT id, title, barrio_smultiple, coordenadas_p_0_coordinate, coordenadas_p_1_coordinate, gratuita_b, temas_smultiple FROM eventosDiarios WHERE temas_smultiple IN ('$tema1') limit 0,10";      

$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
// looping through all results
// products node

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $evento = array();
        $evento["id"] = $row["id"];
        $evento["title"] = $row["title"];
        $evento["barrio_smultiple"] = $row["barrio_smultiple"];

        $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"];
        $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"];
        $evento["gratuita_b"] = $row["gratuita_b"];
        $evento["temas_smultiple"] = $row["temas_smultiple"];

        // push single product into final response array
        array_push($response["eventos"], $evento);
    }
   }
 }

 //HACEMOS LA CONSULTA PARA EL TERCER TEMA FAVORITO, EN CASO DE QUE EXISTA, Y DEVOLVEMOS 10 EVENTOS
if (isset($_GET['temas_smultiple2'])) {
$tema2 = $_GET['temas_smultiple2'];

 $query = "SELECT id, title, barrio_smultiple, coordenadas_p_0_coordinate, coordenadas_p_1_coordinate, gratuita_b, temas_smultiple FROM eventosDiarios WHERE temas_smultiple IN ('$tema2') limit 0,5";      

$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
// looping through all results
// products node

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $evento = array();
        $evento["id"] = $row["id"];
        $evento["title"] = $row["title"];
        $evento["barrio_smultiple"] = $row["barrio_smultiple"];

        $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"];
        $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"];
        $evento["gratuita_b"] = $row["gratuita_b"];
        $evento["temas_smultiple"] = $row["temas_smultiple"];

        // push single product into final response array
        array_push($response["eventos"], $evento);
    }
   }
 }

 // success
$response["success"] = 1;

// echoing JSON response
 echo json_encode($response);

// Liberar resultados
mysql_free_result($result);


// Cerrar la conexión
 mysql_close($link);
?>

I have prooved with shuffle($response); and shuffle_assoc($response) but nothing..

Thanks in advance.

Regards.

1 Answer 1

2

I think you want to shuffle $response['eventos'] and not $response.

shuffle($response['eventos'])

.. since your main array is an associative array with few keys, where I'm pretty sure you don't care which order the elements are in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.