Log in

View Full Version : Help With Arrays



ladieballer2004
08-23-2009, 08:31 PM
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.




//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:


<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

JasonDFR
08-24-2009, 03:46 PM
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.