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 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
Reply
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  
Join Date: Jun 2007
Posts: 53
Reputation: naju is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
naju's Avatar
naju naju is offline Offline
Junior Poster in Training

Help Re: Display random image from database and...

  #2  
Mar 16th, 2008
how about selecting the random records from the database itself?
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
Reply With Quote  
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

Re: Display random image from database and...

  #3  
Mar 16th, 2008
ok i will go try it out adn i will update you in a few minutes
Reply With Quote  
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

Re: Display random image from database and...

  #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 10:55 pm.
Reply With Quote  
Join Date: Jun 2007
Posts: 53
Reputation: naju is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
naju's Avatar
naju naju is offline Offline
Junior Poster in Training

Re: Display random image from database and...

  #5  
Mar 16th, 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  
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

Re: Display random image from database and...

  #6  
Mar 16th, 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  
Reply

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

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

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

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