Display random image from database and...

Thread Solved

Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Display random image from database and...

 
0
  #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. ?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 57
Reputation: naju is an unknown quantity at this point 
Solved Threads: 2
naju's Avatar
naju naju is offline Offline
Junior Poster in Training

Re: Display random image from database and...

 
0
  #2
Mar 16th, 2008
how about selecting the random records from the database itself?
if its fine then here is your req sql

  1. $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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Display random image from database and...

 
0
  #3
Mar 16th, 2008
ok i will go try it out adn i will update you in a few minutes
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Display random image from database and...

 
0
  #4
Mar 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 57
Reputation: naju is an unknown quantity at this point 
Solved Threads: 2
naju's Avatar
naju naju is offline Offline
Junior Poster in Training

Re: Display random image from database and...

 
0
  #5
Mar 17th, 2008
use as
SELECT car_name, LEFT(car_desc, 20) as car_desc, car_image FROM cars ORDER BY RAND()
Hunters Never Hurt
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Display random image from database and...

 
0
  #6
Mar 17th, 2008
thanks a lot!!! the funny thing is that i know most the functions and their uses but i have a problem with implementation. what book(s) do(did) you use when studying php/mysql?
thanks again
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC