Re: web form data to arrays?
| From: | William Stokes | Date: | Wed, 17 Jan 2007 14:13:24 +0000 |
| Subject: | Re: web form data to arrays? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-247219@lists.php.net to get a copy of this message | ||
But if the form is printed with foreach don't I need some type of counter?
Otherwise the form that is posted will contain only the data of the last
iteration of the loop that wrote the original form. right?
-Will
""Németh Zoltán"" <znemeth@alterationx.hu> kirjoitti
viestissä:1169041453.5202.3.camel@zoli-desktop...
> On sze, 2007-01-17 at 15:23 +0200, William Stokes wrote:
>> Hello,
>>
>> I have a script which retrieves rows from DB and prints them to a user
>> editable form to a web page. I don't know how to print the form objects
>> so
>> that all editable fields are put to arrays for updating them to DB. I
>> have
>> tried to create arrays sarjanID[] etc. but havent got it to work...
>>
>> Pls. see code below.
>>
>> Thanks
>> -Will
>>
>> $sql = "SELECT * FROM x_kilpailu WHERE omistaja = '$updateid' ORDER BY
>> 'sorter' ASC";
>> $row = mysql_fetch_row($result);
>> $result = mysql_query($sql);
>> $num = mysql_num_rows($result);
>> $cur = 1;
>> while ($num >= $cur) {
>
> why don't you use this:
>
> while ($row = mysql_fetch_array($result)) {
>
> and then you can omit $cur, and also you won't need $num
>
>> $row = mysql_fetch_array($result);
>> $id = $row["id"];
>> $nimi = $row["nimi"];
>> $kilpailutyyppi = $row["kilpailutyyppi"];
>> $sarjataulukko = $row["sarjataulukko"];
>> $joukkueitakpl = $row["joukkueitakpl"];
>> $sorter = $row["sorter"];
>> $kilpailut[] = array("$id", "$nimi", "$kilpailutyyppi",
>> "$sarjataulukko",
>> "$joukkueitakpl", "$sorter"); //create arrays here
>> $cur++;
>> }
>>
>> ......
>>
>> foreach ($kilpailut as $value) { //print arrays to a web form
>> if ($value[3] == 1) {
>> $staulu = "Kyll";
>> } elseif ($value[3] == 0) {
>> $staulu = "Ei";
>> }
>>
>> print "<input type=\"hidden\" name=\"sarjanID[ID]\" value
>> =\"$value[0]\">\n";
>> print "<input type=\"text\" name=\"sarjanID[nimi]\"
>> size=\"20\"
>> maxlength=\"20\" value =\"$value[1]\">\n";
>> print "<select name=\"sarjanID[tyyppi]\">\n";
>> print "<option selected>$value[2]</option>\n";
>> print "<option>Sarja</option>\n";
>> print "<option>Cup</option>\n";
>> print "</select>\n";
>> print "<select name=\"sarjanID[sarjataulukko]\">\n";
>> print "<option selected>$staulu</option>\n";
>> print "<option>Kylla;</option>\n";
>> print "<option>Ei</option>\n";
>> print "</select>\n";
>> print "<select name=\"sarjanID[joukkueitayht]\">\n";
>> print "<option selected>$value[4]</option>\n";
>> for ($i=3; $i<=20; $i++ ) {
>> print "<option>$i</option>\n";
>> }
>> print "</select>\n";
>> print "<input type=\"text\" name=\"sarjanID[sortteri]\" value
>> =\"$value[5]\">\n";
>> print "<input type=\"checkbox\" name=\"poista\">Poista
>> kilpailu<br><br>\n";
>> }
>>
>
> all input fields go to $_POST/$_GET depending on the form method you
> use. so you don't have to create an array in the form itself.
> just do it like
>
> <select name=\"tyyppi\">
>
> and then you will have that value in $_POST['tyyppi'] or $_GET['tyyppi']
>
> greets
> Zoltán Németh