Jump to content

I need help with counting (I guess).


jamesxg1

Recommended Posts

Hiya peeps,

 

I have a mysql database with a record of all the images a customer uploads on my site, these images are displayed on there account. I can get this to work but the problem I'm facing is I need a minimum of 5 images displayed so if they only upload 2 images for instance I need to detect that and add my "No Image Uploaded" image. But if they have uploaded 5 or more I need to just let them be displayed without adding the "No Image Uploaded" picture.

 

Does anyone have any ideas as I'm very stuck!

 

Many thanks

 

James.

Link to comment
Share on other sites

This is the code I use for each image that a customer has uploaded.

$image_return .= '<div class="image"><a title="' . $image['caption'] . '" class="background_light border_medium" onmouseover="this.className=\'background_lightest border_light\'" onmouseout="this.className=\'background_light border_medium\'" href="#"><img src="' . $image['name'] . '" alt="image" width="94" height="94" /></a></div>';

 

And this for the any that they haven't (giving that they have uploaded less than 5 images).

$image_return .= '<div class="image"><a title="' . $image['caption'] . '" class="background_light border_medium" onmouseover="this.className=\'background_lightest border_light\'" onmouseout="this.className=\'background_light border_medium\'" href="#"><img src="noimage.jpg" alt="image" width="94" height="94" /></a></div>';

 

Many thanks,

 

James.

Link to comment
Share on other sites

Ok here's the code I have so far.

function images() {

		$get_images = "SELECT * FROM `images` WHERE `pid` = '$this->pid'";
		$run_get_images = mysql_query($get_images);

		if($run_get_images) {
			$image_num = mysql_num_rows($run_get_images);

			if($image_num >= 5) {
				while($image = mysql_fetch_assoc($run_get_images)) {
					$image_return .= '<div class="image"><a title="' . $image['caption'] . '" class="background_light border_medium" onmouseover="this.className=\'background_lightest border_light\'" onmouseout="this.className=\'background_light border_medium\'" href=""><img src="' . $image['name'] . '" alt="image" width="94" height="94" /></a></div>';
				}
			} elseif($image_num < 5) {
				$real_amount = 5;
				$needed = $real_amount - $image_num;

				for($i=1; $i == $needed; $i++) {
					$image_return .= '<div class="image"><a title="' . $image['caption'] . '" class="background_light border_medium" onmouseover="this.className=\'background_lightest border_light\'" onmouseout="this.className=\'background_light border_medium\'" href="#"><img src="noimage.jpg" alt="image" width="94" height="94" /></a></div>';
				}
			}

			return $image_return;
		} else {
			return false;
		}
	}

 

I have for the for() wrong, as I am not familiar with that function.

 

Many thanks,

 

James.

Link to comment
Share on other sites

I think I have it working :D, just checking if this is full-proof LOL.

	function images() {

		$get_images = "SELECT * FROM `images` WHERE `pid` = '$this->pid'";
		$run_get_images = mysql_query($get_images);

		if($run_get_images) {
			$image_num = mysql_num_rows($run_get_images);

			if($image_num >= 5) {
				while($image = mysql_fetch_assoc($run_get_images)) {
					$image_return .= '<div class="image"><a title="' . $image['caption'] . '" class="background_light border_medium" onmouseover="this.className=\'background_lightest border_light\'" onmouseout="this.className=\'background_light border_medium\'" href=""><img src="' . $image['name'] . '" alt="Image." width="94" height="94" /></a></div>';
				}
			} elseif($image_num < 5) {
				$real_amount = 5;
				$needed = $real_amount - $image_num;

				for ($i = 1; $i <= $needed; $i++) {
					$image_return .= '<div class="image"><a title="No Image Uploaded." class="background_light border_medium" onmouseover="this.className=\'background_lightest border_light\'" onmouseout="this.className=\'background_light border_medium\'" href="#"><img src="noimage.jpg" alt="No Image Uploaded." width="94" height="94" /></a></div>';
				}
			}

			return $image_return;
		} else {
			return false;
		}
	}

 

Many thanks,

 

James.

Link to comment
Share on other sites

Yea, you're sort of right there. You'll want to use the less than or equal to operator (<=) rather than equal to (==) on this line.

for($i=1; $i == $needed; $i++) {

 

EDIT: Yup you fixed it :)

 

Thank you very much for your help wildteen88! Much appreciated!

 

Many thanks,

 

James.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.