PHP foreach? Definitely not while

Discussion in 'PHP' started by codeislife, Oct 29, 2010.

  1. #1
    Hey everyone! So I was working on a new thing and I ran into a problem. I have a page where a MySQL array is called and I get a list of names. After each name I need a list box and then at the end of all the names I need a submit button. The problem is that I need each individual answer in the list box to be posted to another page by the submit button...This is what I have (I know it's not right and will not work)

            while ($row = mysql_fetch_array($result)) {
                    echo '<form id="activate" name="activate" method="POST" action="chapter_activate1.php" target="_self">' . $row['firstname'] . " " . $row['lastname'];
                    echo '  <select name="edit_status" >
                                            <option value="approved" >Approve</option>
                                            <option value="reject">Reject</option>
                                    </select><br />';
            }
            echo '<input value="Submit" name="submit" type="submit" /></form>';
    Code (markup):


    from what I understand, I can use a foreach construct, but I do not understand how. Could someone point me in the right direction? Thanks.
     
    codeislife, Oct 29, 2010 IP
  2. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You have your open form tag inside the loop and the close tag outside the loop. So you are going to have multiple open tags, but only one close tag.

    while ($row = mysql_fetch_array($result)) {
    echo '<form id="activate" name="activate" method="POST" action="chapter_activate1.php" target="_self">' . $row['firstname'] . " " . $row['lastname'];
    echo ' <select name="edit_status" >
    <option value="approved" >Approve</option>
    <option value="reject">Reject</option>
    </select><br />';
    echo '<input value="Submit" name="submit" type="submit" /></form>';
    }

    This code will have a submit button for each select box. The user will only be able to edit one at a time.

    Also, I dont see any unique id in this code. You will not know what row to update in the database.

    If you want multiple select boxes, and only one submit button, then you need to write more code. Let me know if you want me to post an example.
     
    fr33lanc3, Oct 29, 2010 IP
  3. codeislife

    codeislife Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    An example would be great. What I am trying to get to in the end is an update on multiple database rows. So how could I get a unique variable to post to the next page for each row?
    an example would be awesome! thank you for your fast reply
     
    codeislife, Oct 29, 2010 IP
  4. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    take the form tags outside of the loop first.
    echo '<form id="activate" name="activate" method="POST" action="chapter_activate1.php" target="_self">';

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

    echo $row['firstname'] . " " . $row['lastname'];
    // change the select name to an array that includes the unique id
    echo ' <select name="edit_status['.$row['unique_id'].']" >
    <option value="approved" >Approve</option>
    <option value="reject">Reject</option>
    </select><br />';
    }
    echo '<input value="Submit" name="submit" type="submit" /></form>';




    Then, in your form post code where you would updated, you would do something like this:

    // loop over the array
    foreach ($_POST['edit_status'] as $unique_id){

    $qry = "update table
    set status = '".$_POST['edit_status'][$unique_id] ."' // this will be approved or reject
    where unique_id = $unique_id";
    mysql_query($qry);
    }

    This is quick and dirty, you would want to do some validation in here, but the general idea is to set the id as the key value in an array for the status and then loop over the array.
     
    fr33lanc3, Oct 29, 2010 IP
  5. codeislife

    codeislife Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think I understand your process behind this.

    I will try it out here within the next hour and let you know how it goes.

    Thank you for all of your help!
     
    codeislife, Oct 29, 2010 IP