random image from table

Reply

Join Date: May 2009
Posts: 14
Reputation: sanjaypandit is an unknown quantity at this point 
Solved Threads: 0
sanjaypandit sanjaypandit is offline Offline
Newbie Poster

random image from table

 
0
  #1
29 Days Ago
hi friends,

again a problem

I have a mysql table name tbl_admin fields are below

id(int), image1(text), image2 (text) , image3 (text)


now i want to show these pics, i use randomly function to display them

  1. $query=select * from tbl_admin where id=5;
  2. $result=mysql_query($query);
  3. $row=mysql_fetch_array($result)

now for show i use

  1. <img src="<? rand($row['image1'],$row['image2'],$row['image3'])">

Problem occur when image1 or image2 or image3 is blank

than sometime image show blank,,, but i don't want to show up blank, if there is only 2 images then show only 2 images randomly, if there is only one show only one


Thanks
Last edited by sanjaypandit; 29 Days Ago at 9:06 am.
SanjayPandit
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
0
  #2
29 Days Ago
Why not use the RAND function of MySQL?
  1. $query = "SELECT * FROM tbl_admin WHERE id=5 ORDER BY RAND();
  2.  
Also, if you only want one image at a time, you should add LIMIT 1 to the end of the query.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 539
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 63
network18 network18 is offline Offline
Posting Pro

order by rand() won't work here

 
0
  #3
28 Days Ago
Originally Posted by Will Gresham View Post
Why not use the RAND function of MySQL?
  1. $query = "SELECT * FROM tbl_admin WHERE id=5 ORDER BY RAND();
  2.  
Also, if you only want one image at a time, you should add LIMIT 1 to the end of the query.
Ordering by rand() won't help here, in fact it would do nothing to the query, as the query will return only one record at a time and ordering by rand() won't change actually the order, by which the image columns will appear in the result set, at random.
Edit: These images are present in the single record and one of them can be blank too
Last edited by network18; 28 Days Ago at 3:32 am.
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 539
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 63
network18 network18 is offline Offline
Posting Pro
 
0
  #4
28 Days Ago
Try what i could think of ( a very dirt code actually !!) below -
  1. <?
  2. if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) )
  3. {
  4. $img = rand($row['image1'],$row['image2'],$row['image3']);
  5. }
  6. else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL ))
  7. {
  8. $img = rand($row['image1'],$row['image2']);
  9. }
  10. else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image2'] =='' && $row['image2'] ==NULL ))
  11. {
  12. $img = rand($row['image1'],$row['image3']);
  13. }
  14. else if(($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL ))
  15. {
  16. $img = rand($row['image2'],$row['image3']);
  17. }
  18. //This "else if" you can be comment safely , if you are pretty sure all image will never be blank
  19. else if(($row['image2'] =='' && $row['image2'] ==NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL ))
  20. {
  21. $img = '';
  22. }
  23.  
  24. ?>
  25. <img src="<? echo $img; ?>">
Its very flexible code an should produce the non-blank image each time at random.
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 184
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 19
venkat0904's Avatar
venkat0904 venkat0904 is offline Offline
Junior Poster
 
0
  #5
28 Days Ago
this will work fine for now bt suppose in near future he needs to do the same with 4 images or more... this code becomes useless then... it will increase exponentially
i cant think of somethin better too but this i dont feel is a practical solution

Originally Posted by network18 View Post
Try what i could think of ( a very dirt code actually !!) below -
  1. <?
  2. if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) )
  3. {
  4. $img = rand($row['image1'],$row['image2'],$row['image3']);
  5. }
  6. else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL ))
  7. {
  8. $img = rand($row['image1'],$row['image2']);
  9. }
  10. else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image2'] =='' && $row['image2'] ==NULL ))
  11. {
  12. $img = rand($row['image1'],$row['image3']);
  13. }
  14. else if(($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL ))
  15. {
  16. $img = rand($row['image2'],$row['image3']);
  17. }
  18. //This "else if" you can be comment safely , if you are pretty sure all image will never be blank
  19. else if(($row['image2'] =='' && $row['image2'] ==NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL ))
  20. {
  21. $img = '';
  22. }
  23.  
  24. ?>
  25. <img src="<? echo $img; ?>">
Its very flexible code an should produce the non-blank image each time at random.
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 539
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 63
network18 network18 is offline Offline
Posting Pro
 
0
  #6
27 Days Ago
I know that and the number of images wont increase until the columns in the table increase.
The more practicle solution will be to create the altogether seperate table for storing the images for each id , if we know they are going to increase, so that it woks fine for any number of images.Just need to take care when you insert the particular record in the table.
  1. create table images(
  2. image_id int(2) NOT NULL AUTO_INCREMENT,
  3. image varchar(60) COLLATE utf8_bin DEFAULT NULL,
  4. id int(2) ,
  5. entry_date datetime DEFAULT NULL,
  6. PRIMARY KEY (`image_id`)
  7. )
So in short there will be 3 entries for the single record i there are 3 images for that particular id.
And now here the "order by rand()" will be of our use.
  1. <?
  2. $q = "select image_id,image_name from images where id='".$id."' and image is not null order by rand()";
?>
Last edited by network18; 27 Days Ago at 5:13 am.
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Reply

Tags
image, random

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC