php - Showing Featured Item From the Database -
i have database table below.
id, bungalow_name, type, address, featured
a bungalow can featured in home page. if bungalow featured, featured
column has value 1
. have 50 bungalows in tables , 5-7 bungalows featured @ given time.
let's assume featured bungalow names below.
bungalow 1, bungalow 2, bungalow 3, .........., bungalow 6
what i'm trying show featured bungalow in home page each day. , want loop below below each month. given don't want show bungalow randomly each page load. want show per day 1 bungalow basis.
today -> bungalow 1 tomorrow -> bungalow 2 day after tomorrow -> bungalow 3 ... after bungalow 6, bungalow 1 shown on next day.
how can it? possible sql/php?
you use mysql query:
select * bungalows id = ( select b1.id bungalows b1 left join bungalows b2 on b1.id>b2.id , b2.featured=1 b1.featured=1 group b1.id having count(b2.id) = (select datediff(curdate(), '2013-05-06') mod (select count(*) bungalows featured=1)) )
please see fiddle here. '2013-05-06' day when want start show first featured bungalow. shown ordered id, strarting '2013-05-06'.
edit
the following query return number of elapsed days since 2013-05-06:
select datediff(curdate(), '2013-05-06')
the mod function return integer remainder of division of number of elapsed day number of featured rows:
select datediff(curdate(), '2013-05-06') mod (select count(*) bungalows featured=1)
if there 6 featured bungalows, return 0 first day,1 second,2,3,4,5, , 0,1,2...again.
mysql not have function return rank (number of row), have simulate somehow. simulated way:
select b1.id, count(b2.id) bungalows b1 left join bungalows b2 on b1.id>b2.id , b2.featured=1 b1.featured=1 group b1.id
i'm joining bungalows table itself. rank of bungalow id count of bungalows have id less (hence join b1.id>b2.id).
i'm selecting row have rank returned function above:
having count(b2.id) = (select datediff(curdate(), '2013-05-06') mod (select count(*) bungalows featured=1))
if use mysql, initial query simplified this:
select b1.* bungalows b1 left join bungalows b2 on b1.id>b2.id , b2.featured=1 b1.featured=1 group b1.id having count(b2.id) = (select datediff(curdate(), '2013-05-06') mod (select count(*) bungalows featured=1))
Comments
Post a Comment