PDA

View Full Version : Not sure if this is possible - Ramdom quotes from testimonials.


cocoonfx
12-12-2007, 09:15 AM
Hello Chaps


I want to create a dynamic ramdom quote of the week to make the site i am creating look dynamic and being constantly updated.

I will create a table in mysql with about 100 quotes from customer testimonials and i then want the site to update with a new quote from either everytime the page is refreshed or once a week dynamically.

I then will have the availablity for my client to upload new quotes or remove quotes so that it remains fresh.

I can do this with images in Flash but i want to see if i can do this in php. Any clues on how to do this would be wonderful.

Here is an example of my site in flash with ramdom images if you press F5 it should change the main picture every time.

http://www.lizgregsonphotography.com

Regards
James

lux
12-12-2007, 11:15 AM
1. query your database and retrieve the strings in an array
2. use the rand() function to pick one of the strings and store it in a var
3. echo the var on your web page

go to php.net and in the search function list type rand to find a full explanation of how to use the function

davidj
12-12-2007, 11:38 AM
just about to say that!

blast!

lux
12-12-2007, 11:41 AM
*note to davidj: must try harder.

hehe

davidj
12-12-2007, 11:46 AM
set your Array from db results

$row

then do a count on it...

$count = count($array)-1; //<< count array -1 because your referencing keys

then use rand() function

$rand = rand(0, $count) //<< with the first argument being the minimum value and $count being the max


this example would parse as $row[0] or $row[5] which ids records in the $row array by key
echo $row[$rand];

davidj
12-12-2007, 11:48 AM
to give a star trek'd themed insult...

stick that up your worm hole

cocoonfx
12-12-2007, 01:08 PM
Excellent thank you very much.

I shall have a play night with this.

lux
12-12-2007, 01:18 PM
hahaha joker!

davidj
12-12-2007, 01:23 PM
i have other themes!

watch yourself

im a ninja with some handsome qualities.

A mix you dont want to mess with

domedia
12-12-2007, 02:44 PM
im a ninja with some handsome qualities.
A mix you dont want to mess with
http://img5.allocine.fr/acmedia/medias/nmedia/18/36/04/40/18448050.jpg

davidj
12-12-2007, 02:45 PM
err

thats right

pick on the fat kid!

cocoonfx
12-14-2007, 07:36 PM
Hello Chaps


Sorry i can only get this to show 1 or the first entry in the table.

$query =sprintf("SELECT * FROM tips");
$result =mysql_query($query,$dbh);
$row= mysql_fetch_array($result);

$array = $_GET['tip'];

$count = count($array)-1;
$rand = rand(0,$count);

echo $row[$rand];

Now i am sure i am doing something wrong but i can figure what. HELP!

davidj
12-14-2007, 09:20 PM
$query =sprintf("SELECT * FROM tips");
$result =mysql_query($query,$dbh);
$row= mysql_fetch_array($result);

$array = $_GET['tip'];

$count = count($array)-1;
$rand = rand(0,$count);

echo $row[$rand];

this is wrong...
$array = $_GET['tip'];

please study the Array vid tutorial to give you an idea how these work

$row is already an array once set

cocoonfx
12-15-2007, 09:25 PM
Hello

I am completely lost on this. I think i understand the array's but i can not get the code to work. if $row is an array

If i uses

$query =sprintf("SELECT * FROM tips");
$result =mysql_query($query,$dbh);
$row= mysql_fetch_array($result);

$count = count($row)-1;
$rand = rand(0,$count);
echo $rand;

This ramdomly shows the count i.e 0-4 which is half way to where i want to go. but i can not work out how to do the reference keys.

I have followed the tutorial and i sort of undertand whats going on but i can not get it to work at all.

I have tried everything and each time i get more confused.

:confused:

lux
12-17-2007, 09:56 AM
hint: $row['col_name']

cocoonfx
12-17-2007, 08:15 PM
Hello Guys

Thank you for you assistance but i have had to go to another forum to see if i could get some more advice and also to the to a SQL programmer from my 9-5 job today and both the forum and the programmer came up with the code below.

$query=mysql_query("SELECT COUNT(*) FROM tips");
$row=mysql_fetch_row($query);

$rand = rand(0,$row[0]);

$query=mysql_query("SELECT * FROM tips where tip_id = '$rand'");
$row= mysql_fetch_object($query);echo ($row->tip);

-------

I am going to make it a mission to understand arrays the dj tutorial is good but i think i need something close to what i was trying to acheive to understand it better.

davidj
12-17-2007, 08:28 PM
who gave you this?

$query=mysql_query("SELECT COUNT(*) FROM tips");
$row=mysql_fetch_row($query);

$rand = rand(0,$row[0]);
$query=mysql_query("SELECT * FROM tips where tip_id = '$rand'");
$row= mysql_fetch_object($query);echo ($row->tip);

querying the db twice is uncalled for

davidj
12-17-2007, 08:32 PM
will sort out a better method tomorrow morning

davidj
12-17-2007, 08:47 PM
please try this in your SQL Query (only the one)

i have not tested this so dont blame me if its no good but its worth a shot

select * from table order by rand() limit 1

cocoonfx
12-17-2007, 11:59 PM
Some german gave me the code.....

(it didn't work!) I like your fight spirit and us british look to get the optimal results the quickest way....

Having project managed German programmers for the last 2 years i have found the saying that german's are "super efficent" is a load of b*ll**ks..... classic example budget for 4 servers for optimal speed for unlimited user (1000+) the Germans cut it down to 1 saying they can make the 1 go faster.... which means instead of 1000+ users being able to use the system just 250 can twice as slow as the current system........

Sorry if this offends any germans :lol:

lux
12-18-2007, 10:12 AM
change col_name to the name of your db table col which holds the quotes you want to display.

let me know if you have any questions about the code


//query the database to return all quotes
$query = pg_query("select * from tips");

//create an array to store the quotes
$quotes = array();

//loop through the retrieved db values from the db query and store the values in the $quoates array
while($result = pg_fetch_assoc($query)){
$quotes[] = $result['col_name'];
}

//find out how many quotes there are
$total = sizeof($quotes);

//use the rand function to pick one of the indexes from the $quotes array
$nmbr = (rand(0,($total-1)));

//specify using the $nmbr variable which of the quotes to use and assign its value to $quote
$quote = $quotes[$nmbr];

echo $quote;

davidj
12-18-2007, 01:38 PM
nice one lux

Ehugg
01-17-2008, 01:49 AM
Would this be possible with images?

davidj
01-17-2008, 08:51 AM
yes you can use that method to display any random thingy