•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,846 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,771 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 800 | Replies: 5 | Solved
![]() |
| |
•
•
Join Date: May 2006
Posts: 69
Reputation:
Rep Power: 3
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
$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:
Rep Power: 3
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 10:55 pm.
use as
SELECT car_name, LEFT(car_desc, 20) as car_desc, car_image FROM cars ORDER BY RAND()
Hunters Never Hurt
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
age amd avatar backup blue gene breach business chips daniweb data data protection database development dos economy energy enterprise europe government hacker hardware hp ibm ibm. news intel ibm it linux medicine memory microsoft news open source openoffice pc ps3 recession red hat russia security server sql sun supercomputer supercomputing survey technology trends ubuntu working x86
- pop ups are back (Viruses, Spyware and other Nasties)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- 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


Hybrid Mode