| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| NamePros Member | 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! |
| |
| | #2 (permalink) |
| DNOA Member | I actually wrote one for my old website (www.vibewave.com) that retrieves a random quote... Here: PHP Code:
__________________ codeboards A high-quality community of programmers -- Join today and post! We want new members! |
| |
| | #4 (permalink) |
| DNOA Member | 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! |
| |
| | #5 (permalink) | |
| Senior Member | Quote:
Lux
__________________ 1 Cent Web Hosting @ Online HostGatorVoucher.com Adsense Hack for Vbulletin - Fully 3.6 Compatible ForexArea.com Free E-Course in Programming EA's | |
| |
| | #7 (permalink) | |
| NamePros Regular | 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:
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. | |
| |
| | #8 (permalink) | |
| NamePros Member | Quote:
PHP Code: I'm sure I did everything right (including connecting to the database). To prove it: when I replace PHP Code: PHP Code: 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! | |
| |
| | #9 (permalink) |
| DNOA Member | 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! |
| |
| | #10 (permalink) |
| NamePros Member | Thanks for you help compuXP! the script I used: PHP Code:
__________________ 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! |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |