phpretard Posted February 28, 2011 Share Posted February 28, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/ Share on other sites More sharing options...
AbraCadaver Posted February 28, 2011 Share Posted February 28, 2011 I don't quite understand your third point, so if you can elaborate that would be great. You will undoubtedly use array_diff() and possibly array_intersect(). Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1180981 Share on other sites More sharing options...
phpretard Posted February 28, 2011 Author Share Posted February 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1180990 Share on other sites More sharing options...
Zane Posted February 28, 2011 Share Posted February 28, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181002 Share on other sites More sharing options...
phpretard Posted March 1, 2011 Author Share Posted March 1, 2011 Why do you make it look so easy??? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181005 Share on other sites More sharing options...
phpretard Posted March 1, 2011 Author Share Posted March 1, 2011 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()); Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181194 Share on other sites More sharing options...
AbraCadaver Posted March 1, 2011 Share Posted March 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181340 Share on other sites More sharing options...
phpretard Posted March 1, 2011 Author Share Posted March 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181370 Share on other sites More sharing options...
AbraCadaver Posted March 1, 2011 Share Posted March 1, 2011 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"])' Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181398 Share on other sites More sharing options...
Zane Posted March 1, 2011 Share Posted March 1, 2011 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"? Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181563 Share on other sites More sharing options...
phpretard Posted March 2, 2011 Author Share Posted March 2, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/229174-manipulating-array-data/#findComment-1181626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.