php & mysql random function

Soldato
Joined
10 Apr 2006
Posts
7,920
Location
North West
I need to create a website using php and a mysql database with 1 table.
(its a sweet shop)

The table has already been created for me and populated with data.

I have to create a shopping cart for the website which can add products from the table into it. Im assuming this involves session as I am new to php so I may be wrong!
But on the main page of the site i wanted to create a "sweet of the day" which would be a random selection from the database of the sweets name and image, which are under the columns sName and sImgName.

I've seen someone make a random function on these forums that picks a different sig every time the page is loaded, will that do the job or is it completely different as every time i will have to be querying the database?

Any help will be much appreciated, and anyone that has some spare time over the next 48hours with a good amount of php/mysql knowledge if they could add me to MSN i'd be their friend forever :D
 
Sessions for a shopping cart is the general method of doing things.

As for the sweet of the day, you could probably pull it off using purely MySQL functions—take a look here. I'm unsure on this (so don't quote me), but other more complicated methods include adding entries into crontab or using PHP to store dates somewhere and perform checking based on that.
 
either really, whatever is easiest to do as i could jsut change sweet of the day to sweet of the moment or something.

Might help if i give more data.

The sweets are organised by something called "uid" , so if the randomiser changed this number everyday/time the page was loaded, then i could link it to the other columns?
ie

1 Toffee Toffee.jpg
2 JellyBean Bean.jpg

etc

So if the number were to change it would change what was linked to it therefore changing what is in the DIV, im just guessing here as i have no idea about mysql really :D or php for that matter
 
another question for mysql people!

In the database i have sCategory, each sweet has a category.

For building the menu system i wanted a list of each different category, so it would list:

Fudge
Bubblegum
Chocolate Bar

At the moment i can pull the sCategory out fine and display it, but if there are 3 sweets with sCategory=Fudge, it will display Fudge 3 times.

How can i do it so it takes the data, checks for multiples, deletes them and therefore just displays 1 line for each category?

So instead of showing

Fudge
Fudge
Fudge
Bubblegum
Bubblegum

it would go to

Fudge
Bubblegum

Thanks!
 
Code:
select sweet_id, sweet_name from sweets order by md5(concat(date_format(now(), "%Y%j"), sweet_id)) limit 1;

= different random sweet for each day

Ideally you'd want to store prior "sweet of the day"'s and exclude them from being chosen until they had all been chosen, thats easy to do but a few more lines of code ;)
 
Back
Top Bottom