 |
|
 |
|
Next: Help - error message
|
| Author |
Message |
External

Since: Feb 05, 2008 Posts: 11
|
(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? >> Stay informed about: array wizardry |
|
| Back to top |
|
 |  |
External

Since: Feb 05, 2008 Posts: 11
|
(Msg. 2) Posted: Tue Dec 16, 2008 6:27 pm
Post subject: Re: array wizardry [Login to view extended thread Info.] 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. >> Stay informed about: array wizardry |
|
| Back to top |
|
 |  |
External

Since: Dec 01, 2003 Posts: 190
|
(Msg. 3) Posted: Tue Dec 16, 2008 9:26 pm
Post subject: Re: array wizardry [Login to view extended thread Info.] 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 >> Stay informed about: array wizardry |
|
| Back to top |
|
 |  |
External

Since: Dec 09, 2007 Posts: 66
|
(Msg. 4) Posted: Wed Dec 17, 2008 6:23 am
Post subject: Re: array wizardry [Login to view extended thread Info.] 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 >> Stay informed about: array wizardry |
|
| Back to top |
|
 |  |
External

Since: Dec 01, 2003 Posts: 190
|
(Msg. 5) Posted: Wed Dec 17, 2008 12:26 pm
Post subject: Re: array wizardry [Login to view extended thread Info.] 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 >> Stay informed about: array wizardry |
|
| Back to top |
|
 |  |
External

Since: Feb 05, 2008 Posts: 11
|
(Msg. 6) Posted: Thu Dec 18, 2008 5:30 am
Post subject: Re: array wizardry [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Dec 17, 5:18 pm, "J.O. Aho" 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 >> Stay informed about: array wizardry |
|
| Back to top |
|
 |  |
| Related Topics: | PHPtriad v2.2.1 removal - When I fist started using apache/php/mysql I used phptriad [a wonderful piece of kit] to get me up and running. I've used this ever since. I'm think inow of 'upgrading' to laters versions of all three applications, and have found various tutorials/Ho...
MySQL regular expression to match a imploded item - Hello I need to use MySQL's REGEXP (POSIX compliant) to search registries where one field is an imploded set of integer values separated with pipes "|". I need to match one of these imploded values directly on a sql select. $sql = "SELECT...
Weird mysql_connect problem - Hello. My mysql_connect just started to give me following error today, Fatal error: Call to undefined function: mysql_connect() in ..../database_functions/db_functions.inc.php on line 11. So it seems that my php no longer finds php-mysql module...
MySQL - question about displaying data - I am working an some pages that have database content on them. I'm sorry that I don't have any versions of it online yet, but it isn't hard to explain. The site lists restaurants with contact information. All of the information is kept in a MySQL..
How can i Optimize sql Query ? - Hi, I'd like to optimize this query: Code: SELECT * FROM `links` WHERE active = "1" AND mainweight != 0 ORDER BY Rand()*(1/mainweight) LIMIT 5 I have a database of links wich has 3 000 rows. I'd like to select weighted random links from ... |
|
You can post new topics in this forum You can 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
|
|
|
|
 |
|
|