Forum Moderators: coopster

Message Too Old, No Replies

FROM multiple Dropdown list to array then INSERT to database

array

         

daniel35

6:29 am on Mar 17, 2011 (gmt 0)

10+ Year Member



Hi poeple!

I'am facing a serious problem with PHP.

I have a table with two column's.
In the first one there is generaited names of animals from database.
Beside names in the second columns of the table I have an select option dropdown list generaited from database with some values to chose like mammals, fish, insekts etc.
If we say there is 5 animals in the name column then it will pop up beside them on the other column of the table 5
the same select options with their values. I mean five select options in different rows but they are the same with the same values offcourse. My question is How do I collect those values from five different rows in array and then INSERT them to database? For animals id's I did like this,
To save them all populated id's of those 5 animals in array and insert them to database table with name "all_animals" I did
$sql_animals = "SELECT * FROM anilams WHERE group_id = 1 "; // grup_id means if the animals is from africa, Asia ....
$result = mysql_query($sql_animals);
while ($row = mysql_fetch_assoc($result)){

$animal_array[] = $row['animal_id'];
}

foreach($animal_array as $value)
{
$sql_stu = "INSERT INTO all_animals (animal_id) VALUES ('$value')";
mysql_query($sql_stu) OR die(mysql_error());

}
And all id's inserted to the table. But When I try to do the same with select options like this:

print '<select name="kindOfAnimal[]">';
$sql_animalType = "SELECT * FROM kindOfAnimal ";
$rst = mysql_query($sql_animalType);
while ($row = mysql_fetch_array($rst)){
print '<option value="'.$row['animalType_id'].'">'.$row['animalType_name'].'</option>';
$Type_array[] = $row['animalType_id'];

foreach($Type_array as $Type_value)
{

$sql_TypeOF = "INSERT INTO all_animals (animalType_id) VALUES ('$Type_value')";
mysql_query(sql_TypeOF) OR die(mysql_error());

// I tried even like below…..
print '<option value="'.$row['animalType_id'].'">'.$row['animalType_name'].'</option>';
// I tried even to echo of COUNTS($Type_array)…. Only one rows shows


}
}
print '</select>';
And inserts nothig. Only 0. But if I delete the bracket after the name of the select option.. I mean <select name="kindOfAnimal[]"> and writing instead <select name="kindOfAnimal"> then value of the last select option inserts but not thos others. I want actualy INSERT both id's of animals and kindOfAnimal to table "all_animals" in there by coding
$sql = "INSERT INTO all_animals (animal_id, animalType_id ) VALUES ('$value', '$Type_value')";
mysql_query(sql_TypeOF) OR die(mysql_error());
But as I said before all animal_id inserting but from select option side only the value of the last one inserts.
I hope you have the hole picture of the problem...
And Pls help.... How can I collect all values of the select options those who are in different rows of the table and INSERT them to database. Thank you for your time.

usmc wlh1975

10:51 am on Mar 17, 2011 (gmt 0)

10+ Year Member



The easiest way of dealing with the issue is to create a loop when building the initial dhtml objects so that each select receives a different name - like "name1", "name2", etc. You can do this quite simply by using the $$ symbol while building your dhtml form.

Your processing code on submission can use a similar technique to deal with the select names in the $_GET/$_POST global arrays. To make it easier, you could put the iteration count inside of a hidden field when you are building the dhtml initially.

Or by using a hidden field on your form which is meant to hold all of the selections made in a text field and using onblur or onclick to capture the information.

When initially producing the dhtml, you can make it so that each row in your table "knows" what row number it is and then concatenate that information with the selection that was made and then put it in a text field with a field separator symbol of your choosing. You end up with something like this "r1:Elephant:Africa;r2:lion:North America;r3:lion:Africa;....

then you can parse them back into separate fields prior to inserting them into the database.

daniel35

4:00 pm on Mar 17, 2011 (gmt 0)

10+ Year Member



Hi usmc wlh1975!
Thank you for your quick response. Let me descripe furthere...
here in header there is javascript functions to change the side .....
To populate data based on input I use javascript like this in the first page...

<select name="animalGroup" onchange="htmlData('animal_list.php', 'ch='+this.value)" >
<option value="pick subject">Chose animal group</option>
<?php
$sql_g = " SELECT * FROM animalgroup ORDER BY group_name ASC";
$res = mysql_query($sql_g);
while ($row_g = mysql_fetch_array($res)) {
print '<option value="'.$row_g['group_id'].'">'.$row_g['group_name'].'</option>';
}

?>
</select>*

<div id="txtResult">
<input type="hidden" name="animal_id" />


<select name="kindOfAnimal[]" multiple="multiple">
<option> </option>
</select>*
</div>


and it populate a list of groups in animal_list.php.
When I chose the group then the table populats with names of animals and select options. How many select options pop up depends on how many animals in chosen group they are.
So far no problem.
I dont have any form tags in animal_list.php, just a table and following code.
<?php
if (isset($_GET['ch']) && $_GET['ch'] != "" && is_numeric($_GET['ch']) && $_GET['ch'] >= 1) {

$cID = (integer)$_GET['ch'];

$sql = "SELECT * FROM anilams WHERE group_id = ".$cID." ";

$res = mysql_query($sql);

echo "<table width='100%' border='1' cellspacing=0 cellpadding=2 class='dtable' align='left' id='abs'>
<tr>
<th width='auto'>name</th>
<th width='auto'>Kind of</th>
</tr>";


while ($row = mysql_fetch_array($res)){

print '<tr>'.
'<td nowrap>'.
'<p'.$row['animal_id'].'">'.$row['name'].'</p>'.
'<input type="hidden" name="animal_id" value="'.$row['animal_id'].'">'.
'</td>'.

'<td nowrap>'.
// '</select>';
'<select name="kindOfAnimal[]">';
$sql_animalType = "SELECT * FROM kindOfAnimal ";
$rst = mysql_query($sql_animalType);
while ($row = mysql_fetch_array($rst)){
print '<option value="'.$row['animalType_id'].'">'.$row['animalType_name'].'</option>';
$Type_array[] = $row['animalType_id'];

foreach($Type_array as $Type_value)
{

$sql_TypeOF = "INSERT INTO all_animals (animalType_id) VALUES ('$Type_value')";
mysql_query(sql_TypeOF) OR die(mysql_error());

// I tried even like below…..
print '<option value="'.$row['animalType_id'].'">'.$row['animalType_name'].'</option>';
// I tried even to echo of COUNTS($Type_array)…. Only one rows shows

}
}
print '</select>'.
'</td>'.
'</tr>';
}
print '</table>';

I'am new user of PHP and I wish I could understand what you mean on your description becouse it's useful. I would realy apricate if you descripe more detail with writing your codes, like "do like this...." b/c I'am dummy.....;)
Thank you again for your quick response.