Re: Mixing Arrays
| From: | Jason Wong | Date: | Tue, 15 Apr 2003 11:05:36 +0000 |
| Subject: | Re: Mixing Arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-143907@lists.php.net to get a copy of this message | ||
On Tuesday 15 April 2003 18:37, Webmaster wrote:
> Anybody out there that can help me with this one?
>
> Have the following form posting arrays to a text file :
>
> <form method="post" action="prod.php">
> <input type="text" name="prodid[ ]" size="7">
> <input type="text" name="proddes[ ]" size="35">
> <input type="text" name="prodid[ ]" size="7">
> <input type="text" name="proddes[ ]" size="35">
> <input type="text" name="prodid[ ]" size="7">
> <input type="text" name="proddes[ ]" size="35"> ...
> and so on
> <input type="submit" value="Submit">
>
> prodid is the Product ID and
> proddes is the Product Description
>
> On prod.php I have :
>
> $data1 = join("%", $prodid);
> $data2 = join("%", $proddes);
> $main_data = "$data1" . "$data2";
>
>
> $fh = fopen("products.txt", "w");
> if (!$fh) {
> die("<br>Failed to open file");
> }
> fwrite($fh, "$main_data");
> fclose($fh);
>
> this generates the output in the textfile as:
>
> 1%2%3%Product One Description%Product Two Description%Product Three
> Description
>
> Question is - how do I get the script to write to the text file so that I
> have a result that looks like this:
>
> 1%Product One Description%2%Product Two Description%3%Product Three
> Description
The way you have named your variables you would probably have to use a
for-loop.
If I was doing it I would have named them differently, thus:
<form method="post" action="prod.php">
<input type="text" name="prod[1][id]" size="7">
<input type="text" name="prod[1][des]" size="35">
<input type="text" name="prod[2][id]" size="7">
<input type="text" name="prod[2][des]" size="35">
...
...
<input type="submit" value="Submit">
Then:
foreach ($_POST['prod'] as $value) {
$main_data = $value['id'] . $value['des'];
}
I'll leave you to fill in the blanks.
--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
The New England Journal of Medicine reports that 9 out of 10 doctors agree
that 1 out of 10 doctors is an idiot.
*/