Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2006
    Location
    Malvern, PA
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Auto-Generation of an Array

    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 ')'

    PHP Code:
        $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()         ); 

  • #2
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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
    PHP Code:
    <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
    PHP Code:
    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
       
    }


  • #3
    New Coder
    Join Date
    Nov 2006
    Location
    Malvern, PA
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #4
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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.

  • #5
    New Coder
    Join Date
    Nov 2006
    Location
    Malvern, PA
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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. . .

    PHP 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 

  • #6
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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:

    PHP Code:
    $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.

  • #7
    New Coder
    Join Date
    Nov 2006
    Location
    Malvern, PA
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JohnDubya View Post
    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

  • #8
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by dmilani View Post
    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:
    PHP Code:
    if (isset($_POST['submit'])) {


    OR

    PHP Code:
    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).

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    To avoid a foreach notice on uninstantiated arrays check to ensure that the data is available and its an array:
    PHP Code:
    if (isset($_POST['submit']))
    {
        if (isset(
    $_POST['group']) && is_array($_POST['group']))
        {
            foreach (
    $_POST['group'] AS $checked)
            {
            }
        }

    Or
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •