[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 10-28-2005, 01:12 PM   #1 (permalink)
NamePros Member
 
SquireQuack's Avatar
 
Join Date: Aug 2005
Posts: 125
525.30 NP$ (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!
__________________
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  
Old 10-28-2005, 01:47 PM   #2 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (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
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 10-28-2005, 05:56 PM   #3 (permalink)
Domains my Dominion
 
sdsinc's Avatar
 
Join Date: Aug 2005
Location: Web 1.0
Posts: 6,284
1,095.94 NP$ (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 Animal Rescue Wildlife Breast Cancer
Easy:

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

and you fetch one random record at a time
__________________
Buy now - MassDeveloper.com $500
sdsinc is offline  
Old 10-28-2005, 08:20 PM   #4 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (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?
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 10-28-2005, 08:31 PM   #5 (permalink)
Senior Member
 
luxinterior's Avatar
 
Join Date: Jul 2004
Location: Kizmiaz
Posts: 1,091
638.00 NP$ (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


Quote:
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  
Old 10-29-2005, 07:15 AM   #6 (permalink)
Domains my Dominion
 
sdsinc's Avatar
 
Join Date: Aug 2005
Location: Web 1.0
Posts: 6,284
1,095.94 NP$ (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 Animal Rescue Wildlife Breast Cancer
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.
__________________
Buy now - MassDeveloper.com $500
sdsinc is offline  
Old 10-30-2005, 08:31 AM   #7 (permalink)
tgo
NamePros Regular
 
Join Date: Aug 2005
Posts: 317
103.75 NP$ (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-n...ws-from-table/

Last edited by tgo; 10-30-2005 at 08:39 AM.
tgo is offline  
Old 10-30-2005, 06:56 PM   #8 (permalink)
NamePros Member
 
SquireQuack's Avatar
 
Join Date: Aug 2005
Posts: 125
525.30 NP$ (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  
Old 10-30-2005, 07:26 PM   #9 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (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.
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 10-31-2005, 05:50 AM   #10 (permalink)
NamePros Member
 
SquireQuack's Avatar
 
Join Date: Aug 2005
Posts: 125
525.30 NP$ (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  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 02:02 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85