Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5934

Re: First question and introducing myself

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: First question and introducing myself
Date 2012-05-10 15:29 -0400
Organization A noiseless patient Spider
Message-ID <joh4ua$7t5$1@dont-email.me> (permalink)
References <jogm85$5m5$1@dont-email.me>

Show all headers | View raw


On 5/10/2012 11:18 AM, Shake wrote:
> Hi All,
>
> That's my first post in this group. I would start introducing myself...
> Hello! and apologizing if I make any mistake, writing (English is no my
> natural language) or not following the rules of this group.
>
> And now, the on topic question:
>
> A little introduction: it's about preparing variables, taking some vars
> getted by $_POST and passing to $_SESSION.
>
> The basic idea is a function to trim(), and check some rules on this
> $_POST values. And the problem to solve is walking throught a
> "multidimensional array path".
>
> An example:
>
> I get via post:
>
> $_POST['not']['interesting']
> $_POST['yes']['interesting']['selector']
> $_POST['yes']['interesting'][0]['something']
> $_POST['yes']['interesting'][0]['otherthing']
> $_POST['yes']['interesting'][1]['something']
> $_POST['yes']['interesting'][1]['otherthing']
> $_POST['other']
> ...
>
> Check (for example) and set into $_SESSION the
>
> $_POST['yes']['interesting'][0] array
>
> And get a checked:
>
> $_SESSION['yes']['interesting'][0]['something']
> $_SESSION['yes']['interesting'][0]['otherthing']
>
>
> Suposse i do:
>
> $selector = trim($_POST['yes']['interesting']['selector'])
>
> if( trim($_POST['yes']['interesting'][$selector]['otherthing'])==''
> || othercheck
> ) {
> $the_data = trim($_POST['yes']['interesting'][$selector]['otherthing']);
> // do more things $the_ data
> $_SESSION['yes']['interesting'][$selector]['otherthing'] = $the_data;
>
> }
>
> And I repeat this kind of if segment a few times... And I want to
> convert it to a method or function...
>
> What I really want to do (And I don't found how to do) is something like:
>
> (This is some... php pseudocode)
>
> public function checkPostData( $array_path)
> {
> if(trim($_POST[ the $array_path ] )) do somethin.
> }
>
> and call by: checkPostData(['yes']['interesting']['selector']);
>
> Thats its not posible, of course...
>
> then I though passing by reference...
>
> public function checkPostData( & $item) {
>
> // Get the parents indexs until arrive to $_POST
>
> }
>
> But I don't get any result in my "investigation". Then I though that
> anybody have to found this requiremente before... and tried to search on
> the web... But I don't get the answers, probably because I don't know
> the best words to search for.
>
> And that's all.
>
> Sorry for the long of the text, and thanks for all.
>
> Bye

Hi, Shake, and welcome to the group.  Don't worry about your English - 
it's just fine.

As for your problem - there really isn't a good way to handle it in PHP. 
  Part of the problem is the number of indexes you need for each 
variable (number of array dimensions).  There is nothing wrong with 
doing it; it just makes things harder to process, as you've found.

Another problem is you want to store it in the $_SESSION array with the 
same indexes.  Is there a reason for that?  Again, it's not a problem in 
itself, but it does complicate matters.

You could pass the indexes you want as an array, i.e.

checkPostData(array('yes', 'interesting', 'selector'));

But it will be a bit awkward processing in the function.  Getting the 
value wouldn't be hard, i.e. (not tested)

function checkPostData($indexes) {
   $value = $_POST;
   foreach ($indexes as $index)
     $value = $value[$index];

   ... $value should now hold the value and can be validated.

However, putting it back into the $_SESSION array would be a little 
harder.  You wouldn't want to use a foreach() loop because the last 
element isn't an array.  But something like this might work:

   $data = $_SESSION;
   for ($i = 0; $i < count($indexes) - 1; $i++) {
     if (!exists($data[$indexes[$i]]))
       $data[$indexes[$i]] = array();
     $data = $data[$indexes[$i]]'
   }
   $data[$indexes[count($indexes)-1]] = $value;

Note: You could also use $i instead of count($indexes)-1 in the last 
statement, but that is more prone to possible errors.

Of course I've left out some testing (like to insure the arrays are 
correct), but hopefully you get the idea.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

First question and introducing myself Shake <QUITAESTO@QUITAESTO.NOES> - 2012-05-10 17:18 +0200
  Re: First question and introducing myself Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-10 15:29 -0400
  Re: First question and introducing myself Denis McMahon <denismfmcmahon@gmail.com> - 2012-05-10 22:05 +0000
  Re: First question and introducing myself Shake <QUITAESTO@QUITAESTO.NOES> - 2012-05-11 10:47 +0200
    Re: First question and introducing myself Denis McMahon <denismfmcmahon@gmail.com> - 2012-05-12 21:40 +0000
      Re: First question and introducing myself Shake <alex.estevezQUENO@QUENOfilnet.es> - 2012-05-13 23:27 +0200

csiph-web