FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » First question and introducing myself
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
First question and introducing myself [message #178012] Thu, 10 May 2012 15:18 Go to next message
Shake is currently offline  Shake
Messages: 40
Registered: May 2012
Karma: 0
Member
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
Re: First question and introducing myself [message #178023 is a reply to message #178012] Thu, 10 May 2012 19:29 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: First question and introducing myself [message #178031 is a reply to message #178012] Thu, 10 May 2012 22:05 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 10 May 2012 17:18:27 +0200, Shake wrote:

> [stuff about multi dimensional arrays]

I'm not sure if this will work, or if it does, if the result will be what
you want. Feel free to test it out. $a is $_POST and $b is $_SESSION:

<?php

$a = array ( "one" => "a", "two"=>"b", "three"=>"c", "four"=>array
( "une"=>"12","deux"=>"15","trois"=>array
("eins"=>"fred","zwei"=>"jim","drei"=>"3","vier"=>" susan
"),"quatre"=>" james", "cinq"=>"234 ",
"six"=>"mouse"),"five"=>"several words in here" );

$b = array();

function some_validation_function( $var ) {
$var = trim( $var );
return $var;
}

function multi_array_walk( $arr, &$parent ) {
foreach ( $arr as $key => $value ) {
if ( is_array ( $value ) )
multi_array_walk( $value, $parent[$key] );
else if ( is_string( $value ) )
$parent[$key] = some_validation_function( $value );
}
}

var_dump( $a );
var_dump( $b );

multi_array_walk ( $a, $b["post"] );

var_dump( $a );
var_dump( $b );

?>

Rgds

Denis McMahon
Re: First question and introducing myself [message #178042 is a reply to message #178012] Fri, 11 May 2012 08:47 Go to previous messageGo to next message
Shake is currently offline  Shake
Messages: 40
Registered: May 2012
Karma: 0
Member
First, Thanks, Jerry Stuckle and Denis McMahon, for your help.

Jerry, your code doesn't do what I want/need, but gave me some
interesting clues :)

Denis, I am not sure if you're code does, but I guess yes it does.

After your messages, I get this code that does just what I want:

<?
function setArrayPathValue($arrayPath, $src, & $dst)
{
foreach ($arrayPath as $index) $src = $src[$index];

// Here I could validate $src

$result2 = & $dst;
while(!empty($arrayPath))
{
$result3 = & $result2;
$index = array_shift($arrayPath);
if(!isset($result3[$index])) $result3[$index] = array();
$result2 = & $result3[$index];
}
$result3[$index] = $src;
}

$dato['ruta']['a']['un']['dato'] = 'contenido';
setArrayPathValue( array('ruta','a','un','item'), $dato, $_SESSION );

print_r($_SESSION);
?>


Thanks
Re: First question and introducing myself [message #178088 is a reply to message #178042] Sat, 12 May 2012 21:40 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Fri, 11 May 2012 10:47:28 +0200, Shake wrote:

> Denis, I am not sure if you're code does, but I guess yes it does.

Hi

I thought your question was, essentially, to copy the elements of one
multi-dimensional array ($_POST) into the same multi-dimensional
structure inside another array ($_SESSION), performing some string
function (trim) on them as part of the copying process?

Perhaps I misunderstood the question.

Rgds

Denis McMahon
Re: First question and introducing myself [message #178098 is a reply to message #178088] Sun, 13 May 2012 21:27 Go to previous message
Shake is currently offline  Shake
Messages: 40
Registered: May 2012
Karma: 0
Member
Denis McMahon avait prétendu :
> On Fri, 11 May 2012 10:47:28 +0200, Shake wrote:
>
>> Denis, I am not sure if you're code does, but I guess yes it does.
>
> Hi
>
> I thought your question was, essentially, to copy the elements of one
> multi-dimensional array ($_POST) into the same multi-dimensional
> structure inside another array ($_SESSION), performing some string
> function (trim) on them as part of the copying process?
>
> Perhaps I misunderstood the question.
>

What I want is to copy an item from a multidimensional array to
another. This item could be an array, or not. And I want to copy the
item in the same multidimensional structure :)

Greetings
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Windows binaries 64bit for PHP
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 11:16:05 GMT 2024

Total time taken to generate the page: 0.05568 seconds