Using order by x and use rand()

Hi, I am displaying 15 hotels in a image carousel, but would like those 15 hotels to be randomised, so it chooses the 15 according to hit_Count_Week DESC but I’d like them to be displayed randomly.

<?php    
select Id_Hot, Nom_Hot, IdRsrt_Hot, IdType_Hot, Foto1_Thumb, IdCat_Hot, Act_Hot, Dir_Hot, Date_Created, hit_Count_Week from tbl_hotels where Act_Hot=1 order by hit_Count_Week DESC LIMIT 15
?>

I tried

hit_Count_Week DESC, rand() LIMIT 15

But it displayed 15 random hotels but not according to hit_Count_Week DESC

Could you retrieve the 15 hotels into an array then display them in a random order, that is have your code do the random ordering rather than the query?

I agree - more efficient to extract the 15 into an array then shuffle the array rather than using order by rand in the query.

OK cheers guys,

Will look into this as havent done it that way before.

I manged to put the data into an array using below

"select Id_Hot, Nom_Hot, IdRsrt_Hot, IdType_Hot, Foto1_Thumb, IdCat_Hot, Act_Hot, Dir_Hot, Date_Created, hit_Count_Week from tbl_hotels where Act_Hot=1 order by hit_Count_Week DESC LIMIT 15");
$num = 1;
$numb = 1;
while($f[]=mysql_fetch_array($d))

//echo "<pre>";
//print_r ($f);
//echo "</pre>";
{ ?>

But im not sure how to now use the array data to fill in the html, so instead of echo $f[‘Id_Hot’] I tried echo $f[0] and didnt get anything

<a href="hotel.php?hotel_ID=<?php echo $f['Id_Hot']?>&amp;Type=<?php echo $f['IdType_Hot']?>&amp;Resort=<?php echo $f['IdRsrt_Hot']?>" title="<?php echo $f['Nom_Hot']?>"><img src="http://www.checksafetyfirst.com<?php echo $f['Foto1_Thumb']?>" id="MostPopularIMG<?php echo $num++ ?>" width="203" height="146" alt="<?php echo $f['Nom_Hot']?>"/></a>

I’m a bit unsure alos where to randomise the array

  1. forget mysql_query, use PDO
  2. to randomize an array use shuffle()
  3. to output an array use foreach()

so

$sql = "select * from tbl_hotels where Act_Hot=1 order by hit_Count_Week DESC LIMIT 15");
$hotels = $pdo->query($sql)->fetchAll();
shuffle($hotels);
foreach ($hotels as $num => $f) {
?>
    <a href="hotel.php?hotel_ID=<?=$f['Id_Hot']?>&amp;Type=<?=$f['IdType_Hot']?>&amp;Resort=<?=$f['IdRsrt_Hot']?>" title="<?=$f['Nom_Hot']?>">
        <img src="http://www.checksafetyfirst.com<?=$f['Foto1_Thumb']?>" id="MostPopularIMG<?=$num?>" width="203" height="146" alt="<?=$f['Nom_Hot']?>"/>
    </a>
<?
 }
1 Like

Cheers colshrapnel,

Have jotted that down in my little php book, something new to learn from.

But I put it in the page and it causes a fatal error on the pdo line.

Fatal error: Call to a member function query() on a non-object in en\index.php on line 517

Yes, PDO object doesn’t pop out of nowhere you have to create it first.

You will need to connect to the database via PDO and use PDO throughout your script for it to work.
I highly recommend that you leave the depreciated mysql for PDO.

Oh I see, sorry yes, will go through it now

@colshrapnel has written a comprehensive PDO tutorial, found here

These are a couple of shorter articles I found useful when getting started with PDO

http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

PDO has many advantages beyond not being obsolete. You won’t look back.

The word is deprecated not depreciated - and the mysql_ interface is no longer deprecated as it was removed from PHP in December last year and so no longer exists at all.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.