Results 1 to 2 of 2

Thread: Help With Arrays

  1. #1
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Arrays

    So I have a portion of my form that has three rows and four columns. The form is asking my users to enter activities or jobs they have been involved in. They have to fill in at least one row but they don't have to fill in all three. Right now my code entered all three even if a row was blank. So i put a while loop in it. It skips the empty row now but the loop won't stop. I tried using a for loop but I still have the same problem. Here is my code.

    Code:
    //enter rows into database
    foreach($_POST['Activity'] as $row=>$Act)
    {
    while (!empty($_POST['Activity']))
    { 
    
      $Activity=($Act);
      $Position=($_POST['Position'][$row]);
      $StartDate=($_POST['StartDate'][$row]);
      $EndDate=($_POST['EndDate'][$row]);
    
      $involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate)
      VALUES ('$Activity','$Position','$StartDate','$EndDate')";
     
    
    
      if (!mysql_query($involv,$con))
      {
        die('Error: ' . mysql_error());
      }
      echo "$i<br/>";
      }
     
    
    }

    Here is my form:
    Code:
    <body>
    <form action="insert.php" method="post">
    <table width="77%">
     <td height="63" colspan="5"><h3>Other involvement during high school, college (clubs, sports, work, volunteer, etc.): </h3></td>
        </tr>
        <tr>
          <td width="20%"><h3>Activity</h3></td>
          <td width="19%"><h3>Position</h3></td>
          <td width="23%"><h3>Start Date</h3></td>
          <td width="25%" height="60"><h3>End Date</h3></td>
        </tr>
        <tr>
          
          <td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" />
          <td height="63"><input name="Position[]" type="text" id="Position[]" size="15" />
          <td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" />
          <td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" />
        </tr>
        <tr>
          
          <td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" />
          <td height="63"><input name="Position[]" type="text" id="Position[]" size="15" />
          <td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" />
          <td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" />
        </tr>
        <tr>
          
          <td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" />
          <td height="63"><input name="Position[]" type="text" id="Position[]" size="15" />
          <td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" />
          <td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" />
    </table>
    <p>&nbsp;</p>
    <p>
      
      <input type="submit" name="Submit" id="Submit" value="Submit" />
      
    </p>
    </form>
    </body>
    </html>

    Please Help. Thankyou in advance

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Unless you unset the items in your array, it will never be empty, thus, your while loop always returns true. Try unseting your values after you are done using them. Or you can use array_shift() to get the first value of an array and remove it.

    I haven't spent enough time looking at the logic behind your code to give you any great advice on what to change. But you definitely need to change the while loop because as it is written now, it will always return true.

Bookmarks

Posting Permissions

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