Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About rick.emmet

  • Rank
    Regular Member

Profile Information

  • Gender
    Not Telling
  1. Hello benanamen & psycho, I want to reset the keys in the sub arrays & I don't want to reset the keys 'name', 'type', 'tmp_name', 'error' or 'size.' In this case, the resulting $_FILES array can then be further processed, where the sub arrays are required ($_FILES['userfile']['name'][$num] for instance). We didn't do much of anything with multi-dimensional arrays in the last PHP class I took. I've been studying, experimenting and testing multi-dimensional arrays for the last week or so, and have learned a bunch of neat tricks. This seems like it will be a useful tool for my bag of tricks. Also, This is the way that the $_FILES array is set up, so, I want to be able to work with it as is - normally there will be many elements in this array. If you could point me in the right direction, it will be very much appreciated. Cheers, Rick
  2. Hi Everyone, I've been looking online for a way to reset the keys of sub arrays. Heres a portion of what I have: Array ( [userfile] => Array ( [name] => Array ( [5] => IMG_20170325_152954871_HDR.jpg ) [type] => Array ( [5] => image/jpeg ) [tmp_name] => Array ( [5] => /Applications/XAMPP/xamppfiles/temp/phpA49huQ ) ) ) I've found a couple of examples that are close to what I'm looking for: $_FILES = array_map('array_values', $_FILES); // And also tried foreach ($_FILES as &$val) { $val = array_values($val); } But both of them produce this output: Array ( [userfile] => Array ( [0] => Array ( [5] => IMG_20170325_152954871_HDR.jpg ) [1] => Array ( [5] => image/jpeg ) [2] => Array ( [5] => /Applications/XAMPP/xamppfiles/temp/phpA49huQ ) ) ) I want to go one level deeper and reset the sub array keys like so: Array ( [userfile] => Array ( [name] => Array ( [0] => IMG_20170325_152954871_HDR.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => /Applications/XAMPP/xamppfiles/temp/phpA49huQ ) ) ) Everything I've tried has failed. Any ideas? Thanks so much. Cheers, Rick
  3. Hello Jacques1, Here's what I was attempting to do... // Create an array of only the file type $types = array(); foreach ($_FILES as $filename => $arrayofvalues) { $types['type'] = $arrayofvalues['type']; } // Create a sub-array of types foreach ($types as $subArray) { foreach ($subArray as $val) { $simple[] = $val; } } // Get the key & value of just image files $images = preg_grep('/\image\b/', $simple); // Use the differenc array function to find the indices I want $dif = array_diff($simple, $images); // Loop through the difference array and uset non-photos foreach ($dif as $key => $value) { unset($_FILES['userfile']['name'][$key]); unset($_FILES['userfile']['type'][$key]); unset($_FILES['userfile']['tmp_name'][$key]); unset($_FILES['userfile']['error'][$key]); unset($_FILES['userfile']['size'][$key]); } And the output was: Full FILES array: Array ( [userfile] => Array ( [name] => Array ( [0] => text.txt [1] => [2] => Home Page.tiff [3] => video_icon.png [4] => watersports.gif [5] => IMG_20170325_130533573_HDR.jpg ) [type] => Array ( [0] => text/plain [1] => [2] => image/tiff [3] => image/png [4] => image/gif [5] => image/jpeg ) // MORE OF THE $_FILES ARRAY BELOW All the 'type' array: Array ( [type] => Array ( [0] => text/plain [1] => [2] => image/tiff [3] => image/png [4] => image/gif [5] => image/jpeg ) ) Simple images array: Array ( [0] => text/plain [1] => [2] => image/tiff [3] => image/png [4] => image/gif [5] => image/jpeg ) the elements to remove: Array ( [0] => text/plain [1] => ) FILES array after unsetting: Array ( [userfile] => Array ( [name] => Array ( [2] => Home Page.tiff [3] => video_icon.png [4] => watersports.gif [5] => IMG_20170325_130533573_HDR.jpg ) [type] => Array ( [2] => image/tiff [3] => image/png [4] => image/gif [5] => image/jpeg ) // MORE OF THE $_FILES ARRAY BELOW Thanks for your assistance, Rick
  4. Hello Requinex and Jacques1, Thanks for responding. I have a series of functions that strip tags, check for upload errors, mime type or anything that's black listed. The code creates a new image and destroys the old one. Anyone can copy an HTML form and add userfile buttons to send any number of files to the server. I found that if there's a whole in the userfile type array (such as ' ') the code processes all the subsequent images and places them in the upload folder. I want to unset these from the $_FILES array and then hand off the remaining files to the functions that filter the input. I would like to use preg_grep() to find all the cases in the userfile array that are not ( ^ ) images. I attempted to use preg_grep on $_FILES, but can't retrieve anything other than “array” so I wanted to create a mono-dimensional array, use preg_grep on it and then unset the corresponding files in the $_FILES array. The $_ FILES array can then be processed with a limit on the number of photos. The idea is that I won't process thirty or fifty photos, and then limit the number moved to the upload folder. I don't want the processing overhead if I can avoid it. The text book shows the use of the array construct like this: $products = array('tires', 'oil', 'spark plugs'); and that worked. A variable is supposed to be exactly what is inside of it, so: $newArray = array($values); for instance, should be the same as: $ newArray = array('image/tiff', 'image/png', 'image/gif', '', 'image/jpeg', 'text/plain'); I just looked up array() in the Manual and it show that one can use the key and value pair or just the values. I tried to create an array with the key value pair in this manner: $values .= $key." => '".$value."', "; And I get the output: Array ( [0] => 0 => 'image/tiff', 1 => 'image/png', 2 => 'image/gif', 3 => '', 4 => 'image/jpeg', 5 => 'image/jpeg' ) And again if I cut and past the output in red into the line of code (within the parenthesis): $cantSayOnTV = array(0 => 'image/tiff', 1 => 'image/png', 2 => 'image/gif', 3 => '', 4 => 'image/jpeg', 5 => 'image/jpeg'); I get a proper array like this: Array ( [0] => image/tiff [1] => image/png [2] => image/gif [3] => [4] => image/jpeg [5] => image/jpeg ) instead of one that has a single index of [0]. The fact that the code can output a duplicate of the userfile type array makes its failure when using a variable seem odd to me. Any help with this is very much appreciated. Cheers, Rick
  5. Hello Everyone, This ought to be straightforward, I want to retrieve the entire “type” array out of the $_FILES array, so that I can run a test to determine what uploaded files are images. Everything I've looked at in the Manual and at various forums hasn't worked. Since there are a number of image types, I tried to use preg_grep to find any image files uploaded in the “type” array, and all I could retrieve was, “array.” I tried several other approaches and the most I could retrieve was, “[userfile] => array.” Since this is a multi-dimensional array, I had previously attempted to loop through the array and could only print out the proper keys and values, but couldn't contain them in a variable outside of the loop. I then tried a foreach loop with a key and value and have been able to grab the values for each upload, image/jpeg, text/plane, or even ' ' (which is really important – I want to unset these.) But when I attempted to create a mono-dimensional array, I'm having all kinds of trouble. I'm not getting any error message, just garbage output. I created a variable and assigned the formatted data values with single quotations and a comma. Then I removed the last comma and space and even used settype to make sure that the variable contains a string. When I run the code, I get an array with one index [0] and all my values, see webPage.jpg: Once I have this data as an array, I can use preg_grep to find all the non-images and unset them, as the indices will be exactly the same as the ones in the $_FILES array. Get this, in order to create the last array in webPage.jpg, I copied and pasted the string from the HTML output, “The string in the Values Var is: ” (in red) into the array() for the variable $cantSayOnTV, and then it can output that to the page as a proper array. Here's my code. At this point, the insert page simply calls this function: function forUpload() { // Try a different approach foreach ($_FILES['userfile']['type'] as $key => $value) { $values .= "'".$value."', "; } // remove the last two characters $values = rtrim($values,', '); // set the type for the variable settype($values, "string"); echo "The string in the Values Var is: ".$values."<br />"; echo gettype($values)."<br />"; // TRY to make this an array $newArray = array($values); // Try to echo out the simple array here echo '<pre>'; print_r($newArray); echo '</pre>'; "<br />"; // force the output to become an array $cantSayOnTV = array('image/tiff', 'image/png', 'image/gif', '', 'image/jpeg', 'text/plain'); echo '<pre>'; print_r($cantSayOnTV); echo '</pre>'; "<br />"; echo "Exiting Data Validation on line 218"; exit; } So, is there a straightforward way to grab all of the “type” array from the $_ FILES array – or is there some way to force the string to be converted into a proper array? Thanks in advance for you assistance! Cheers, Rick
  6. Hello All, I'm developing the front end of a database driven site using Bootstrap. I would like to allow viewers to see numerous photos uploaded to the site, by other users, in a very convenient way. On a desktop or laptop computer, this is no problem, however, on a mobile device it's more challenging for users to view thumbnail photos and click to see a large version of the photo. The “bootstrap carousel with gallery thumbs” does fit on my tiny Droid Mini, but I thought I'd throw this out to the community and see if all of you have strong feelings either way about this. This must be a useful feature and not a problem for the user to interact with. Also, do any of you have suggestions for best practices on this topic? Thanks so much for your input! Cheers, Rick
  7. Hi Requinix, Thank you so much for the reply! I spent all my time trying to find the correct way to nest this - I didn't even think about un-nesting it. I think this will work fine. BTW, I don't remember making any changes to this at all; this came from a tutorial or a posting on the forum (or a link from the forum). Looking at it now, its seems that it could not have ever worked as written. That really makes me wonder - how does one spell that sound Scooby Doo used to make when he was confused? Making something somewhat complicated into something very complicated doesn't usually work!! Thanks again, I'll get right on it. Cheers, Rick
  8. Hey Everyone, I've been beating my head against the wall for two days on this one. I have a snippet of code that I have not touched for years – it used to run perfectly. I think I must have inadvertently changed the code at some point, and I can't get it to run properly. It's a nesting problem in a series of “if statements” and no matter what, I can't seem to nest these conditionals so they'll work. I'd like to use a switch statement, but I have five different arguments for it. Here's the code: $pagination = ""; if ($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1) { // When the condition is met, the code below runs and then drops down to "echo $pagination;" $pagination.= "<a href=\"$targetpage?page=$prev\">< previous </a>"; } else { $pagination.= "<span class=\"disabled\">< previous</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) { //not enough pages to bother breaking it up for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) { $pagination.= "<span class=\"current\"> $counter </span>"; } else { $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>"; } // END OF FIRST SERIRES OF IF STATEMENTS } } elseif ($lastpage > 5 + ($adjacents * 2)) { //enough pages to hide some //close to beginning; only hide later pages if ($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) { $pagination.= "<span class=\"current\"> $counter </span>"; } else { $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\"> $lastpage</a>"; } //in middle; hide some front and some back } elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2 </a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) { $pagination.= "<span class=\"current\"> $counter </span>"; } else { $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>"; } } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\"> $lastpage</a>"; //close to end; only hide early pages } else { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2 </a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) { $pagination.= "<span class=\"current\"> $counter </span>"; } else { $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>"; } } } } //next button if ($page < $counter - 1) { $pagination.= "<a href=\"$targetpage?page=$next\"> next ></a>"; } else { $pagination.= "<span class=\"disabled\">next ></span>"; $pagination.= "</div>\n"; } } } echo $pagination; My “do-while” loop (below the pagination) creates a set of small tables that display information for each record. On page 1, everything looks fine. On any other page, the pagination only displays the “previous” link. Once the condition “if ($page > 1)” is met the next line is executed, echoing the “previous” link, and the code drops to the bottom of the function. I've tried every other kind of logic I can think of and am all out of ideas. BTW, I'm using procedural PHP on this project. Any help will be very appreciated.
  9. Thanks Psycho, I haven't been able to get back to this for four days (yikes), but I'll start on it today. Cheers, Rick
  10. Hello scootstah and cyberRobot, Thank you for the replies! My service provider has been changing out their equipment and I haven't had access for several days. I may be overwriting the session somehow, didn't even occur to me to look into that and I'm not generating any errors at all. When I have a problem this messed up, it's ussually a matter of having the wrong approach, so that's why I didn't post any code. Is there a type of functionality that either of you prefer to use in this kind of situation? Thanks again! Cheers, Rick
  11. Hello All, I'm attempting to create functionality that allows users to update a sales figure by: adding one of two ad-ons (or both) and / or subtracting a promotional discount if one applies. I'm setting both of my ad-on variables, as well as the discount to zero, then I'm using plain PHP to process the math. When I first arrive on the page, from an editing page, the correct figure is displayed at the bottom of the form ($9.99), but when I click on the recalculate button (form action=”THIS_PAGE”) the base price disappears and the total at the bottom becomes “$0.” These are $_SESSION variables, so it seems to me that they should remain persistent. I've been looking at JavaScript DOM Storage shopping carts, and the use of multidimensional arrays such as $_SESSION['cart'] to solve this problem. It seems to me that this should be very straightforward, but perhaps I'm approaching this incorrectly. I would like to get input on the best approach to this problem – don't even want to bother anyone with code at this point. Do any of you have a very reliable approach or model to suggest? Thanks a bunch in advance. Cheers, Rick
  • 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.