Jump to content

Declaring dynamic variables?


binxalot

Recommended Posts

Okay, I've stumped myself good on this one and I'm not too sure on how to do this since it seems that I might be at this for hours if I leave it to myself.

 

I have a form which displays a list of ads which are to run on a web site at specific times of the day. So the form outputs all of the day's ads in one list, each entry is given a dynamic name variable and each ad has a time input box in minutes and second like so:

 

echo"<input name=\"shour$pid\" type=\"text\" id=\"shour$pid\" value=\"$ahour\" size=\"3\" maxlength=\"2\">
                              : <input name=\"smin$pid\" type=\"text\" id=\"smin$pid\" value=\"$amin\" size=\"3\" maxlength=\"2\">
                              <input name=\"ap$pid\" type=\"hidden\" value=\"$ap\">$at "; 			  

 

The $pid is a number which increments by one for each ad entry. it gets added onto each of the fields. so the end product is a form with two text entries for hour and minute and a hidden form id # which references the database entry for each ad type

 

How do I process these dynamic variables in my form?  Since each of the variables can have any number attached to them, I need for each ad type to update a database with that specific ad's new time and date.  I've only ever made forms with hard coded variables so this is a whole new problem for me. 

Link to comment
https://forums.phpfreaks.com/topic/114594-declaring-dynamic-variables/
Share on other sites

Instead of creating the names the way you did, use arrays:

<?php
echo '<input name="shour[' .$pid . ']" type="text" id="shour_' . $pid . '" value="' . $ahour. '" size="3" maxlength="2">
                              : <input name="smin[' . $pid . ']" type="text" id="smin_' . $pid .'" value="' . $amin . '" size="3" maxlength="2">
                              <input name="ap[' . $pid . ']" type="hidden" value="' . $ap . '">' . $at;
?>

 

Then in the processing script you can do something like:

<?php
foreach ($_POST['shour'] as $pid => $val) {
    echo 'SHour: ' . $val . '<br>';
    echo 'SMin: ' . $_POST['smin'][$pid] . '<br>';
    echo 'Ap: ' . $_POST['ap'][$pid] . '<br>';
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.