User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 397,596 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 2,828 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: 671 | Replies: 5 | Solved
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Display random image from database and...

  #1  
Mar 16th, 2008
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:

  1. <?php
  2. $get_pic = "SELECT car_name, LEFT(car_desc, 20), car_image FROM cars";
  3. $get_pic_res = mysql_query($get_pic) or die(mysql_error());
  4.  
  5. if (mysql_num_rows($get_pic_res) < 1){
  6. $display_desc = "<p><em>No products available!</em></p>";
  7. }
  8. else{
  9. while ($pics = mysql_fetch_array($get_pic_res)){
  10. $pics_name = strtoupper(stripslashes($pics[car_name]));
  11. $pics_desc = stripslashes($pics[car_desc]);
  12. $pics_price = $pics[car_price];
  13.  
  14. $images = array
  15. (
  16. /*array('file' => $pics_name, // here i tried to pass the variable into the array
  17. 'caption' => $pics_desc),*/ // i'm not sure that can be done though
  18. array('file' => 'mazda',
  19. 'caption' => 'This is the Mazda 626. A cheap, yet luxurious car.'),
  20. array('file' => 'buick',
  21. 'caption' => 'This car is all over the country so you may want
  22. to think of getting another car.'),
  23. array('file' => 'hummer',
  24. 'caption' => 'If you have the money fo this car, then you MUST have the money for fuel'),
  25. array('file' => 'xrc',
  26. 'caption' => 'This is a toy car, plain and simple!')
  27. );
  28.  
  29. $i = rand(0, count($images)-1);
  30. $selectedImage = "../images/{$images[$i]['file']}.jpg";
  31. $caption = $images[$i]['caption'];
  32.  
  33. if (file_exists($selectedImage) && is_readable($selectedImage))
  34. {
  35. $imageSize = getimagesize($selectedImage);
  36. }
  37.  
  38. }
  39.  
  40. }
  41.  
  42. ?>
AddThis Social Bookmark Button
Reply With Quote  

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:18 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC