Jump to content

Help using looping array insert values into different array


Recommended Posts

I'm working on a script that pulls a league schedule and then lets the user edit it and update it back into the mySQL database. The part I'm having trouble with is passing some of the array variables into the next page...

 

The loop that outputs the schedules does so by each gametime... for instance, if there are games at 6, 6:30, 7 and 7:30, it'll output the following for each time:

 

6 PM

Field #1

Team 1

vs.

Team 2

Field #2

Team 1

vs.

Team 2

Field #3

Team 1

vs.

Team 2

 

6:30 PM

Field #1

Team 1

vs.

Team 2

Field #2

Team 1

vs.

Team 2

Field #3

Team 1

vs.

Team 2

 

Currently the setup for each time is as such (which does nothing):

<input type="gametime[]" value="<? echo $better_gametime ?>" size="10">

 

Within each of these tables containing the schedule, each team is defined by:

<select name="slot[<?php echo($sid); ?>|<?php echo($fld); ?>|<?php echo($better_gametime); ?>|a]">Team 1</select>

 

With $sid being the schedule's ID, the $fld being the field number, the $gametime being the time of the game and the "a" being whether it's team 1 or team 2...

 

And on the page to update the whole thing:

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 
   
mysql_query("	UPDATE schedules SET 
		league_date	= '$date', 
		league		= '$league', 
		park_id		= '$location', 
		week		= '$week', 
		notes		= '$notes', 
		field		= '$fld', 
		gametime	= '$better_gametime', 
		team		= '$value', 
		a_or_b		= '$team' 
		WHERE schedule_id	= '$schedule_id'") 
		or die(mysql_error());   

 

So the question is how I get the new value the user inputs into that above input to feed into the array for the teams within its greater loop...

 

Hopefully that makes sense and any help is greatly appreciated. Thanks!

I'm not sure why you have all those details packing into the "<select"

what parts do you want to update ? (what's updatable by the user?)

 

If I am looking at it correctly then I would probably do something like this

<form >
<!-- loop thought schedules -->
6 PM <br>
<input type="hidden" name="slot[]" value="<?php echo $sid;?>">
<input type="text" name="field[<?php echo $sid;?>]" value="Field #1">
<input type="text" name="TeamA[<?php echo $sid;?>]" value="Team 1"> vs. 
<input type="text" name="TeamB[<?php echo $sid;?>]" value="Team 2"><br>

<!--
6:30 PM <br> details pulled from the database etc etc etc 
-->

<!-- end loop -->

</form>

 

foreach($_POST['slot'] as $sid){
    echo "Field ".$_POST['field'][$sid];
    echo "Team1 ".$_POST['TeamA'][$sid];
    echo "Team2 ".$_POST['TeamB'][$sid];
}

 

Note this is just a basic version and the hidden field isn't really needed but i kept it to make it simpler

Insert Quote

I'm not sure why you have all those details packing into the "<select"

what parts do you want to update ? (what's updatable by the user?)

 

Let me try to use more code to exemplify what's going on with a picture...

 

pic.png

 

You can see the things that you can edit are the game times and the various teams. I had some help setting up the initial array to build the schedule in a thread here, which is why everything is packed into the slot array.

 

But I'm unsure how to get that time to adjust itself to the new value if it's changed?

 

Does that make more sense? Thanks for the help!

okay to update the time, you need to do 2 things

1. update the form

from

<input type="gametime[]" value="<? echo $better_gametime ?>" size="10">

to (PS i assume type should of been name)

<input name="gametime[<?php echo($sid); ?>]" value="<? echo $better_gametime ?>" size="10">

 

2. grab the new value

   list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key);
   $better_gametime = $_POST['gametime'][$schedule_id]; //<---ADD this line

 

Okay this is kinda strange as you are pulling the value twice but the exploded one is static so you replace it with the dynamic one

okay to update the time, you need to do 2 things

1. update the form

from

<input type="gametime[]" value="<? echo $better_gametime ?>" size="10">

to (PS i assume type should of been name)

<input name="gametime[<?php echo($sid); ?>]" value="<? echo $better_gametime ?>" size="10">

 

2. grab the new value

   list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key);
   $better_gametime = $_POST['gametime'][$schedule_id]; //<---ADD this line

 

Okay this is kinda strange as you are pulling the value twice but the exploded one is static so you replace it with the dynamic one

 

The trouble with your solution is that the $sid isn't defined until later in a different loop...

 

What I did come up with, was switching the input to:

<input type="text" name="gametime[<? echo $better_gametime ?>|t]" value="<? echo $better_gametime ?>" size="10"><br/>

 

And on the processing page to:

foreach($_POST['gametime'] as $time => $times) {
list($better_gametime,$t) = explode('|',$time);

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 

if ($orig_gametime == $better_gametime) $better_gametime = $times;

mysql_query("	UPDATE schedules SET 
		league_date	= '$date', 
		league		= '$league', 
		park_id		= '$location', 
		week		= '$week', 
		notes		= '$notes', 
		field		= '$fld', 
		gametime	= '$better_gametime', 
		team		= '$value', 
		a_or_b		= '$team' 
		WHERE schedule_id	= '$schedule_id'") 
		or die(mysql_error());     


}

}

 

Which, when echoed, correctly shows the old time and the new value... so my next step was to insert the:

	if ($orig_gametime == $better_gametime) $better_gametime = $times;

 

To try and simply say... if the original time matches the gametime in the slot entry, then change the value of the variable to this new time... but it doesn't work -- nothing changes at all.

 

Any ideas?

 

Thanks for the help!

I found the mistake in the if statement... I didn't define the $orig_time variable anywhere... so I changed the top foreach to:

 

foreach($_POST['gametime'] as $time => $times) {
list($orig_gametime,$t) = explode('|',$time);

 

But still no dice...

It won't work because its has no way of linking back to the sid,

I'm sorry but i don't have a very clear picture, of how your system should work.. it does seam like its becoming spaghetti code.

 

this maybe a good time to take a step back and review it as a whole!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.