Jump to content

Problem reading an Array


jaisol

Recommended Posts

Hi

 

What I need to do is retrieve information from a web form, that contains a file id, and which options from check boxes have been ticked.

The code in the web form is.

				    <td width=\"206\"><p>$filename1<br><img src=\"$filename1\" alt=\"$filename1\" /></p>
			    <p>
			      <label>
			        <input type=\"checkbox\" name=\"image_id[$unique_id1][save]\" value=\"save\" id=\"CheckboxGroup1_1\" />
			        Save</label>
			      <br />
			      <label>
			        <input type=\"checkbox\" name=\"image_id[$unique_id1][delete]\" value=\"delete\" id=\"CheckboxGroup1_2\" />
			        Delete</label>
			        <br><label>
			        <input type=\"checkbox\" name=\"image_id[$unique_id1][rotate]\" value=\"rotate\" id=\"CheckboxGroup1_3\" />
			        Image needs rotating</label>
			      <br />
			    </p></td>

 

 

This returns the following when I do a


var_dump of $_POST['image_id'][/code]

when all three boxes are ticked.

 

array(1) { [4600]=> array(3) { ["save"]=> string(4) "save" ["delete"]=> string(6) "delete" ["rotate"]=> string(6) "rotate" } } 

 

the format is as follows

4600 is the file ID, the 3 others are save, delete, and rotate

 

It appears to work ok, but I can't get at the data easily

 

I can get the key using key(), but I can't get the other bits of data out using a foreach loop.

 

if(isset($_POST['image_id']))
{
	$file_info		=	$_POST['image_id'];
	if (isset($file_info))
	{
		var_dump($_POST['image_id']);
		while($element = current(file_info))
		{     
			echo "<br> Loop                     = " . key($file_info)."\n";
			$index_key		=	key($file_info);
			foreach ($dog1 as $index_key => $value)
			{
			        Echo "<br> foreach value = $value"; 
			}
	     	next($file_info); 
		}
	}
}

 

I'm wondering if the array that is being sent back is ok as I tried to creat an array that sent back the same and couldn't, so maybe my code in the form needs changing.

Link to comment
Share on other sites

One of the problems is that you forget dollar sign at one place before "file_info" :)

 

And one question to you. Do you have a set of blocks like the described block? If you have a set of blocks of information about files I can give you another possibility to get it in an easy way.

Link to comment
Share on other sites

The $ sign is there but it was omited because I changed the var name in the msg from $dog1 to $file info to make it easier to read.

 

You ask about "blocks" err which block do you mean? The web page form has upto 100 blocks of that code, its generated from a database and echoed back to the web page.

 

Yes I could use option boxes for save and delete, and a tick box for rotate, and yes someone may want to save the file and rotate it at the same time.

I just want to use 3 tick boxes and get the data back if they are ticked.

 

 

Link to comment
Share on other sites

This is the php code that is trying to read the $_post bit

function save_selected()
{
if(isset($_POST['image_id']))
{
	$dog1		=	$_POST['image_id'];
//		$dog2		=	$_POST[]
	if (isset($dog1))
	{
		var_dump($_POST['image_id']);
//			$dog		=	$_POST['image_id'];
//			echo "<br> dog 0 = " . $dog['1500'] . __LINE__ ;
//			echo "<br> dog 1 = " . $dog[1] . __LINE__ ;
		echo "<br> Key for dog 1 = " . key($dog1) . __LINE__;
//			var_dump($dog);
//			echo "<br> dog dump = " . var_dump($dog);
//			echo "<br> dog = " . $dog;
		while($element = current($dog1))
		{     
			echo "<br> Loop = " . key($dog1)."\n";
			$index_key		=	key($dog1);
			foreach ($dog1 as $index_key => $value)
			{
				Echo "<br> foreach value = $value"; 
			}
	     	next($dog1); 
		}
/*			foreach ($dog as $value)
		{
			echo "<br> Value 0 = " . $value['0'];
			echo "<br> Value 1 = " . $value['1'];
			echo "<br> Value 2 = " . $value['2'];
			echo "<br> Value 3 = " . $value['3'];
			echo "<br> Value 4 = " . $value['4'];
			echo "<br> Value 5 = " . $value['5'];
			echo "<br> Value = " . $value;
//				var_dump($value);
		}
		foreach($dog as $key => $value)
		{
			echo "<br> Value key 0 = " . $value['0'];
			echo "<br> Value key 1 = " . $value['1'];
			echo "<br> Value key 2 = " . $value['2'];
			echo "<br> Value key 3 = " . $value['3'];
			echo "<br> Value key 4 = " . $value['4'];
			echo "<br> Value key 5 = " . $value['5'];
			echo "<br> Value key = " . $value;				
		}
*/
	}
}
//TODO
}

Link to comment
Share on other sites

The web page form has upto 100 blocks of that code, its generated from a database and echoed back to the web page.

This is what I asked about. And every block has a unique id. That's great :)

 

In such a case it's better to use another algorithm.

 

1. Every block has a unique id.

2. When create names and ids, use this id. I'll change your code and then show you how to work with it.

 

<td width=\"206\"><p>$filename1<br><img src=\"$filename1\" alt=\"$filename1\" /></p>
    <p>
      <label>
        <input type=\"checkbox\" name=\"image_{$unique_id1}_save\" value=\"save\" id=\"CheckboxGroup1_1\" />
        Save</label>
      <br />
      <label>
        <input type=\"checkbox\" name=\"image_{$unique_id1}_delete\" value=\"delete\" id=\"CheckboxGroup1_2\" />
        Delete</label>
        <br><label>
        <input type=\"checkbox\" name=\"image_{$unique_id1}_rotate\" value=\"rotate\" id=\"CheckboxGroup1_3\" />
        Image needs rotating</label>
      <br />
    </p></td> 

Curly brackets here image_{$unique_id1}_rotate (and also in other 2 places) are important!!!

 

Well, how would you use it? Answer: easy :)

 

Make a loop (foreach) iteration through $_POST and check keys:

foreach( $_POST as $key => $val )
{
  list( $info, $id, $action)=explode( '_', $key);
  if( $info == 'image' )
  {
     // use $id and $action: $id is a unique id and $action can be any from this set (save, delete, rotate) - do whatever you wish
    // you will see here only checkboxes that were checked at your form
    // any code here
  }
}

From my point of view this algorithm is easier to understand and it produces more clear code.

 

PS. Advantage of this method: you don't need to know your id, you get it from the POST array.

Link to comment
Share on other sites

<?php
if(isset($_POST['image_id'])){
foreach ($_POST['image_id'] as $file_id=>$actions){
	if (isset($actions['save']){
		///
	}
	if (isset($actions['delete'])){
		///
	}
	if (isset($actions['rotate'])){
		///
	}
}
}

 

Your $_POST['image_id'] is an array where each key is the file id, and the value is an array of actions.  You can just loop over it using a standard foreach loop, including a variable for the key.  Then you just test which actions are set and do whatever needs done for each action.

 

Link to comment
Share on other sites

Many thanks Kicken & SergeiSS.

 

I managed to solve it I just needed 2 foreach loops

function save_selected()
{
if(isset($_POST['image_id']))
{
	$file_status	=	$_POST['image_id'];
	$index_key	=	key($file_status);
	foreach ($file_status as $index_key => $value)
	{
		$file_id	=	$index_key;
		$dog2	=	$value;
		foreach ($dog2 as $index_key => $value)
		{
			Echo "<br>Index_key, foreach value = $file_id, $value";
                                                // File_id gives me the file id and the $value gives me the save, delete, whatever bit.
                                                if($value =="save")
                                                .......
		}
	}
}
}

 

I like the use of the curly braces {}, and explode().

 

The problem I had was the $_post[image_id] var coming back was an array within an array and runing foreach once gave me the file id and "array" for the $value. It just needed running through twice and taking the first $value as the array for the second foreach.

 

Many thanks

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.