Closed Thread Icon

Topic awaiting preservation: How to create MySQL rows using individual array entry items... Pages that link to <a href="https://ozoneasylum.com/backlink?for=26448" title="Pages that link to Topic awaiting preservation: How to create MySQL rows using individual array entry items..." rel="nofollow" >Topic awaiting preservation: How to create MySQL rows using individual array entry items...\

 
Author Thread
Kristo
Nervous Wreck (II) Inmate

From:
Insane since: Oct 2004

posted posted 08-15-2005 00:21

I'm building a form and using PHP to create text fields based on a user selection. I want the individual to select the number of children in their family from a dropdown, then return the page & auto-create "Child x Name" and "Child x Age" fields per the number of children they selected. Here's the portion of code:

code:
<form name="children_number" method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>?children=self">

... Using javascript to submit the form when the number of children is selected from the dropdown menu. New form uses this number to auto-create the fields as follows:

// Create child fields from number of children selected on dropdown menu
if (isset($children)) {
	$number = $_POST['children_number'];
	for ($j=1; $j<$number+1; $j++) {
		echo ("\t\t\t\t<tr>\n");
		echo ("\t\t\t\t\t<td>&nbsp;&nbsp;&nbsp;Child " . $j . " Name</td>\n");
		echo ("\t\t\t\t\t<td width=\"220\"><input type=\"text\" name=\"child_name[]\" size=\"30\"></td>\n");
		echo ("\t\t\t\t</tr>\n");
		echo ("\t\t\t\t<tr>\n");
		echo ("\t\t\t\t\t<td>&nbsp;&nbsp;&nbsp;Child " . $j . " Age</td>\n");
		echo ("\t\t\t\t\t<td width=\"220\"><input type=\"text\" name=\"child_age[]\" size=\"30\"></td>\n");
		echo ("\t\t\t\t</tr>\n");
	}
}



This works well. What I'm wanting to do, however, is to take values from each & add them to individual rows per MySQL. For example, I would create a new row in let's say the CHILD table using child_name[0] as the child's name and child_age[0] as the child's age, then create the next row using child_nament and child_agent. Is this possible? Been searching the Internet & can't find a thing that is helping. How can I take individual values like this from an array and use them to create different rows?

Is there a better way to do this?

(Edited by Kristo on 08-15-2005 00:22)

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 08-15-2005 01:13

Something like this?

code:
// Connection to database is allready open.
foreach ($child_name AS $key=>$val){
    $query = "INSERT INTO `child` (`parent_id`,`child_name`,`child_age`)"
     ." VALUES ('$parent','$child_name[$key]','$child_age[$key]')";
    if (mysql_query($query)) echo "Child $key was added to the database!";
     else echo "Child $key was not added to the database! MySQL said: ".mysql_error();
}


(parent_id is the associated parent of the children being added, $parent is assumed to be allready specified.)



(Edited by zavaboy on 08-15-2005 01:18)

Kristo
Nervous Wreck (II) Inmate

From:
Insane since: Oct 2004

posted posted 08-15-2005 01:51
quote:
(parent_id is the associated parent of the children being added, $parent is assumed to be allready specified.)



zavaboy -

Thanks - I'll try this & see if it works. I'm just learning how to use arrays - would you explain how I would associate parent_id to each of these?

Thanks again for your help!

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 08-15-2005 02:36

Well, you have two database tables, let's assume you have the database 'kristo_people' and the tables 'parents' and 'children'. You can associate them with an id, the parent id.

code:
// The tables would be like:
CREATE TABLE `parents` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`mr_name` VARCHAR( 30 ) NOT NULL ,
`mrs_name` VARCHAR( 30 ) NOT NULL ,
`last_name` VARCHAR( 50 ) NOT NULL ,
`description` TEXT NOT NULL ,
`joined` TIMESTAMP NOT NULL ,
PRIMARY KEY ( `id` )
);
CREATE TABLE `children` (
`parent_id` INT UNSIGNED NOT NULL ,
`name` VARCHAR( 30 ) NOT NULL ,
`age` TINYINT( 2 ) UNSIGNED NOT NULL
);


Now with values in the tables...

code:
INSERT INTO `parents` ( `id` , `mr_name` , `mrs_name` , `last_name` , `description` , `joined` )
VALUES (
'', 'Joe', 'Sarah', 'Smith', 'They grew up in a small town.', NOW( )
), (
'', 'David', 'Donna', 'Brown', 'They grew up in a large city.', NOW( )
);
INSERT INTO `children` ( `parent_id` , `name` , `age` )
VALUES (
'1', 'Ken', '11'
), (
'1', 'Tyler', '14'
), (
'2', 'Billy', '10'
), (
'2', 'Jimmy', '12'
), (
'2', 'Tommy', '23'
);


Now we can use PHP code like this:

code:
// Allready connected to database...
$query1 = "SELECT * FROM `parents`";
$result1 = mysql_query($query1);
while ($row1 = mysql_fetch_array($result1,MYSQL_ASSOC)){
    $children = array();
    $query2 = "SELECT * FROM `children` WHERE `parent_id`='$row1[id]' ORDER BY `name` ASC";
    $result2 = mysql_query($query2);
    while ($row2 = mysql_fetch_array($result2,MYSQL_ASSOC)){
        $children[] = "$row2[name] ($row2[age])";
    }
    echo "<p>$row1[mr_name] &amp; $row1[mrs_name] $row1[last_name] have the children "
     .implode(", ",$children).".<br />\n$row1[description]</p>\n";
}


This should print something like:

code:
<p>Joe &amp; Sarah Smith have the children Ken (11), Tyler (14).<br />
They grew up in a small town.</p>
<p>David &amp; Donna Brown have the children Billy (11), Jimmy (14), Tommy (23).<br />
They grew up in a large city.</p>


Help?

Kristo
Nervous Wreck (II) Inmate

From:
Insane since: Oct 2004

posted posted 08-15-2005 04:11

Yeah, that explains it really well. I tried implementing it, but am getting the following error message:

quote:

Warning: Invalid argument supplied for foreach() in /****/join_complete.php on line 40




Here's my code.

code:
$doc_root = $_SERVER['DOCUMENT_ROOT'];

// Start session
session_start;

// Connect to the database
include ("$doc_root/includes/connect.php");

// Define variables
$primary_adult_name = $_SESSION['primary_adult_name'];
$secondary_adult_yn = $_SESSION['secondary_adult_yn'];
$secondary_adult_name = $_SESSION['secondary_adult_name'];
$children_number = $_SESSION['children_number'];
$child_name = $_SESSION['child_name'];
$child_age = $_SESSION['child_age'];
$receive_reminder_yn = $_SESSION['receive_reminder_yn'];
$reminder_frequency = $_SESSION['reminder_frequency'];
$username = $_SESSION['username'];
$password = $_SESSION['password'];
$email = $_SESSION['email'];
$church_code = $_SESSION['church_code'];

// Add family to database
$sql = "INSERT INTO heart_family VALUES ('', '$primary_adult_name', '$secondary_adult_yn', '$secondary_adult_name', '$children_number', '', '$church_code', '$receive_reminder_yn', '$reminder_frequency')";
$result = mysql_query($sql) or die ("<p class=blue>Error adding family: mysql_error() .</p>");

// Get family_id from new record
$sql = "SELECT * FROM heart_family WHERE primary_adult_name = '$primary_adult_name'";
$result = mysql_query($sql) or die("Couldn't retrieve family.");

while($row = mysql_fetch_array($result))
{
	$family_id = $row['family_id'];
	$_SESSION['family_id']=$family_id;
}

// Add children to database
foreach ($child_name AS $key=>$val) {
    $query = "INSERT INTO heart_child (child_id, child_name, child_age, family_id) VALUES ('', '$child_name[$key]','$child_age[$key]', '$family_id')";
    if (mysql_query($query)) echo "Child $key was added to the database!";
     else echo "Child $key was not added to the database! MySQL said: ".mysql_error();
}



Any ideas?

Kristo
Nervous Wreck (II) Inmate

From:
Insane since: Oct 2004

posted posted 08-15-2005 04:30

It worked!

The problem I was getting was due to not starting the session correctly - I was calling...

code:
// Start session
session_start;



... instead of...

code:
// Start session
session_start();



Man - it's always the little things isn't it? It's working perfectly now - thanks again!

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 08-15-2005 17:48

I'm glad I could help.

« BackwardsOnwards »

Show Forum Drop Down Menu