PDA

View Full Version : Auto-Generation of an Array



dmilani
08-01-2008, 11:19 PM
Hi,

I am trying to generate an array based on variables from a html form. For each checked box I want to create a record.

Is the following code idea feasible with just a syntax error?
I keep getting a parse error:
Parse error: syntax error, unexpected T_VARIABLE, expecting ')'



$a = array(
if (!$fall)
{
}
else
{"$fall"
}

if (!$winI)
{
}
else
{, "$winI"
{

if (!$winII)
{
}
else
{, "$winII"
}

if (!$sprg)
{
}
else
{, "$sprg"
}

) ;


foreach ($a as $session) {

/* Define insert query for registration table */
$sql3 = "INSERT INTO registration VALUES
('regID', '$league', '$teamID', '$reqdiv', '$misc', '$division', '$session', '$date')";

/* exec registration insert */
$result3 = @mysql_query($sql3) or die("Error #" . mysql_errno() . ":" . mysql_error() );

JohnDubya
08-02-2008, 12:20 AM
Your example is quite inefficient. You can just use [] after the name of a group of checkboxes to put all those checked into an array, then do a foreach() on that array. Like so:

HTML FORM


<form method="post" action="form.php">
<input type="checkbox" name="group[]" value="fall" />
<input type="checkbox" name="group[]" value="winI" />
<input type="checkbox" name="group[]" value="winII" />
<input type="checkbox" name="group[]" value="sprg" />

<input type="submit" name="submit" value="Submit" />
</form>


TOP OF form.php


if (isset($_POST['submit'])) {
$group = $_POST['group']

foreach ($group as $value) {
//do the insert query...the $value will equal the value you specified for each checked checkbox
}
}

dmilani
08-02-2008, 04:57 AM
Hey John,

Thanks for the suggestion. If nothing else I hope I provided some comic relief. Anyhow, I tried to implement your idea. I replaced the checkbox section of my form with your code. For some reason the checkboxes don't show up. Any thoughts about why? My HTML page is: HTML 4.01 Transitional

Is that compatible?

Thanks again. . .

Dan

JohnDubya
08-02-2008, 05:12 AM
Hmm, they should be showing up at least. I just created a page with HTML 4.01 Transitional and used the code, and it showed up. Try the code on a different page and see if it shows up for you. It could be a problem that is confined to the page you're working on, so let's at least remove that possibility.

dmilani
08-02-2008, 04:00 PM
Hey John,

I figured out the HTML problem. In the text I pasted, all the spaces were actually 's. not sure where they came from. . .

Anyhow, now the php is reporting a parsing error at the foreach function.
Parse error: syntax error, unexpected T_FOREACH in ****/Travel_alt.php on line 106

This error happens on the basic example and in my whole form. I've included the my whole so you can see the context of my code. . .


begin(); // begin transaction

/* exec contacts insert */
$result1 = @mysql_query($sql1) or die("Error #" . mysql_errno() . ":" . mysql_error() );

if (!$result1)
{
rollback(); // transaction rolls back
echo "you rolled back";

exit;
}
else
{
/* debug confirmation for contacts insertion */
echo "Contact insert worked <br>" ;


/* retrieve and define contactID */
$contactID = mysql_insert_id();


/* Define insert query for teams table */
$sql2 = "INSERT INTO teams VALUES
('teamID', '$name', '$age', '$club', '$contactID', '$fallleague', '$falldiv')";

/* exec teams insert */
$result2 = @mysql_query($sql2) or die("Error #" . mysql_errno() . ":" . mysql_error() );

if (!$result2)
{
rollback(); // transaction rolls back
echo "you rolled back";

exit;
}
else
{

/* debug confirmation for teams insertion */
echo "Team insert worked <br>" ;


/* retrieve and define teamID */
$teamID = mysql_insert_id();

if (isset($_POST['submit'])) {
$group = $_POST['group']

foreach ($group as $session) {
echo "$group" ;
echo "$session" ;


/* Define insert query for registration table */
$sql3 = "INSERT INTO registration VALUES
('regID', '$league', '$teamID', '$reqdiv', '$misc', '$division', '$session', '$date')";



/* exec registration insert */
$result3 = @mysql_query($sql3) or die("Error #" . mysql_errno() . ":" . mysql_error() );

if(!$result3)
{
rollback(); // transaction rolls back
echo "you rolled back";

exit;
}
else
{
commit(); //transaction is committed

/* debug confirmation for registration insertion */
echo "registration insert worked <br>" ;


} // end of third else
} // end of foreach
} // end if (isset. . .

} // end of second else

} // end of first else

JohnDubya
08-02-2008, 04:57 PM
Ok, a couple of things.

First, the reason it's not expecting a foreach() statement is because you don't have a semi-colon after setting the $group variable. It needs to be:


$group = $_POST['group'];

Also, with the if (isset($_POST['submit'])) { and the foreach(), you really need to stay consistent on opening/closing your brackets. You open and close your brackets in other statements, but not on these. It will help you a ton in the long run to pick a certain way of doing it and stay consistent to that.

You might also consider using an if (!empty($group)) before running the foreach() on it, just in case someone doesn't check any boxes at all.

dmilani
08-02-2008, 05:44 PM
Ok, a couple of things.

Also, with the if (isset($_POST['submit'])) { and the foreach(), you really need to stay consistent on opening/closing your brackets. You open and close your brackets in other statements, but not on these.

Are you saying one is not supposed to close these brackets or that I didn't close the brackets? I thought I did close the brackets at the bottom of that snippet after the query is executed.

FWIW, The script is working as expected.
I am going to attempt to implement your suggestion about using (!empty($group))

Thanks again for your tutoring.
dan

JohnDubya
08-02-2008, 07:39 PM
Are you saying one is not supposed to close these brackets or that I didn't close the brackets? I thought I did close the brackets at the bottom of that snippet after the query is executed.

Ahh, yeah, they are closed. I didn't see that the statements stretched down that far. Well, and you also use two methods of open/close brackets, and it's better to just use one, for consistency. (this is purely to help you and/or others read your code better...not for programming-sake)

Either use:

if (isset($_POST['submit'])) {

}

OR


if (isset($_POST['submit']))
{

}

Just FYI, I use the former, since it uses fewer lines. But it is also harder for some people to see that they have opened a bracket, since it is at the end of the line (I don't have that problem).

Fou-Lu
08-02-2008, 08:00 PM
To avoid a foreach notice on uninstantiated arrays check to ensure that the data is available and its an array:


if (isset($_POST['submit']))
{
if (isset($_POST['group']) && is_array($_POST['group']))
{
foreach ($_POST['group'] AS $checked)
{
}
}
}

Or


if (isset($_POST['submit']))
{
$group = (isset($_POST['group']) && is_array($_POST['group'])) ? $_POST['group'] : array();
foreach ($group AS $checked)
{
}
}

A foreach requires that the data be an array or it will trigger an error. Assigning a checkbox input provides no results if no checkboxes are selected. Check the existence and type to ensure you're handling the checkboxes as an array.