Jump to content

Recommended Posts

Hi Guys need some help with this:

On my first page I generate a form with a while loop from a table with employees.

I’m not going to paste my whole code for fast reading:

While($myrow = $result ->fetch_assoc()){
$emp_id = $myrow[“id”];
$emp_name = $myrow[“name”];
$display  .= <<<EOF_
<input type="hidden" name="monday_$emp_id" id=" monday_$emp_id" value="$monday" />
<input type="hidden" name="tuesday_$emp_id " id="tuesday_$emp_id" value="$tuesday " />
EOF_;
}

I give each field the name ie. monday_$emp_id to give them each unique names:

My question is how do I recall those variables  on my input page?

On my input page I use a foreach to get all the variables but need to enter them into a table with the fields (emp_id, monday,tuesday) etc.

The problem is that the variables on the input page is ie. $Monday_23 and the number 23 can be anything depending on the employee id. I have managed with explode() to get the emp_id and tried to use $Monday = $Monday_{$emp__id}; but that doesn’t seem to work.

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/254500-passing-variables/
Share on other sites

The code is irrelevant to question really, All I want to know if there is a way to echo or use the variable if you have the following:

In a nut shell this is what I have.

 

A textField called Monday_$emp_id in a while loop. Lets say I’m working with the 1st employee for the example. So on my input page I will have this.

 

$Monday_1 = “100”;  I know I have a variable on the input page called $Monday_unknownNumber but the unknownNumber is the id of the employee so I can get it.

 

$emp_id=”1”; The unknown number above is always the id of an employee so I can get it. So for me to echo it or use it I need to do something like the following.

 

echo “$Monday_{$emp_id}”; this is the part I’m looking for, this code obviously doesn’t work.

 

Link to comment
https://forums.phpfreaks.com/topic/254500-passing-variables/#findComment-1304998
Share on other sites

As Muddy_Funster stated you should create the form fields as arrays with the IDs as the indexes for the array. However, what you are doing for the id parameter of the fields is fine since you don't access those on the PHP receiving page. Also, no need to assign the database fields to a temporary variable if you are not going to use it later - just use the array value in the output. Example format:

While($myrow = $result ->fetch_assoc()){
$emp_name = $myrow['name'];
$display  .= <<<EOF_
<input type="hidden" name="monday[{$myrow['id']}]" id=" monday_$emp_id" value="$monday" />
<input type="hidden" name="tuesday[{$myrow['id']}] " id="tuesday_$emp_id" value="$tuesday " />
EOF_;
}

 

Now you can access all the "monday" values using a foreach loop such as

foreach($_POST['monday'] as $empID => $mondayValue)
{
    //
}

Link to comment
https://forums.phpfreaks.com/topic/254500-passing-variables/#findComment-1305043
Share on other sites

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.