| | |
Display random image from database and...
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2006
Posts: 69
Reputation:
Solved Threads: 1
hi, i am trying to display a random product from my database along with its description, price and image. but i am having a little trouble. i can display a random product and its info by directly inputting it into an array, but i can't call it from the database itself WITH the array.
i checked google but i just kept being sent to pages that only have scripts that do as i did below or i get taken to pages that have nothing to do with php at all.
Any help will be appreciated.
Here's the code i'm using:
i checked google but i just kept being sent to pages that only have scripts that do as i did below or i get taken to pages that have nothing to do with php at all.
Any help will be appreciated.
Here's the code i'm using:
php Syntax (Toggle Plain Text)
<?php $get_pic = "SELECT car_name, LEFT(car_desc, 20), car_image FROM cars"; $get_pic_res = mysql_query($get_pic) or die(mysql_error()); if (mysql_num_rows($get_pic_res) < 1){ $display_desc = "<p><em>No products available!</em></p>"; } else{ while ($pics = mysql_fetch_array($get_pic_res)){ $pics_name = strtoupper(stripslashes($pics[car_name])); $pics_desc = stripslashes($pics[car_desc]); $pics_price = $pics[car_price]; $images = array ( /*array('file' => $pics_name, // here i tried to pass the variable into the array 'caption' => $pics_desc),*/ // i'm not sure that can be done though array('file' => 'mazda', 'caption' => 'This is the Mazda 626. A cheap, yet luxurious car.'), array('file' => 'buick', 'caption' => 'This car is all over the country so you may want to think of getting another car.'), array('file' => 'hummer', 'caption' => 'If you have the money fo this car, then you MUST have the money for fuel'), array('file' => 'xrc', 'caption' => 'This is a toy car, plain and simple!') ); $i = rand(0, count($images)-1); $selectedImage = "../images/{$images[$i]['file']}.jpg"; $caption = $images[$i]['caption']; if (file_exists($selectedImage) && is_readable($selectedImage)) { $imageSize = getimagesize($selectedImage); } } } ?>
how about selecting the random records from the database itself?
if its fine then here is your req sql
this will randomize the order of records while fetching
if its fine then here is your req sql
PHP Syntax (Toggle Plain Text)
$get_pic = "SELECT car_name, LEFT(car_desc, 20), car_image FROM cars ORDER BY RAND()";
this will randomize the order of records while fetching
Hunters Never Hurt
•
•
Join Date: May 2006
Posts: 69
Reputation:
Solved Threads: 1
ok i've tried it out and it works perfectly, thanks! i just have one slight problem though, when i use
SELECT car_name, LEFT(car_desc, 20), car_image FROM cars ORDER BY RAND() the description doesnt get displayed under the image, but using SELECT car_name, car_desc, car_image FROM cars ORDER BY RAND() it gets displayed. any ideas? i will keep trying to figure it out though Last edited by mikki2; Mar 16th, 2008 at 11:55 pm.
use as
SELECT car_name, LEFT(car_desc, 20) as car_desc, car_image FROM cars ORDER BY RAND() Hunters Never Hurt
![]() |
Similar Threads
- pop ups are back (Viruses, Spyware and other Nasties)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- SWF adrotator Component for ASP.NET 2.0 (ASP.NET)
- blank pop-ups (Viruses, Spyware and other Nasties)
- Image Verification in Asp.net (ASP.NET)
- I lack focus... (Java)
- Random text display with image, link (PHP)
- image help (PHP)
Other Threads in the PHP Forum
- Previous Thread: Where to get statistics for PHPLD?
- Next Thread: Sending pic attachments via PHP form
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql navigation number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white xml youtube





