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

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

now for show i use

<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

Why not use the RAND function of MySQL?

$query = "SELECT * FROM tbl_admin WHERE id=5 ORDER BY RAND();

Also, if you only want one image at a time, you should add LIMIT 1 to the end of the query.

commented: Great! +6

Why not use the RAND function of MySQL?

$query = "SELECT * FROM tbl_admin WHERE id=5 ORDER BY RAND();

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

Try what i could think of ( a very dirt code actually !!) below -

<? 
	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; ?>">

Its very flexible code an should produce the non-blank image each time at random.

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

Try what i could think of ( a very dirt code actually !!) below -

<? 
	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; ?>">

Its very flexible code an should produce the non-blank image each time at random.

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.

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`)
)

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.

<?
$q = "select image_id,image_name from images where id='".$id."' and image is not null order by rand()";

?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.