| | |
random image from table
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2009
Posts: 14
Reputation:
Solved Threads: 0
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
now for show i use
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
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)
$query=select * from tbl_admin where id=5; $result=mysql_query($query); $row=mysql_fetch_array($result)
now for show i use
PHP Syntax (Toggle Plain Text)
<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
0
#2 29 Days Ago
Why not use the RAND function of MySQL?
Also, if you only want one image at a time, you should add
PHP Syntax (Toggle Plain Text)
$query = "SELECT * FROM tbl_admin WHERE id=5 ORDER BY RAND();
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.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Sep 2009
Posts: 539
Reputation:
Solved Threads: 63
•
•
•
•
Why not use the RAND function of MySQL?
Also, if you only want one image at a time, you should addPHP Syntax (Toggle Plain Text)
$query = "SELECT * FROM tbl_admin WHERE id=5 ORDER BY RAND();LIMIT 1to the end of the query.
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
follow me on twitter
•
•
Join Date: Sep 2009
Posts: 539
Reputation:
Solved Threads: 63
0
#4 28 Days Ago
Try what i could think of ( a very dirt code actually !!) below -
Its very flexible code an should produce the non-blank image each time at random.
PHP Syntax (Toggle Plain Text)
<? if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) ) { $img = rand($row['image1'],$row['image2'],$row['image3']); } else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL )) { $img = rand($row['image1'],$row['image2']); } else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image2'] =='' && $row['image2'] ==NULL )) { $img = rand($row['image1'],$row['image3']); } else if(($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL )) { $img = rand($row['image2'],$row['image3']); } //This "else if" you can be comment safely , if you are pretty sure all image will never be blank else if(($row['image2'] =='' && $row['image2'] ==NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL )) { $img = ''; } ?> <img src="<? echo $img; ?>">
"The discipline of writing something down is the first step towards making it happen."
follow me on twitter
follow me on twitter
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
i cant think of somethin better too
but this i dont feel is a practical solution•
•
•
•
Try what i could think of ( a very dirt code actually !!) below -
Its very flexible code an should produce the non-blank image each time at random.PHP Syntax (Toggle Plain Text)
<? if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) ) { $img = rand($row['image1'],$row['image2'],$row['image3']); } else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL )) { $img = rand($row['image1'],$row['image2']); } else if(($row['image1'] !='' && $row['image1'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image2'] =='' && $row['image2'] ==NULL )) { $img = rand($row['image1'],$row['image3']); } else if(($row['image2'] !='' && $row['image2'] !=NULL ) && ($row['image3'] !='' && $row['image3'] !=NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL )) { $img = rand($row['image2'],$row['image3']); } //This "else if" you can be comment safely , if you are pretty sure all image will never be blank else if(($row['image2'] =='' && $row['image2'] ==NULL ) && ($row['image3'] =='' && $row['image3'] ==NULL ) && ($row['image1'] =='' && $row['image1'] ==NULL )) { $img = ''; } ?> <img src="<? echo $img; ?>">
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
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
•
•
Join Date: Sep 2009
Posts: 539
Reputation:
Solved Threads: 63
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.
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.
?>
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)
create table images( image_id int(2) NOT NULL AUTO_INCREMENT, image varchar(60) COLLATE utf8_bin DEFAULT NULL, id int(2) , entry_date datetime DEFAULT NULL, PRIMARY KEY (`image_id`) )
And now here the "order by rand()" will be of our use.
PHP Syntax (Toggle Plain Text)
<? $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
follow me on twitter
![]() |
Similar Threads
- display random images from folder (PHP)
- dynaically show image and random image (VB.NET)
- Javascript random image with assigned/accompanying quote (JavaScript / DHTML / AJAX)
- Random Image (Java)
- Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random (C++)
- Random image with text and link (JavaScript / DHTML / AJAX)
- creating a image table in sql and retriveing image in data grid (C#)
- Random Image linking help (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: Error
- Next Thread: create playlist for audio player ?
| Thread Tools | Search this Thread |
animation api applet array back backup bmp broken button byte clone cloning code delete directory disk displayimageinsteadofflash drive dynamic file filename flash ftp function gdi generator ghost google hard header hosting image images java jpanel jpeg link loops matrix mediawiki method multimedia mysql number open panel php picture picturebox problem python random reading reputationmanagement screen scroll search shot subdomain swf swf. swing transfer upload url variable web website whileloop windows






