944,198 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1360
  • PHP RSS
Nov 5th, 2009
0

random image from table

Expand Post »
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

php Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  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; Nov 5th, 2009 at 9:06 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanjaypandit is offline Offline
23 posts
since May 2009
Nov 5th, 2009
0
Re: random image from table
Why not use the RAND function of MySQL?
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Nov 7th, 2009
0

order by rand() won't work here

Why not use the RAND function of MySQL?
PHP Syntax (Toggle Plain Text)
  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; Nov 7th, 2009 at 3:32 am.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Nov 7th, 2009
0
Re: random image from table
Try what i could think of ( a very dirt code actually !!) below -
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Nov 7th, 2009
0
Re: random image from table
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

Click to Expand / Collapse  Quote originally posted by network18 ...
Try what i could think of ( a very dirt code actually !!) below -
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 13
Solved Threads: 21
Junior Poster
venkat0904 is offline Offline
186 posts
since Oct 2009
Nov 7th, 2009
0
Re: random image from table
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.
PHP Syntax (Toggle Plain Text)
  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.
PHP Syntax (Toggle Plain Text)
  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; Nov 7th, 2009 at 5:13 am.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Error
Next Thread in PHP Forum Timeline: create playlist for audio player ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC