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?
felgall
September 8, 2016, 9:47pm
3
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']?>&Type=<?php echo $f['IdType_Hot']?>&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
forget mysql_query, use PDO
to randomize an array use shuffle()
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']?>&Type=<?=$f['IdType_Hot']?>&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.
SamA74
September 9, 2016, 9:37am
9
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
SamA74
September 9, 2016, 9:50am
11
@colshrapnel has written a comprehensive PDO tutorial, found here
I am not sure it this is a right place to ask, but I found this community most friendly and thoughtful, so I want to try. If it’s not appropriate, feel free to delete this thread.
I spent several years on Stack Overflow answering questions on PHP. Being interested personally in database wrappers, I kept an eye on PDO related questions in particular.
With a lot of data to review, one is able to collect some statistics, and even trace the common patterns of (mis)use. So did I. From such an exper…
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.
felgall
September 9, 2016, 9:18pm
13
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.
system
Closed
December 10, 2016, 4:18am
14
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.