Jump to content

looping a mysql_fetch_array for existing $var. push in the right direction neede


Recommended Posts

I have this script that displays the number of input fields depending on the number of images already uploaded to a certain ID. It's a multi-upload script. Now when I pass the variables through the process.php file, I'm trying for it to look into the database and directory, and see if a file with that name already exist. and If the files with that certain name exists it'll add +1 to the [key] value that has been passed, for each of  the uploaded images.

 

My images are named, depending on it's upload [key], and the ID so f.e. :

$name = "HID".$id."_img".$nkey.".jpg";
where $nkey = $key +1;

 

here's my scripts:

<?php 
include 'dbcon.php';

$id = $_POST['id'];

$sql = "SELECT * FROM house WHERE id='".$id."'";
$result = mysql_query($sql) or die("Could not connect");

$row = mysql_fetch_array($result);

echo "<p>The following pictures have been uploaded to the profile of <b>".$row['name']." ".$row['lastn']. "</b></p>";

foreach ( $_FILES["pictures"]["error"] as $key => $error ) 
	{
		if ( $error == UPLOAD_ERR_OK ) 
			{

				$img = $_FILES["pictures"]["tmp_name"][$key];					
				$name = $_FILES["pictures"]["name"][$key];
				$path = "upload/$name";

				// Giving the uploaded picture a new name based on ID and $key number

				$epl = explode(".", $name);
				$nkey = $key + 1;
				$nname = "HID".$id."_img".$nkey.".".$epl[1];

				// Resizing the image 

				$src = imagecreatefromjpeg($img);
				$width = imagesx($src);
				$height= imagesy($src);
				$x = 600;
				$y = ($height/$width) * 600;
				$dst = imagecreatetruecolor($x,$y);
				imagecopyresampled($dst,$src,0,0,0,0,$x,$y,$width,$height);

				// Retrieve data to see if a file with that name already exists 

/* Now this is a far as I can get without getting confused all in the script */ 

				sql2 = "SELECT * FROM hpics WHERE id='".$id."'";
				$result2 = mysql_query($sql2) or die("Error while retreiving picture information.");
				$row = mysql_fetch_array($result2);
				$ex_pic = $row['p1'];

				for ($i=1; $i <= 9; $i++ )
					{

					$nkey = $key + 1;

				}

				$nname = "HID".$id."_img".$nkey.".".$epl[1];

/* End confusing part =========================================================================*/
				$file = "upload/$nname";
				imagejpeg($dst,$file);
				echo "<img src=\"".$file."\"><br /><br />";

				$sql3 = "INSERT INTO hpics (id,p1) VALUES ('$id', '$file')";
				$result3 = mysql_query($sql3) or die("Error inserting image path to database");			

		}
	}


?>

 

What I'm trying to do is, loop through all the pictures url in the database and folder, for that 1 image that is being uploaded, and compare the names and see if they already exists. If they do exists add +1 to $nkey, and if the new name with the $nkey exists again, do another +1 to the $nkey. Until if finds a name that doesn't already exist.

 

Is this possible?

If so can someone point me in the right direction. I'm not sure if to use the for(), while(), if() or foreach() function.

 

??? ??? ???

If they do exists add +1 to $nkey, and if the new name with the $nkey exists again, do another +1 to the $nkey. Until if finds a name that doesn't already exist.
Is the important part to start with a certain $nkey and then increment that number. Or do you only need an unique number to make the filename unique?

I'd prefer do start with a certain $nkey and increment it depending on the availability of that name.

They way I'm thinking of doing is that. I have a Max number of 9 images that can be uploaded per ID. Let say e certain profile already has 7 pictures, and and he deletes 1, let sat # 4. If he decides to upload 3 more pictures, they will be named accordingly: pic_4,pic_8,pic_9... Where they script sees that pic_4 is open and names 1 of the images pic_4 again, then jump to _8, _9, since they are still open.

 

 

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.