SearchSearch   

array wizardry

 
   Webmaster Forums (Home) -> PHP MySQL RSS
Next:  PHP Shopping Carts Software  
Author Message
strawberry

External


Since: Apr 20, 2007
Posts: 21



(Msg. 1) Posted: Tue Dec 16, 2008 2:10 pm
Post subject: array wizardry
Archived from groups: alt>php>sql (more info?)

Hi all. Can anyone help me out here?

A script like this...
[code]
echo "<form action='processform.php' method='POST'>\n";
echo "<select name='name[sphere]'>\n";
echo "<option value='colour[] '>Select One</option>\n";
echo "<option value='colour[red]'>1</option>\n";
echo "<option value='colour[green]'>2</option>\n";
echo "<option value='colour[blue]'>3</option>\n";
echo "</select><br>\n";
echo "<select name='name[cube]'>\n";
echo "<option value='colour[]'>Select One</option>\n";
echo "<option value='colour[red]'>1</option>\n";
echo "<option value='colour[green]'>2</option>\n";
echo "<option value='colour[blue]'>3</option>\n";
echo "</select><br>\n";
echo "<input type='submit' value = 'go'>\n";
echo "</form>\n";[/code]

can produce an array like this...

[code]$_POST:
Array
(
[name] => Array
(
[sphere] => colour[red]
[cube] => colour[green]
)

)[/code]


but I guess I'm after an array more like this...
[code]
$_POST:
Array
(
[0] => Array
(
[primitive] => sphere
[colour] => red
)

[1] => Array
(
[primitive] => sphere
[colour] => green
)

)[/code]

I suspect that this can be done by simply revising the form, without
the need for any additional post-processing.

Going one step further, I'd really like values to passed to the array
ONLY if a value other than the default is selected, but I guess I can
just pass all the values and then use a loop to filter the results.

Any thoughts?
Back to top
strawberry

External


Since: Apr 20, 2007
Posts: 21



(Msg. 2) Posted: Tue Dec 16, 2008 6:27 pm
Post subject: Re: array wizardry
Archived from groups: per prev. post (more info?)

> Ouch, don't use echo for this, exit PHP and use normal HTML, it's faster and
> kinder for your server.

In reality, the combobox, and all its values, are populated by the
results of a query. Under these circumstances, would you still
advocate the 'stop-start' approach?

> The post processing would be the absolute simplest way, the example is a
> simple and not caring about if the indata is sent in the correct format.
> If you must do it on the other end, you would need to take help of javascript
> and maybe use json objects which you then can transfer into an array in PHP if
> you have the json extension (or write your own small parser in php)

No thanks. I'll heed your sage advice instead.

Cheers.
Back to top
J.O. Aho

External


Since: May 31, 2006
Posts: 2034



(Msg. 3) Posted: Wed Dec 17, 2008 2:45 am
Post subject: Re: array wizardry
Archived from groups: per prev. post (more info?)

strawberry wrote:
> Hi all. Can anyone help me out here?
>
> A script like this...
> [code]
> echo "<form action='processform.php' method='POST'>\n";
> echo "<select name='name[sphere]'>\n";
> echo "<option value='colour[] '>Select One</option>\n";
> echo "<option value='colour[red]'>1</option>\n";
> echo "<option value='colour[green]'>2</option>\n";
> echo "<option value='colour[blue]'>3</option>\n";
> echo "</select><br>\n";
> echo "<select name='name[cube]'>\n";
> echo "<option value='colour[]'>Select One</option>\n";
> echo "<option value='colour[red]'>1</option>\n";
> echo "<option value='colour[green]'>2</option>\n";
> echo "<option value='colour[blue]'>3</option>\n";
> echo "</select><br>\n";
> echo "<input type='submit' value = 'go'>\n";
> echo "</form>\n";[/code]

Ouch, don't use echo for this, exit PHP and use normal HTML, it's faster and
kinder for your server.

--- example ---
<?php
$i = $_POST['i'];
if(is_numeric($i))
$i++;
else
$i=0;
?>
<form action='processform.php' method='POST'>
....
</form>
<?php
echo $i;
?>
--- eof ---

> can produce an array like this...
>
> [code]$_POST:
> Array
> (
> [name] => Array
> (
> [sphere] => colour[red]
> [cube] => colour[green]
> )
>
> )[/code]
>
>
> but I guess I'm after an array more like this...
> [code]
> $_POST:
> Array
> (
> [0] => Array
> (
> [primitive] => sphere
> [colour] => red
> )
>
> [1] => Array
> (
> [primitive] => sphere
> [colour] => green
> )
>
> )[/code]
> I suspect that this can be done by simply revising the form, without
> the need for any additional post-processing.

$newarray = array()
foreach($_POST['name'] AS $key => $value)
$newarray[] = array('primitive' => $key, 'color' => $key);

The post processing would be the absolute simplest way, the example is a
simple and not caring about if the indata is sent in the correct format.


> Going one step further, I'd really like values to passed to the array
> ONLY if a value other than the default is selected, but I guess I can
> just pass all the values and then use a loop to filter the results.

If you must do it on the other end, you would need to take help of javascript
and maybe use json objects which you then can transfer into an array in PHP if
you have the json extension (or write your own small parser in php).


--

//Aho
Back to top
Norman Peelman

External


Since: Jan 27, 2007
Posts: 65



(Msg. 4) Posted: Wed Dec 17, 2008 6:23 am
Post subject: Re: array wizardry
Archived from groups: per prev. post (more info?)

strawberry wrote:
>> Ouch, don't use echo for this, exit PHP and use normal HTML, it's faster and
>> kinder for your server.
>
> In reality, the combobox, and all its values, are populated by the
> results of a query. Under these circumstances, would you still
> advocate the 'stop-start' approach?
>
>> The post processing would be the absolute simplest way, the example is a
>> simple and not caring about if the indata is sent in the correct format.
>> If you must do it on the other end, you would need to take help of javascript
>> and maybe use json objects which you then can transfer into an array in PHP if
>> you have the json extension (or write your own small parser in php)
>
> No thanks. I'll heed your sage advice instead.
>
> Cheers.
>

or the HEREDOC aproach... that way you can still use variables.

http://us2.php.net/manual/en/language.types.string.php



--
Norman
Registered Linux user #461062
Back to top
J.O. Aho

External


Since: May 31, 2006
Posts: 2034



(Msg. 5) Posted: Wed Dec 17, 2008 6:18 pm
Post subject: Re: array wizardry
Archived from groups: per prev. post (more info?)

strawberry wrote:
>> Ouch, don't use echo for this, exit PHP and use normal HTML, it's faster and
>> kinder for your server.
>
> In reality, the combobox, and all its values, are populated by the
> results of a query. Under these circumstances, would you still
> advocate the 'stop-start' approach?

Yes, as you don't have to parse the none php-code, of course you can use a
less bad echo type, single quotes and concating strings, which is slightly
worse than stop-start method.

Example:
echo '<input type="text" name="data[]" value="'.$value.'">';


--

//Aho
Back to top
strawberry

External


Since: Apr 20, 2007
Posts: 21



(Msg. 6) Posted: Thu Dec 18, 2008 5:30 am
Post subject: Re: array wizardry
Archived from groups: per prev. post (more info?)

On Dec 17, 5:18 pm, "J.O. Aho" <u....RemoveThis@example.net> wrote:
> strawberry wrote:
> >> Ouch, don't use echo for this, exit PHP and use normal HTML, it's faster and
> >> kinder for your server.
>
> > In reality, the combobox, and all its values, are populated by the
> > results of a query. Under these circumstances, would you still
> > advocate the 'stop-start' approach?
>
> Yes, as you don't have to parse the none php-code, of course you can use a
> less bad echo type, single quotes and concating strings, which is slightly
> worse than stop-start method.
>
> Example:
> echo '<input type="text" name="data[]" value="'.$value.'">';
>
> --
>
>   //Aho

Coming back to original question, I eventually found this link which,
although I might not have expressed it terribly well, turns out to be
exactly what I was after:

http://www.nightbluefruit.com/blog/?p=13#comment-3667
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> PHP MySQL
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum