Jump to content

Manipulating Array Data


phpretard

Recommended Posts

What I need is to:

 

- take the information from array 1

- remove it from array 2

- then ADD it OR take from array 3 (this of course depends on array 1)

 

I also have to keep in mind that array 1 could be empty thus needing to empty array 2 and IF the information is in array 3 then remove it

 

array 3 is the main array that I will update the database row which will in turn display the correct check boxes checked or not

 

Thank you for reading, I hope you can help me.

 

Array //#1 this is the submitted information
(
    [0] => 103100000
    [1] => 103200000
    [2] => 103400000
    [3] => 103500000
    [4] => 103700000
)

Array //#2 all possible choices
(
    [0] => 103100000
    [1] => 103200000
    [2] => 103300000
    [3] => 103400000
    [4] => 103500000
    [5] => 103600000
    [6] => 103700000
)

Array //#3 currently stored in database / marks the check boxes "checked" 
(
    [0] => 103100000
    [1] => 103200000
    [2] => 103300000
    [3] => 103400000
    [4] => 103500000
    [5] => 103600000
    [6] => 103700000
)

 

[attachment deleted by admin]

Link to comment
Share on other sites

Yes and so far:

 

if(empty($_POST['page'])){ // if no checkboxes are submitted
$array1 = array();
$array3 = array();
$newaccess = "";

}else{ // begin problem
$array1 = $_POST['page']; //OK
$array2 = $_POST['page_array']; //OK
$array3 = explode(",", $_POST['existing']); //OK
$array4 = array_diff($array2,$array1); //HMM
$newaccess = implode(",",$array4); // AH SH@@#$#
}

 

So ... if $array4 has a value then I need to add or delete it from $array3

 

Link to comment
Share on other sites

Here ya go.

 

$array1 = array(500,503,9909,887,454)
$array2 = array(100,223,200,109,500,503,9909, 787, 887,454);
$array3 = array(223,200,109,500,503,9909);

foreach($array1 as $v) {
    if(in_array($v, $array2)) {
          $findIt = array_search($v, $array2);
          unset($array2[$findIt]);
    }
    if(in_array($v, $array3)) {
          $findIt = array_search($v, $array3);
          unset($array3[$findIt]);
    } else $array3[] = $v;
}

Link to comment
Share on other sites

So it works to a point.  I cant figure which value(s) to use when updating...when.

 

Here is my elaboration:

  • I am basically updating the same row for one user (will be multiple eventually). 
  • This user will have access to certain pages where in this case the pages are numbered.
  • The numbers are stored (comma separated) in a mysql text field. 
  • I call the data base for the numbers and explode them into check boxes with the value (of the check box) being the page number.
  • Then it is determined whether or not the box should be checked or not checked.  This code is working assuming there are comma separated numbers in the aforementioned text field.
  • Updating the text field is where the problem starts.
  • I would like to simply delete and update BUT because each page display a different range of numbers / check boxes this proposes the problem.
  • I have gotten the code far enough along so that I can actually update the field however storing the information on the page and comparing it on submission is difficult

 

 

I read the code and understand it about 85%  could you maybe elaborate?

 


if(empty($_POST['page'])){$array1 = array();}else{$array1 = $_POST['page'];}
$array2 = $_POST['page_array'];
$array3 = explode(",", $_POST['existing']);

foreach($array1 as $v) {
	if(in_array($v, $array2)) {
		  $findIt = array_search($v, $array2);
		  unset($array2[$findIt]);
	}		
	if(in_array($v, $array3)) {
		  $findIt = array_search($v, $array3);
		  unset($array3[$findIt]);
	}else{
		$array3[] = $v;
	}

}


	if(empty($array3)){/*do nothing to avoid implode errors*/}else{$newaccess = implode(",",$array1);}
	connect();
	$sql = "update access set pid='$newaccess' where pw = '".$_POST['access']."'";
	$update = mysql_unbuffered_query($sql) or die(mysql_error());	

Link to comment
Share on other sites

I think you're making this way too difficult.  Why all the array comparison?  Why not just update the db with the new values?  I'm not understanding the problem.  Also, this is not flexible at all.  Ideally you should have a table that holds the id from the access table and one page number per row.  This is exactly what relational databases are for.

Link to comment
Share on other sites

Unfortunately I usually do make it too difficult. 

 

I have to compare arrays because with the checkboxes as displayed as they are how else will I know which ones to take away from permissions or add?

 

Can you send me refer me to a site regarding relational DB's.

 

Here is the hacked up code that makes it work properly (for now).

 


if(isset($_POST['save'])){

$existforthis = $_POST['existforthis'];
$existforthis_array = explode(",", $existforthis);

$impnewpages = $_POST['page']; // This is an array
$expexisting = explode(",", $_POST['existing']);


$permissionsremoved = array();
	foreach ($expexisting as $EXPSR){
		if (!in_array($EXPSR, $existforthis_array)) {
			$permissionsremoved[] = $EXPSR;
		}
	}

	if(empty($_POST['page'])){

	// do nothing

	}else{
		$permissionskept = array();
		foreach ($expexisting as $EXPSK){
			if (in_array($EXPSK, $impnewpages)) {
				$permissionskept[] = $EXPSK;
			}
	}
}

	if(empty($_POST['page'])){
		$combinedPs = $permissionsremoved;				
	}else{
		$combinedPs = array_merge($permissionsremoved, $impnewpages);
	}

connect();
$newaccess = implode(",",$combinedPs);
$sql = "update access set pid='$newaccess' where pw = '".$_POST['access']."'";
$update = mysql_unbuffered_query($sql) or die(mysql_error());

}// close if submitted

Link to comment
Share on other sites

OK, here is some pseudo code for what it seems like you are trying to do now:

 

Form page:

// $result = SELECT pid FROM access WHERE pw = 'mysql_real_escape_string("something")' LIMIT 1
$row = mysql_fetch_assoc($result);
$access = explode(',', $row['pid']);
}
// $result = SELECT pid FROM pages
while($row = mysql_fetch_assoc($result)) {
    $checked = in_array($row['pid'], $access) ? 'checked' : ' ';
    echo '<input' . $checked . 'type="checkbox" name="pagearray[]" value="' . $row['pid'] .'">';
}

 

Form processing page:

$newaccess = implode(',', array_map('mysql_real_escape_string', $_POST['pagearray']));
// UPDATE access set pid='$newaccess' WHERE pw = 'mysql_real_escape_string($_POST["access"])'

Link to comment
Share on other sites

So am I right in understanding that you are POSTing a users privileges to every page/script they go to?  In other words, you're storing data that you need in multiple place, over and over again.  Like they way verizon asks you to put your phone number before getting to a CSR, when you know for a fact of experience that they don't even use the number you entered.

 

You should store your permissions in a session if you need them in so many functions/scripts.

 

As for the array problem, I can't understand your elaboration.. why does it work "to a point"?

Link to comment
Share on other sites

To be able to edit a particular page one of the comma separated numbers stored in your row must match the page number you may be trying to edit.

 

The check boxes are for the primary site admin to populate / edit / update the different users rows.

 

I could store the numbers in sessions but the session values still have to be populated somehow.

 

[attachment deleted by admin]

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.