rearrange multidimensional arrays

Hi,

I got some help rearranging a mutlideimensional array and it work perfectly but i have since gone back and reconfigured the original array which changes the results i get from the rearrange function an expert helped me out with.

i know that there is a count and offset involved but i cannot seem to figure out how it works

the new array looks like this from print_r
Array
(
    [image_name] => Array
        (
            [0] => fb_icon_325x325.png
            [1] => twitter.png
        )

    [image_org] => Array
        (
            [0] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378583752]jaymenagy1-fb_icon_325x325.png
            [1] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378583752]jaymenagy3-twitter.png
        )

    [image_location] => Array
        (
            [0] => calgary
            [1] =>  toronto
        )

    [image_fav] => Array
        (
            [0] => yes
            [1] => no
        )

    [image_desc] => Array
        (
            [0] => calgary photo
            [1] => toronto photo
        )

    [image_submit] => Image Submit
)

Open in new window


the original looked like
Array
(
    [same_info] => Array
        (
          [0] =>  yes/no
        )
    [image_name] => Array
        (
            [0] => fb_icon_325x325.png
            [1] => twitter.png
        )

    [image_org] => Array
        (
            [0] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378583752]jaymenagy1-fb_icon_325x325.png
            [1] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378583752]jaymenagy3-twitter.png
        )

    [image_location] => Array
        (
            [0] => calgary
            [1] => toronto
        )

    [image_fav] => Array
        (
            [0] => yes
            [1] => no
        )

    [image_desc] => Array
        (
            [0] => calgary photo
            [1] => toronto photo
        )

    [image_submit] => Image Submit
)

Open in new window


i wanted it to arrange the data into a new array so all the image details pertaining to that single image multiplied by how ever many images are within the array

the expert gave me this function to help me would which worked excellent but since i removed the array of same info which upsets the offset set within the function the expert gave me. i tried playing arround with it but i just cannot seem to get it to work. i can get it so that it displays the results of one image properly but if i have more than one it remains the same

the function is
	//SET NEW VARIABLE FOR ALL POST VALUE UPON FORM SUBMIT
	$arr=$_POST;

	// SAVE A COPY OF THE INITIAL STATE
	$old = $arr;
	
	// REMOVE STRINGS
	foreach ($arr as $key => &$sub)
	{
		if (is_string($sub)) unset($arr[$key]);
	}
	
	// MATRIX-INVERT ARRAYS
	$cnt = 0;
	while (TRUE)
	{
		foreach ($arr as $key => $sub)
		{
			$new[$cnt][$key] = $sub[$cnt];
			unset($arr[$key][$cnt]);
		}
		$cnt++;
		if (empty($arr[$key][$cnt])) break;
	}
	
	// KEEP STRINGS, USE NEW ARRAYS
	$cnt = 0;
	foreach ($old as $key => $sub)
	{
		if (is_string($sub))
		{
			$out[][$key] = $sub;
		}
		elseif (empty($new[$cnt]))
		{
			$out[] = $new[$cnt];
			$cnt++;
		}
	}

Open in new window


where $arr = $_POST; would be the array i would want to rearrange

i have primarily spent my time trying to reconfigure the foreach loop under the $cnt variable but i have little to no success.

any help would be greatly appreciated

the original article is here too for some reference
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_28230037.html
LVL 6
J NUnicorn wranglerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
What is missing here is the test data and the desired output.  A description of the desired output is almost always a time-waster.  If we can see what you want we can usually write the code to create it.

Please run this script and look at the input and output displays, then show us what you want to have different in the output, thanks.
http://www.laprbass.com/RAY_temp_jaymenagy.php

<?php // RAY_temp_jaymenagy.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28233576.html

$arr = array
( "same_info"      => "yes"
, "image_name"     => array( 0 => "gopr0012.jpg",  1 => "photo.jpg"    )
, "image_location" => array( 0 => "calgary",       1 => "canada"       )
, "image_fav"      => array( 0 => "no",            1 => "yes"          )
, "image_desc"     => array( 0 => "description1",  1 => "description2" )
, "image_submit"   => "Image Submit"
)
;

// SHOW THE ORIGINAL DATA
print_r($arr);

// SAVE A COPY OF THE INITIAL STATE
$old = $arr;

// REMOVE STRINGS
foreach ($arr as $key => &$sub)
{
    if (is_string($sub)) unset($arr[$key]);
}

// MATRIX-INVERT ARRAYS
$cnt = 0;
while (TRUE)
{
    foreach ($arr as $key => $sub)
    {
        $new[$cnt][$key] = $sub[$cnt];
        unset($arr[$key][$cnt]);
    }
    $cnt++;
    if (empty($arr[$key][$cnt])) break;
}

// KEEP STRINGS, USE NEW ARRAYS
$cnt = 0;
foreach ($old as $key => $sub)
{
    if (is_string($sub))
    {
        $out[][$key] = $sub;
    }
    elseif (!empty($new[$cnt]))
    {
        $out[] = $new[$cnt];
        $cnt++;
    }
}

// SHOW THE WORK PRODUCT
print_r($out);

Open in new window

0
J NUnicorn wranglerAuthor Commented:
Hi,

The input or test data is the same as this with out exception
original
$arr = array
( "same_info"      => "yes"
, "image_name"     => array( 0 => "gopr0012.jpg",  1 => "photo.jpg"    )
, "image_location" => array( 0 => "calgary",       1 => "canada"       )
, "image_fav"      => array( 0 => "no",            1 => "yes"          )
, "image_desc"     => array( 0 => "description1",  1 => "description2" )
, "image_submit"   => "Image Submit"
)
;

Open in new window


new
$arr = array
( "image_name"     => array( 0 => "gopr0012.jpg",  1 => "photo.jpg"    )
, "image_location" => array( 0 => "calgary",       1 => "canada"       )
, "image_fav"      => array( 0 => "no",            1 => "yes"          )
, "image_desc"     => array( 0 => "description1",  1 => "description2" )
, "image_submit"   => "Image Submit"
)
;

Open in new window

0
Ray PaseurCommented:
Is this right or wrong?  What would you like to see changed?
http://www.laprbass.com/RAY_temp_jaymenagy.php

<?php // RAY_temp_jaymenagy.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28233576.html

$arr = array
( "image_name"     => array( 0 => "gopr0012.jpg",  1 => "photo.jpg"    )
, "image_location" => array( 0 => "calgary",       1 => "canada"       )
, "image_fav"      => array( 0 => "no",            1 => "yes"          )
, "image_desc"     => array( 0 => "description1",  1 => "description2" )
, "image_submit"   => "Image Submit"
)
;

// SHOW THE ORIGINAL DATA
print_r($arr);

// SAVE A COPY OF THE INITIAL STATE
$old = $arr;

// REMOVE STRINGS
foreach ($arr as $key => &$sub)
{
    if (is_string($sub)) unset($arr[$key]);
}

// MATRIX-INVERT ARRAYS
$cnt = 0;
while (TRUE)
{
    foreach ($arr as $key => $sub)
    {
        $new[$cnt][$key] = $sub[$cnt];
        unset($arr[$key][$cnt]);
    }
    $cnt++;
    if (empty($arr[$key][$cnt])) break;
}

// KEEP STRINGS, USE NEW ARRAYS
$cnt = 0;
foreach ($old as $key => $sub)
{
    if (is_string($sub))
    {
        $out[][$key] = $sub;
    }
    elseif (!empty($new[$cnt]))
    {
        $out[] = $new[$cnt];
        $cnt++;
    }
}

// SHOW THE WORK PRODUCT
print_r($out);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

J NUnicorn wranglerAuthor Commented:
stupid spelling mistake they always catch me

thanks a lot
0
J NUnicorn wranglerAuthor Commented:
sorry for wasting your time
0
J NUnicorn wranglerAuthor Commented:
Hi,

actually i tested it again with some fields empty and this is what i get

$_POST data(from print_r)
  [delete_photo] => Array
        (
            [0] => no
            [1] => no
            [2] => no
            [3] => no
        )

    [image_name] => Array
        (
            [0] => analysis.jpg
            [1] => balance.jpg
            [2] => beach ball.jpg
            [3] => binoculars.jpg
        )

    [image_org] => Array
        (
            [0] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy1-analysis.jpg
            [1] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy2-balance.jpg
            [2] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy3-beach ball.jpg
            [3] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy4-binoculars.jpg
        )

    [image_location] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [image_fav] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [image_desc] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [image_submit] => Image Submit
)

Open in new window


$out data(from print_r)
Array
(
    [delete_photo] => no
    [image_name] => analysis.jpg
    [image_org] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy1-analysis.jpg
    [image_location] => 
    [image_fav] => 
    [image_desc] => 
)
Array
(
    [image_submit] => Image Submit
)

Open in new window


some reason is only arranging the first one

  [delete_photo] => Array
        (
            [0] => no
            [1] => no
            [2] => no
            [3] => no
        )

    [image_name] => Array
        (
            [0] => analysis.jpg
            [1] => balance.jpg
            [2] => beach ball.jpg
            [3] => binoculars.jpg
        )

    [image_org] => Array
        (
            [0] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy1-analysis.jpg
            [1] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy2-balance.jpg
            [2] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy3-beach ball.jpg
            [3] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378624838]jaymenagy4-binoculars.jpg
        )

    [image_location] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [image_fav] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [image_desc] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [image_submit] => Image Submit
)

Open in new window


i essentially copied your code and made $arr =$_POST and $_POST is the first array listed
0
Ray PaseurCommented:
It's never a waste of time!  If you're still having trouble with this, here is what we would need to see (the SSCCE)...

1. The form that makes the POST request (a web page link is OK for this)
2. The information that should be filled into the form
3. The desired output from processing the form

Armed with that information, we should be able to show you the logic that would get you the desired output.

Thanks for the points and please post back if you're still having issues with it, ~Ray
0
J NUnicorn wranglerAuthor Commented:
Hi,

thanks Ray as usual you are a big help!!!

i have a script that allows users t0 upload images to a temp folder and then upon completion it redirects the user to a confirm photo page which takes all of the photos with the folder than has "tmp" and the user's username appended to the file name which i did when the user uploads the file to the temp folder.

i then create a post form that scans the directory for all the files with "tmp" and "username" in the file name

the form displays the image and then creates a foreach loop for each photo

the forms is as such
	<form enctype="multipart/form-data" method="post" name="picture_confirm_form" id="picture_confirm_form" action="">	


//HERE IS THE FOREACH LOOP CONTENTS
//CREATE THE FORM INPUTS
			$input ='
				<div class="photo_container">
					<div>
					<input type="hidden" id="delete_photo[]" name="delete_photo[]" value="no" />
						<input type="checkbox" class="myinput large custom" title="delete image" id="delete_photo[]" name="delete_photo[]" value="delete" />
					</div>
					<div class="photo_box">
						<div class="photo">
							<img class="photo" src="'.$clean.'"/>
						</div>
					</div>
					<div class="info_container">
						<div class="info_input">
							Image name:<br />
							<input type="text" id="image_name[]" name="image_name[]" value="'.$image_name.'"/><br />
							<input type="hidden" id="image_name[]" name="image_org[]" value="'.$image_org.'"/><br />
							image date:<br />
							<input type="date" id="image_date[]" name"image_date[]" value="'.$image_date.'"/><br />
							Location photo was taken:<br />
							<input type="text" id=image_location[]" name="image_location[]" value="'.$image_location.'"/><br />
							Add to favoutites?
							<select name="image_fav[]" id="image_fav[]"><br>
								<option id="image_fav[]" name="image_fav[]">'.$image_fav.'</option>
								<option id="image_fav[]" name="image_fav[]">yes</option>
								<option id="image_fav[]" name="image_fav[]">no</option>
							</select>
							Image description:<br />
							<textarea  rows="5" cols="20 id=image_desc[]" name="image_desc[]">'.$image_desc.'</textarea>
						</div>
					</div>
				</div>	
			';
//END FORM
	<div class="clear"></div>
	<div class="image_submit">
		<input type="submit" id="image_submit" name="image_submit" value="Image Submit"/><br />
		</form>
	</div>

Open in new window


the foreach area with duplicate for however may images are present

once the form has created it could be one or twenty images

i originally wanted the user to determine if they wanted to make the settings outlined on the first image universal to all images but then i thought it was kind of redundant as i could just use the post data from the file inputs. so i took that option out. i then realized than when i shortened the file name to get ride of the "tmp" and "username" i could not copy or rename it so i decided to put a hidden field containing the original file path.

essentially what i am trying to do it show all photos uploaded by the user and display them , allow them to update some fields(or not), select whether or not it is a favourite or not, or delete the image.

i do work on the users input and determine how it should be treated whether i delete the image rename it to a differernt folder and copy it to another.

the full script is:
<?php
//SCAN THE DIRECTORY FOR PROPER FILES
$temp_dir=$_SERVER['DOCUME<wbr ></wbr>NT_ROOT'].<wbr ></wbr>"/temp/";
$files= glob("{$temp_dir}*.{jpg,gi<wbr ></wbr>f,png,jpeg<wbr ></wbr>}", GLOB_BRACE);
$website="http://".$_SERVE<wbr ></wbr>R['HTTP_HO<wbr ></wbr>ST'];
$location=$website."/temp/<wbr ></wbr>";
$fav_dir=$_SERVER['DOCUMEN<wbr ></wbr>T_ROOT']."<wbr ></wbr>/gallery/f<wbr ></wbr>avourites/<wbr ></wbr>";
$recent_dir=$_SERVER['DOCU<wbr ></wbr>MENT_ROOT'<wbr ></wbr>]."/galler<wbr ></wbr>y/recent_i<wbr ></wbr>mages/";

//SET UP SOME VARIABLES
$form_submit=empty($_POST[<wbr ></wbr>'image_sub<wbr ></wbr>mit'])?"":<wbr ></wbr>mysqli_rea<wbr ></wbr>l_escape_s<wbr ></wbr>tring($lin<wbr ></wbr>k,($_POST[<wbr ></wbr>'image_sub<wbr ></wbr>mit']));
$username=get_username($_S<wbr ></wbr>ESSION['ui<wbr ></wbr>d']);

//DISPLAY DIRECTIONS
echo '<h7>Confirm and Update Information</h7>';

//START THE FORM
$start_form = '
	<form enctype="multipart/form-da<wbr ></wbr>ta" method="post" name="picture_confirm_form<wbr ></wbr>" id="picture_confirm_form" action="">	

';

//END THE FORM
$end_form= '
	<div class="clear"></div>
	<div class="image_submit">
		<input type="submit" id="image_submit" name="image_submit" value="Image Submit"/><br />
		</form>
	</div>
';

//FORM HAS NOT BEEN SUBMITTED
if(empty($form_submit)){
	echo $start_form;

	//PERFORM WORK ON FILES
	foreach($files as $image){
		
		if(strpos($image,"tmp") && strpos($image,$username)){<wbr ></wbr>
			//REMOVE THE TEMP DIRECTORY FROM NAME
			$clean=str_replace($temp_d<wbr ></wbr>ir,$locati<wbr ></wbr>on,$image)<wbr ></wbr>;
			
			//SET UP VARIABLES
			$image_name=empty($_POST['<wbr ></wbr>image_name<wbr ></wbr>'])?ltrim(<wbr ></wbr>strstr($im<wbr ></wbr>age,"-"),"<wbr ></wbr>-"):mysqli<wbr ></wbr>_real_esca<wbr ></wbr>pe_string(<wbr ></wbr>$link,($_P<wbr ></wbr>OST['image<wbr ></wbr>_name']));<wbr ></wbr>
			$image_org=empty($_POST['i<wbr ></wbr>mage_org']<wbr ></wbr>)?$image:(<wbr ></wbr>$_POST['im<wbr ></wbr>age_name']<wbr ></wbr>);
			$image_location=empty($_PO<wbr ></wbr>ST['image_<wbr ></wbr>location']<wbr ></wbr>)?"":mysql<wbr ></wbr>i_real_esc<wbr ></wbr>ape_string<wbr ></wbr>($link,($_<wbr ></wbr>POST['imag<wbr ></wbr>e_location<wbr ></wbr>']));
			$image_date=empty($_POST['<wbr ></wbr>image_date<wbr ></wbr>'])?"":mys<wbr ></wbr>qli_real_e<wbr ></wbr>scape_stri<wbr ></wbr>ng($link,(<wbr ></wbr>$_POST['im<wbr ></wbr>age_date']<wbr ></wbr>));
			$image_fav=empty($_POST['i<wbr ></wbr>mage_fav']<wbr ></wbr>)?"":mysql<wbr ></wbr>i_real_esc<wbr ></wbr>ape_string<wbr ></wbr>($link,($_<wbr ></wbr>POST['imag<wbr ></wbr>e_fav']));<wbr ></wbr>
			$image_desc=empty($_POST['<wbr ></wbr>image_desc<wbr ></wbr>'])?"":mys<wbr ></wbr>qli_real_e<wbr ></wbr>scape_stri<wbr ></wbr>ng($link,(<wbr ></wbr>$_POST['im<wbr ></wbr>age_desc']<wbr ></wbr>));

			//CREATE THE FORM INPUTS
			$input ='
				<div class="photo_container">
					<div>
					<input type="hidden" id="delete_photo[]" name="delete_photo[]" value="no" />
						<input type="checkbox" class="myinput large custom" title="delete image" id="delete_photo[]" name="delete_photo[]" value="delete" />
					</div>
					<div class="photo_box">
						<div class="photo">
							<img class="photo" src="'.$clean.'"/>
						</div>
					</div>
					<div class="info_container">
						<div class="info_input">
							Image name:<br />
							<input type="text" id="image_name[]" name="image_name[]" value="'.$image_name.'"/><<wbr ></wbr>br />
							<input type="hidden" id="image_name[]" name="image_org[]" value="'.$image_org.'"/><b<wbr ></wbr>r />
							image date:<br />
							<input type="date" id="image_date[]" name"image_date[]" value="'.$image_date.'"/><<wbr ></wbr>br />
							Location photo was taken:<br />
							<input type="text" id=image_location[]" name="image_location[]" value="'.$image_location.'<wbr ></wbr>"/><br />
							Add to favoutites?
							<select name="image_fav[]" id="image_fav[]"><br>
								<option id="image_fav[]" name="image_fav[]">'.$imag<wbr ></wbr>e_fav.'</o<wbr ></wbr>ption>
								<option id="image_fav[]" name="image_fav[]">yes</op<wbr ></wbr>tion>
								<option id="image_fav[]" name="image_fav[]">no</opt<wbr ></wbr>ion>
							</select>
							Image description:<br />
							<textarea  rows="5" cols="20 id=image_desc[]" name="image_desc[]">'.$ima<wbr ></wbr>ge_desc.'<<wbr ></wbr>/textarea><wbr ></wbr>
						</div>
					</div>
				</div>	
			';
					
			//DISPLAY RESULTS
			echo $input;	
		}
	}
	//END FORM
	echo $end_form;
	
}elseif(!empty($form_submi<wbr ></wbr>t) ){	
	//SET NEW VARIABLE FOR ALL POST VALUE UPON FORM SUBMIT
	$arr=$_POST;

	// SAVE A COPY OF THE INITIAL STATE
	$old = $arr;
	
	// REMOVE STRINGS
	foreach ($arr as $key => &$sub)
	{
		if (is_string($sub)) unset($arr[$key]);
	}
	
	// MATRIX-INVERT ARRAYS
	$cnt = 0;
	while (TRUE)
	{
		foreach ($arr as $key => $sub)
		{
			$new[$cnt][$key] = $sub[$cnt];
			unset($arr[$key][$cnt]);
		}
		$cnt++;
		if (empty($arr[$key][$cnt])) break;
	}
	
	// KEEP STRINGS, USE NEW ARRAYS
	$cnt = 0;
	foreach ($old as $key => $sub)
	{
		if (is_string($sub))
		{
			$out[][$key] = $sub;
		}
		elseif (!empty($new[$cnt]))
		{
			$out[] = $new[$cnt];
			$cnt++;
		}
	}
	echo "<pre>";
	print_r($out);
	echo"<br>
<br>
<br>
";
	//CHECK TO SEE IF USER WOULD LIKE TO DELETE THE PHOTO
	foreach($out as $image){
		//SET UP SOME VARIABLES
		$image_delete=empty($image['delete_photo'])?"":$image['delete_photo'];
		$image_name=empty($image['image_name'])?"":$image['image_name'];
		$image_org=empty($image['image_org'])?"":$image['image_org'];
		$image_location=empty($image['image_location'])?"EARTH":$image['image_location'];
		$image_date=empty($image['image_date'])?date("Ymds",time()):$image['image_date'];
		$image_fav=empty($image['image_fav'])?"no":$image['image_fav'];
		$image_desc=empty($image['image_desc'])?" ":$image['image_desc'];
		$image_submit=empty($image['image_submit'])?"":$image['image_submit'];
		
		if(empty($image_delete) && empty($image_submit)){
			//UPDATE THE MYSQL
			if(!empty($image_name) || !isset($image_name)){
				$sql="INSERT INTO photos (name,bb_uploader, description, photo_date,photo_fav,photo_loc)";
				$sql.="VALUES ('".$image_name."','".$username."','".$image_desc."','".$image_date."',";
				$sql.="'".$image_fav."','".$image_location."')";
				$query=mysqli_query($link, $sql) or die(mysqli_error($link));
			
				//CHECK IT YOU MOVE IT TO FAVS
				if(strtolower($image_fav=="no")){
						//MOVE FILES TO GALLERY FOLDER
						$rename=@rename($image_org, $recent_dir.$image_date.$image_name);
						if(!$rename){
							echo '<p style="color:#FF0000;">
									ERROR1: Could not move '.strtoupper($image_name).' file to gallery
								  </p>';	
						}else{
							echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded!</p>';
						}
			
				}elseif(strtolower($image_fav=="yes")){
					//MOVE FILES TO GALLERY FOLDER
					$rename=@rename($image_org, $recent_dir.$image_date.$image_name);
					if(!$rename){
						echo '<p style="color:#FF0000;">ERROR2: Could not move '.strtoupper($image_name).' file to gallery</p>';	
					}else{
						echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded!</p>';
					}
					
					//COPY FILES TO FAVOURITE
					$copy=@copy($recent_dir.$image_date.$image_name, $fav_dir.$image_date.$image_name);
					if(!$copy){
						echo '
							<p style="color:#FF0000;">
								ERROR3: Could not copy '.strtoupper($image_name).' file to favourites gallery
							</p>';	
					}else{
						echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded!</p>';
					}
				}
			}
		}elseif(!empty($image_delete) && empty($image_submit)){
			if(strtolower($image_delete) =="delete"){				
				//DELETE THE PHOTO
				$delete=@unlink($image_org);
				if(!$delete){
					echo '<p style="color:#FF0000;">ERROR: Cannot delete '.strtoupper($image_name).'</p>';
					
				}else{
					echo '<p style="color:green;">'.strtoupper($image_name).' deleted!</p> ';
					
				}
			}elseif(strtolower($image_delete) !== "delete"){
				//UPDATE THE MYSQL
				if(!empty($image_name) || !isset($image_name)){
					$sql="INSERT INTO photos (name,bb_uploader, description, photo_date,photo_fav,photo_loc)";
					$sql.="VALUES ('".$image_name."','".$username."','".$image_desc."','".$image_date."',";
					$sql.="'".$image_fav."','".$image_location."')";
					$query=mysqli_query($link, $sql) or die(mysqli_error($link));
				
					//CHECK IT YOU MOVE IT TO FAVS
					if(strtolower($image_fav=="no")){
							//MOVE FILES TO GALLERY FOLDER
							$rename=@rename($image_org, $recent_dir.$image_date.$image_name);
							if(!$rename){
								echo '<p style="color:#FF0000;">
										ERROR1: Could not move '.strtoupper($image_name).' file to gallery
									  </p>';	
							}else{
								echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded!</p>';
							}
				
					}elseif(strtolower($image_fav=="yes")){
						//MOVE FILES TO GALLERY FOLDER
						$rename=@rename($image_org, $recent_dir.$image_date.$image_name);
						if(!$rename){
							echo '<p style="color:#FF0000;">ERROR2: Could not move '.strtoupper($image_name).' file to gallery</p>';	
						}else{
							echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded!</p>';
						}
						
						//COPY FILES TO FAVOURITE
						$copy=@copy($recent_dir.$image_date.$image_name, $fav_dir.$image_date.$image_name);
						if(!$copy){
							echo '
								<p style="color:#FF0000;">
									ERROR3: Could not copy '.strtoupper($image_name).' file to favourites gallery
								</p>';	
						}else{
							echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded!</p>';
						}
					}
				}
			}
		}elseif(!empty($image_submit)){
			//GO TO DIFFERENT PAGE
			$redirect='
				<script type="text/javascript">
					<!--
					window.location = "media.php"
					//-->
				</script>
			';
			
			//echo $redirect;
			
		}else{
			$e_msg = 'ERROR: System malfunction!';
			reload(3000,"fail",$e_msg);
		}
	}
}
?>

Open in new window


i hope this is a lot more clear
0
J NUnicorn wranglerAuthor Commented:
id also like to point that i did have it working perfectly but it would only perform the actions on one file
0
Ray PaseurCommented:
Sorry, I'm just lost.  Maybe there is some confusion around the difference between empty() and !isset().  See if this makes more sense for your task.

<?php // RAY_temp_jaymenagy.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_28233576.html#a39473819

$arr = array
( 'delete_photo' => Array
        (
            '0' => 'no'
         ,   '1' => 'no'
         ,   '2' => 'no'
         ,   '3' => 'no'
        )
, 'image_name' => Array
        (
            '0' => 'analysis.jpg'
         ,   '1' => 'balance.jpg'
         ,   '2' => 'beach ball.jpg'
         ,   '3' => 'binoculars.jpg'
        )
, 'image_org' => Array
        (
            '0' => "/var/chroot/home/content/21/10892721/html/temp/'tmp1378624838'jaymenagy1-analysis.jpg"
         ,   '1' => "/var/chroot/home/content/21/10892721/html/temp/'tmp1378624838'jaymenagy2-balance.jpg"
         ,   '2' => "/var/chroot/home/content/21/10892721/html/temp/'tmp1378624838'jaymenagy3-beach ball.jpg"
         ,   '3' => "/var/chroot/home/content/21/10892721/html/temp/'tmp1378624838'jaymenagy4-binoculars.jpg"
        )
, 'image_location' => Array
        (
            '0' => ''
         ,   '1' => ''
         ,   '2' => ''
         ,   '3' => ''
        )
, 'image_fav' => Array
        (
            '0' => ''
         ,   '1' => ''
         ,   '2' => ''
         ,   '3' => ''
        )
, 'image_desc' => Array
        (
            '0' => ''
         ,   '1' => ''
         ,   '2' => ''
         ,   '3' => ''
        )
, 'image_submit' => 'Image Submit'
)
;

// SHOW THE ORIGINAL DATA
print_r($arr);

// SAVE A COPY OF THE INITIAL STATE
$old = $arr;

// REMOVE STRINGS
foreach ($arr as $key => $sub)
{
    if (is_string($sub)) unset($arr[$key]);
}

// MATRIX-INVERT ARRAYS
$cnt = 0;
while (TRUE)
{
    foreach ($arr as $key => $sub)
    {
        $new[$cnt][$key] = $sub[$cnt];
        unset($arr[$key][$cnt]);
    }
    $cnt++;
    if (!isset($arr[$key][$cnt])) break;
}

// KEEP STRINGS, USE NEW ARRAYS
$cnt = 0;
foreach ($old as $key => $sub)
{
    if (is_string($sub))
    {
        $out[][$key] = $sub;
    }
    elseif (!empty($new[$cnt]))
    {
        $out[] = $new[$cnt];
        $cnt++;
    }
}

// SHOW THE WORK PRODUCT
print_r($out);

Open in new window

0
J NUnicorn wranglerAuthor Commented:
that worked perfectly i guess the empty was a little to thorough and the isset was definitely a better idea as i am allowing some fields to be empty!

thanks again
0
Ray PaseurCommented:
Yeah, PHP has an "interesting" history with empty() and isset().  At one time you had to test with isset() to avoid a Notice upon testing with empty().  Apparently that has changed.  In HTTP requests made via HTML forms, any input that is of type="text" can be empty and it will be set, and of data type string, but if there is nothing filled in it will be empty().  So I guess I will have to rearrange my thinking about testing with empty() and expecting a Notice!
0
J NUnicorn wranglerAuthor Commented:
i got another question that deals with the last two i would really appreciate your insight on.

essentially i removed the hidden field in the form because it created and offset in ['delete_image'] (1 more value for each time the checkbox was selected)

however if the user does not select any delete checkboxes then everything is fine same if they delete every image but if only one image is selected then the image_delete array only contains one value and the rearranging does not work

if you have time please take a look
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_28234857.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.