Page 1 of 1

Need help with looping through post array

Posted: Fri Jul 09, 2010 9:13 am
by mikeman
I have two tables in my database:
activity
and
activity_options

Say activity contains: Exercise with id of 1
and the activity_options for that Exercise are stored in activity_options - ie running, jumping, skipping. These activity_options all have the same activity_id of 1 so I can link them to each other.

Now I am outputting a form so users can Update any names of either the Activity or the options, but because a random number of options can be associated with 1 activity I made a loop that outputs a form. After this is where I am getting stuck. I have output the form ok but when I post the option_id and option name I am struggling to get my head around how to process the results as assigning the id to a variable doesn't work as there is a random number of option id's!

Code: Select all

echo "Activty id = " . ($_POST['selectactivity']);
echo '<form action="edit_activity" method="post" name="Edit Activity">';
echo 'Activity:' . "<br />";
echo '<input name="event" value="' . $row['event'] . '" /><br /><br />';
echo '<input name="id" type="hidden" value="' . $row['id'] .'" />';

echo '<br />Options:<br />';
while ($row = mysqli_fetch_array($result2)) {
echo '<input name="'. $row['id'] .'" value="' . $row['options'] . '" /><br /><br />';
}

echo '<br /><p><input type="submit" name="submit" value="Save" /></p></form>';

This code updates the activity fine but how do I then do the events!??

Code: Select all

$activityid = $_POST['id'];
$event = $_POST['event'];

$sql = sprintf("UPDATE activity SET event='%s' WHERE id='%s LIMIT 1'", $event, $activityid);
					
if (!mysqli_query($dbc, $sql))
  {
  die('<br /><br />Error!');
  }
 

Re: Need help with looping through post array

Posted: Fri Jul 09, 2010 9:29 am
by eruna
Can you show the structure of your tables and a print_r($_POST) so we can better understand your question?

Re: Need help with looping through post array

Posted: Fri Jul 09, 2010 9:38 am
by mikeman
Sure, my tables are as follows:

activity
col1 = id
col2 = event

activity_option
col1 = id
col2 = activity_id
col3 = options

Print_r coming out like this:
Array ( [event] => Excercise [id] => 1 [2] => Running [3] => Jumping [4] => Skipping [submit] => Save )