jeudi 26 mai 2016

How to use array_rand() or shuffle() functions in php mysql

I have the following working code to output the states and output the relatd cities under each state. However, I want to shuffle or randomize the cities in each state. For example, if the State is California then I want to shuffle or randomize the relate outputted cities under it. I tried to use different methods for this such as ORDER BY RAND(), implode() and so on but didn't get the correct result.

    $get_result = "select DISTINCT state_name, city_name from country WHERE country_name='$select_country' ORDER BY state_name ASC";

    $result = array();

    $run = mysqli_query($dbconfig, $get_result);

    while ($row=mysqli_fetch_array($run)){

        $state_name = $row['state_name'];

        $city_name = $row['city_name'];

            if (!isset($result[$state_name])) {

            echo '<p>' . $state_name . '</p>';

            $result[$state_name] = true;
        }

      echo '<p style="margin-left: 10px;"><a href="http://examplesite.com/'.$city_name.'">'. $city_name .'</a></p>';
}

Any help on this matter would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire