[advanced search]
Results from the most recent live auction are here.
17 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming
User Name
Password

Old 10-28-2005, 01:12 PM   · #1
SquireQuack
NamePros Member
 
SquireQuack's Avatar
 
Trader Rating: (5)
Join Date: Aug 2005
Posts: 125
NP$: 512.00 (Donate)
SquireQuack is on a distinguished road
Randomingly choosing a quote from Database, PHP

I just started PHP and I'm still trying to figure somethings out.

Could anyone provide a code for randomly choosing and displaying a quote from a database (two columns: quote, author)?

Thanks!


Please register or log-in into NamePros to hide ads
__________________
upload² |AIMForum.com - Ready, AIM, Chat!|Google Talk Forum - Where the world talks about Google!
Forum Elves - Quality Forum Posting At Affordable Prices! -|Order A Package|PaidPosts.com Help Boost Your Forum|BannerManage.com Sell direct advertisements on your website effortlessly!
SquireQuack is offline   Reply With Quote
Old 10-28-2005, 01:47 PM   · #2
mholt
DNOA Member
 
Name: Matthew Holt
Location: 127.0.0.1
Trader Rating: (75)
Join Date: May 2004
Posts: 4,867
NP$: 17.21 (Donate)
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
I actually wrote one for my old website (www.vibewave.com) that retrieves a random quote...

Here:

PHP Code:
//get a random quote... using arrays.

//Be sure to connect to the db.
$query1 = "SELECT * FROM quotes";
$result1 = mysql_query($query1);
while (
$row1 = mysql_fetch_array($result1))
{
    
$id1 = $row1['id'];
    
$array1[count($array1) + 1] = $id1;
    
    
shuffle($array1);
}
shuffle($array1);

//ACCESS THE RANDOM NUMBER IS BY $array1[0]
//Now we have the id of the random quote.

$random = $array1[0];

$q1 = "SELECT * FROM quotes WHERE id='$random' LIMIT 1";
$r1 = mysql_query($q1);

while (
$row1 = mysql_fetch_array($r1))
{
    
$content = $row1['content'];
    
$author = stripslashes($row1['author']);
}

//Then just echo $content and $author (the person who said it) when you need it.
?>


It may not be efficient but it works
__________________
FREE: Help With Code

Includes other technical topics:
programming, development, Windows, domain names, and Internet
mholt is offline   Reply With Quote
Old 10-28-2005, 05:56 PM   · #3
sdsinc
Law and disorder
 
sdsinc's Avatar
 
Name: Kate
Location: Expat
Trader Rating: (57)
Join Date: Aug 2005
Posts: 5,133
NP$: 895.11 (Donate)
sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute
Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV
Easy:

select [field] from [table]
order by rand()
limit 1

and you fetch one random record at a time
sdsinc is offline   Reply With Quote
Old 10-28-2005, 08:20 PM   · #4
mholt
DNOA Member
 
Name: Matthew Holt
Location: 127.0.0.1
Trader Rating: (75)
Join Date: May 2004
Posts: 4,867
NP$: 17.21 (Donate)
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
Unfortunately, wouldn't rand() return any number, not just those in the db?
__________________
FREE: Help With Code

Includes other technical topics:
programming, development, Windows, domain names, and Internet
mholt is offline   Reply With Quote
Old 10-28-2005, 08:31 PM   · #5
luxinterior
Senior Member
 
luxinterior's Avatar
 
Name: Lux Interior
Location: Kizmiaz
Trader Rating: (31)
Join Date: Jul 2004
Posts: 1,090
NP$: 637.00 (Donate)
luxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of light
Originally Posted by compuXP
Unfortunately, wouldn't rand() return any number, not just those in the db?



No it wouldn't and yes that is the correct way to do it.

Lux
luxinterior is offline   Reply With Quote
Old 10-29-2005, 07:15 AM   · #6
sdsinc
Law and disorder
 
sdsinc's Avatar
 
Name: Kate
Location: Expat
Trader Rating: (57)
Join Date: Aug 2005
Posts: 5,133
NP$: 895.11 (Donate)
sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute
Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV
Order by Rand() just means "order the resultset in random order" (instead of ASC or DESC for example) so you won't get a record ID which is not part of the resultset.
sdsinc is offline   Reply With Quote
Old 10-30-2005, 08:31 AM   · #7
tgo
NamePros Regular
 
Trader Rating: (3)
Join Date: Aug 2005
Posts: 317
NP$: 102.00 (Donate)
tgo is on a distinguished road
Rand is not the best way but it works.

If its a small database then go for it, but if its more than 100 records dont use that way, the reason is ORDER BY RAND() forces Mysql to generate alot of random numbers.

Quote from below resource
Quote:
What happens when you run such a query? Let’s say you run this query on a table with 10000 rows, than the SQL server generates 10000 random numbers, scans this numbers for the smallest one and gives you this row. Generating random numbers is relatively expensive operation, scaning them for the lowest one (if you have LIMIT 10, it will need to find 10 smallest numbers) is also not so fast (if quote is text it’s slower, if it’s something with fixed size it is faster, maybe because of need to create temporary table).



I code ASP and dont code PHP so I cant give you any code but I think you would be better off with this type of thing

SELECT COUNT(*) FROM DBNAME
get the total records here
Close recordset

calculate your random number between 1 and the total records.
I dont know PHP dont take this literaly
srand((double)microtime()*1000000);
$iRandom = rand(0,$totalrecords);

Now that you have a random number you can use the LIMIT in the Query properly.

SELECT [field] FROM [table] LIMIT $randomvariable, 1

The above limits the rows to one, and starts at the random number

Please dont cut paste my code here as i stated I code ASP and my PHP here is most likely wrong.

For more info on this
http://www.titov.net/2005/09/21/do-...ows-from-table/

Last edited by tgo : 10-30-2005 at 08:39 AM.
tgo is offline   Reply With Quote
Old 10-30-2005, 06:56 PM   · #8
SquireQuack
NamePros Member
 
SquireQuack's Avatar
 
Trader Rating: (5)
Join Date: Aug 2005
Posts: 125
NP$: 512.00 (Donate)
SquireQuack is on a distinguished road
Quote:
PHP Code:
//get a random quote... using arrays.

//Be sure to connect to the db.
$query1 = "SELECT * FROM quotes";
$result1 = mysql_query($query1);
while (
$row1 = mysql_fetch_array($result1))
{
    
$id1 = $row1['id'];
    
$array1[count($array1) + 1] = $id1;
    
    
shuffle($array1);
}
shuffle($array1);

//ACCESS THE RANDOM NUMBER IS BY $array1[0]
//Now we have the id of the random quote.

$random = $array1[0];

$q1 = "SELECT * FROM quotes WHERE id='$random' LIMIT 1";
$r1 = mysql_query($q1);

while (
$row1 = mysql_fetch_array($r1))
{
    
$content = $row1['content'];
    
$author = stripslashes($row1['author']);
}

//Then just echo $content and $author (the person who said it) when you need it.
?>


It may not be efficient but it works



it's returning this error aoubt how in
PHP Code:
while ($row1 = mysql_fetch_array($r1))

$r1 is not a valid MySQL resource for mysql_fetch_array

I'm sure I did everything right (including connecting to the database). To prove it: when I replace
PHP Code:
$q1 = "SELECT * FROM quotes WHERE id='$random' LIMIT 1";

with
PHP Code:
$q1 = "SELECT * FROM quotes WHERE 1 LIMIT 1";

It returns with my first quote.

Just wondering...how do I fix the error? Thanks!
__________________
upload² |AIMForum.com - Ready, AIM, Chat!|Google Talk Forum - Where the world talks about Google!
Forum Elves - Quality Forum Posting At Affordable Prices! -|Order A Package|PaidPosts.com Help Boost Your Forum|BannerManage.com Sell direct advertisements on your website effortlessly!
SquireQuack is offline   Reply With Quote
Old 10-30-2005, 07:26 PM   · #9
mholt
DNOA Member
 
Name: Matthew Holt
Location: 127.0.0.1
Trader Rating: (75)
Join Date: May 2004
Posts: 4,867
NP$: 17.21 (Donate)
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
Apparenlty the variable $random doesn't contain a value, or contains an invalid one. Tweak the script by putting in echo($random); then an exit; statement on the next line. Tell me what shows up.
__________________
FREE: Help With Code

Includes other technical topics:
programming, development, Windows, domain names, and Internet
mholt is offline   Reply With Quote
Old 10-31-2005, 05:50 AM   · #10
SquireQuack
NamePros Member
 
SquireQuack's Avatar
 
Trader Rating: (5)
Join Date: Aug 2005
Posts: 125
NP$: 512.00 (Donate)
SquireQuack is on a distinguished road
Thanks for you help compuXP!

the script I used:
PHP Code:
$q1 = "SELECT * FROM quotes ORDER BY rand() LIMIT 1";
$r1 = mysql_query($q1);

$row1 = mysql_fetch_array($r1);
$content = $row1['quote'];
$author = stripslashes($row1['author']);


echo
"<font size=\"-1\"><i>".$content."</i><br>";
echo
"<div align=right><b>".$author."</b></div></font>";
__________________
upload² |AIMForum.com - Ready, AIM, Chat!|Google Talk Forum - Where the world talks about Google!
Forum Elves - Quality Forum Posting At Affordable Prices! -|Order A Package|PaidPosts.com Help Boost Your Forum|BannerManage.com Sell direct advertisements on your website effortlessly!
SquireQuack is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
http://www.mobisitetrader.com/ domainsubway.com Buy Flash Arcade Game Script
Advertise your business at NamePros
All times are GMT -7. The time now is 02:18 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0