Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php array 1

Status
Not open for further replies.

kurie

Programmer
Jun 4, 2008
170
ZA
im new to php but i need help on how to save array values to a database
i have been working with .net framework and i can implement this idea with ease in c#,vb.
i have a form and on the form i have a number of controls, and im storing data from the from onto the database,
say i have employee details(dependentname, depage) and a button called "addanother dependent". on clicking add another dependent i would like to add dependentname and depage to the array and maybe four more dependants and on save i would like to save all the five dependants to the database
i cant use the $Post array beacause it hold only one dependent at a time, does anyone know how to do this.

Regrads
 
Hi

kurie said:
i cant use the $Post array beacause it hold only one dependent at a time
Wrong. The [tt]$_POST[/tt] array contains data of all fields sent with [tt]POST[/tt] method.

If you are in doubt regarding the content of an array, just display it. Maybe just the array has more dimensions than you expected.
Code:
[url=http://php.net/print_r/]print_r[/url]($_POST);
If the additional information is indeed not present, that means it was not sent. So first the client-side code has to be debugged.

Feherke.
 
hie thanks for your response

are you saying the $_post arrays can hold

dependant1 age1
dependent2 age2
dependent3 age3
...
dependet6 age6
at the same time and i will have to do something like (C# biased)

for i = 0; i < $_PPost.length; i++
{
mysql_Upadte = values(Post["Dependent"], Post["Age"]
}

 
Hi

Yes, it can hold them all. Could you tell us more about the JavaScript which generates the additional [tt]input[/tt]s ? The [tt]$_POST[/tt] array's structure depends on the used naming.

Feherke.
 
hi, Thanks
i wasnt thinking of using javascript for that,
i wanted to clear the original textboxes on clicking the addbuton(ie after adding the text to the $_Post array) so that i can type in additional info. to the textboxes

do i have to use javascript to create addiotional input controls. Or can i create a multi dimensional array fro this.
 
Hi

Oops. Then I misunderstood you. My idea was that you are trying to do something like this :
Code:
# Name      Age
1 [highlight yellow]         [/highlight] [highlight yellow]         [/highlight]

[highlight silver] Submit [/highlight] [highlight silver] Reset [/highlight] [highlight silver] Add [/highlight]
Code:
# Name      Age
1 [highlight yellow]         [/highlight] [highlight yellow]         [/highlight]
2 [highlight yellow]         [/highlight] [highlight yellow]         [/highlight]
3 [highlight yellow]         [/highlight] [highlight yellow]         [/highlight]
4 [highlight yellow]         [/highlight] [highlight yellow]         [/highlight]
5 [highlight yellow]         [/highlight] [highlight yellow]         [/highlight]

[highlight silver] Submit [/highlight] [highlight silver] Reset [/highlight] [COLOR=gray silver] Add [/color]
kurie said:
i wanted to clear the original textboxes on clicking the addbuton(ie after adding the text to the $_Post array) so that i can type in additional info. to the textboxes
That is also a possibility. You can do it in different ways :
[ul]
[li]the Add button submits the [tt]form[/tt] and clear the [tt]input[/tt]s; the PHP script returns 204 No Content[/li]
[li]the Add button sends the data using Ajax and clears the [tt]input[/tt]s[/li]
[li]the Add button puts the data into a JavaScript [tt]Array[/tt] and clears the [tt]input[/tt]s, then the Submit button builds up a request from those data[/li]
[/ul]
kurie said:
do i have to use javascript to create addiotional input controls.
Implementing my theory of using the Adda button, yes.
kurie said:
Or can i create a multi dimensional array fro this.
The [tt]$_POST[/tt] will reflect the naming used when creating the [tt]input[/tt]s :
[ul]
[li]if the [tt]input[/tt]s have distinct [tt]name[/tt]s like Dependent1, Dependent2, ... Dependent5, there will be separate elements in the [tt]$_POST[/tt][/li]
[li]if the [tt]input[/tt]s have identical [tt]name[/tt]s like Dependent[] ( note the brackets ! ), they will be placed in an array in the [tt]$_POST[/tt][/li]
[/ul]

Feherke.
 
Hi

Regarding the solution I described ( and "depicted" ) above.
HTML:
<form action="" method="post" name="dependent">

<table id="list">
<tr><th>#</th><th>Name</th><th>Age</th></tr>
<tr><td>1</td><td><input type="text" name="name[]"></td><td><input type="text" name="age[]"></td></tr>
</table>

<input type="submit" value="Submit">
<input type="reset" value="Reset">
<input type="button" name="add" value="Add" onclick="more()">

</form>
JavaScript:
function more()
{
  if (document.dependent['name[]'].length>=5) return

  var list=document.getElementById('list')
  var ref=list.rows[list.rows.length-1]
  var row=list.insertRow(list.rows.length)
  for (var i=0,l=ref.cells.length;i<l;i++) {
    row.insertCell(row.cells.length).innerHTML=ref.cells[i].innerHTML
  }
  row.cells[0].innerHTML=parseInt(row.cells[0].innerHTML)+1

  if (document.dependent['name[]'].length>=5) document.dependent.add.disabled=true
}
And if you fill the [tt]form[/tt] with the following test data :

[tt][gray]# Name Age[/gray]
[gray]1[/gray] One 11
[gray]2[/gray] Two 22
[gray]3[/gray] Three 33[/tt]

In PHP the [tt]$_POST[/tt] array will hold the data like this :
Code:
Array
(
    [name] => Array
        (
            [0] => One
            [1] => Two
            [2] => Three
        )

    [age] => Array
        (
            [0] => 11
            [1] => 22
            [2] => 33
        )

)
So your rewritten [tt]for[/tt] should be :
PHP:
for ($i=0;$i<count($_POST['name']);$i++) {
  mysql_Upadte($_POST['name'][$i],$_POST['age'][$i]);
}
Tested with Mozillas ( FireFox, SeaMonkey), Opera, Midori and Konqueror, but should work in other browsers too.

Feherke.
 
this is it, thanks very much
i really appreciate your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top