Incrementing number in variable name - php

Background
I have a for loop creating a table of inputs for an html form:
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo">Foo</option>
<option value="bar">Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="" name="bar_'.$i.'">';
}
On submit, this populates a database.
Problem
Each submission needs to be editable. When I return to the form (as an admin) I need to see everything that was stored in the database by a particular user.
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo"';
if($row['waldo_'.$i] == "foo") echo " selected='selected'";
echo '>Foo</option>
<option value="bar"';
if($row['waldo_'.$i] == "bar") echo " selected='selected'";
echo '>Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="'./*...*/.'" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="'./*...*/.'" name="bar_'.$i.'">';
}
My select properly "selects" the correct option, but I don't seem to be able to populate the text input values in a similar manner.
Somehow I need to echo the content in $foo_1, $foo_2, $foo_3, ..., $foo_x.
I have tried using $foo_.$i but that doesn't seem to work.
Is there a simple solution to this problem? Or is there a better method to format everything?

If I'm not misunderstanding your question:
$_POST["foo_".$i]
Should show you the submitted data.
EDIT: Or maybe this is what you are looking for?
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo"';
if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "foo") echo " selected='selected'";
echo '>Foo</option>
<option value="bar"';
if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "bar") echo " selected='selected'";
echo '>Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="';
if(isset($row['foo_'.$i]) && $row['foo_'.$i] != "") echo $row['foo_'.$i];
echo '" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="';
if(isset($row['bar_'.$i]) && $row['bar_'.$i] != "") echo $row['bar_'.$i];
echo '" name="bar_'.$i.'">';
}

Related

Option value selected for loop

I am trying to create a selected value of the previous output.
(the user fills in a form , submits, returns to the form) And I want the selected value to be the value the user previously used.
The vraagNummer and answer are given in the url parameter and the dropdown menu of items available in the list are created in a for loop.
Now I got stuck on that part.. How do I create an option selected value if it is created in a for loop?
Usually I would just put in option value = $vraagNummer selected> <?php echo $vraagNummer ?></option but in this case that wouldn't work. I suppose?
Anyone knows how to fix this?
Same with the A/B/C/D. How do I put in the selected value of answer into the value previously selected while still keeping the others as an option.
With kind regards,
if(!empty($_GET['vraagnummer'])){
$vraagNummer = $_GET['vraagnummer'];
if(!empty($_GET['answer'])){
$answer = $_GET['answer'];
}
}
echo $vraagNummer;
echo $answer;
?>
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
?><option value="<?php echo $i ?>">Question <?php echo $i?></option>
<?php
}
?>
</select>
<select name="result">
<option value="A">Answer A</option>
<option value="B">Answer B</option>
<option value="C">Answer C</option>
<option value="D">Answer D</option>
<input type="submit" id="submit" value="Submit" />
</form>
You simply test the $_GET['question'] against the current $i value in the loop using a ternary if
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
$sel = (isset($_GET['question']) && $_GET['question'] == $i) ? 'selected="selected"' : '';
echo '<option ' . $sel . ' value="' . $i .">Question ' . $i . '</option>';
}
?>
</select>
Warning:
Also be careful with the use of empty() when the variables can contain a zero as zero is considered as empty by the empty() function
Actually I would do your other dropdown like this
<select name="result">
foreach (range('A', 'D') as $char) {
$sel = (isset($_GET['result']) && $char == $_GET['result']) ? 'selected="selected"' : '';
echo "<option $sel value='$char'>Answer $char</option>";
}
</select>

Select-form, make the option stay selected after submit

I have this form:
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
echo '<option value=' . $hej . '>' . $hejsan[$hej] . '</option>';
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
When I press the submit button the select/option will reset to the default, how can I change this and make it stay as the one I selected?
Try this
if (isset($_POST['taskOption'])) {
$Option = $_POST['taskOption'];
}
while($hej < count($hejsan)) {
if($hej==$_POST['taskOption']){
$selected = "selected=selected";
}else{
$selected = "";
}
echo '<option value="' . $hej . '" '.$selected .'>' . $hejsan[$hej] . '</option>';
$hej++;
}
You will need to look at the option you have selected and make the corresponding option selected. Since your form leads to the same page, you can just look at the contents of $_POST.
P.S.: also, you will find it useful to echo a newline after each option to make the output more readable.
Try this:
<?php
$taskOption = '';
if (isset($_POST['taskOption']) {
$taskOption = $_POST['taskOption'];
}
?>
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
if ($hey == $taskOption) {
echo "<option value={$hey} selected>{$hejsan[$hej]}</option>";
} else {
echo "<option value={$hey}>{$hejsan[$hej]}</option>";
}
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
At a if else statement to it. If the option value is equal to the select, then add the selected="selected"
This is an example code. could be that the operator is wrong, im always confused with the greater then and smaller then operators.
<?php
$option_value = 5;
for($i=0; $i > 10; $i++){
if($option_value == 5){
echo '<option value="'.$i.'" selected="selected">Item number'.$i.' is selected</option>';
} else {
echo '<option value="'.$i.'">Item number'.$i.'</option>';
}
}
?>

While filling in a form only update the form do not reload the whole page

I have got the following form:
<form action="" method="get" target="_self">
<select name="Category" class="dropmenu" id="Category"onchange="this.form.submit()">
<option value="">Any</option>
<option value="Keyboard"<?php if ($_GET['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
<option value="Piano"<?php if ($_GET['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
</select>
<select name='Manufacturer' class="dropmenu" onChange="this.form.submit()" >
<?php
echo '<option value="">Any</option>';
while ($row = mysql_fetch_array($RS_Search1)) {
$selected = $_GET['Manufacturer'] == $row['Manufacturer'] ? 'selected' : '';
echo '<option '.$selected.'>' . $row['Manufacturer'] . '</option>';
} ?> </select>
<select name="Color" class="dropmenu" onChange="this.form.submit()">
<?php
echo '<option value="">Any</option>';
while ($Color = mysql_fetch_array($RS_Search2)) {
$selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';
echo '<option '.$selected2.'>' . $Color['Color'] . '</option>';
} ?> </form>
Second form to submit
<form action="search2.php" method="get"> </select><input name="Category" type="hidden" value="<?php echo $_GET['Category']; ?>">
<input name="Manufacturer" type="hidden" value="<?php echo $_GET['Manufacturer']; ?>">
<input name="Color" type="hidden" value="<?php echo $_GET['Color']; ?>">
<select name="price" class="dropmenu">
<?php
echo '<option value="">Any</option>';
while ($Price = mysql_fetch_array($RS_Price)) {
$selected3 = $_GET['price_range'] == $Price['price_range'] ? 'selected' : '';
echo '<option '.$selected3.'>' . $Price['price_range']. '</option>';
} ?>
</select><input name="Search2" type="submit" id="Search2" value="Submit">
</form>
As you can see the option values are pulled from a db, after the first value is chosen. What the form does at the minute is reload the whole page. Is there a way to just reload the form in stead of reloading the whole page. I need to keep the values that are submitted on change of a option value to be able to fill in the next drop down menu.
Any help welcome.

Get Array Value

I have one form that have a select box inside while-loop. I try to get the data or value from that select box. Below is my form code.
<form role="form" method="post" action="test2.php" >
<?php
$endDate = '2014-01-28';
$startDate = '2014-01-27';
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startDate ) ) / 86400 );
$x=0;
while($x<=$daydiff)
{
?>
<select class="form-control" name="day_<?php echo $x; ?>">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
</select>
<select class="form-control" name="time_<?php echo $x; ?>">
<option value="10am">10am</option>
<option value="11am">11am</option>
<option value="12pm">12pm</option>
<option value="1pm">1pm</option>
</select>
<?php
$x++;
}
?>
<input type="hidden" name="count" value="<?php echo $x ?>"/>
<button type="submit" name="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
</form>
Below is my "test2.php".
<?php
for ($i = 0; $i < $_POST['count']; $i++){
$day_var = 'day_' . $i;
$days_list[] = $_POST[$day_var];
$time_var = 'time_' . $i;
$time_list[] = $_POST[$time_var];
echo ($_POST[$day_var]);
echo ($_POST[$time_var]);
}
?>
When I submit it will echo "monday10tuesday11am" how can I extract the value separately i.e. monday10 and tuesday11am?
You can replace your numbering scheme and variable variables with a simple array in HTML.
<select class="form-control" name="day[<?php echo $x; ?>]">
<select class="form-control" name="time[<?php echo $x; ?>]">
$days_list[] = $_POST[$day];
$time_list[] = $_POST[$time];
This would greatly simplify your form2.php. Otherwise, please try to explain what results you are expecting with the code you have.
just add some space to your echo.
echo ($_POST[$day_var]).' '.($_POST[$time_var]);

add to an integer variable after comparing two arrays

I am fairly new to php. I am trying to update the score everytime I loop over this array. my code only works if the first value of the listbox was selected by the user and it gives a zero if its not selected. please help.
This is A.php
{
$SkillsArray = array();
$Score=0;
$SkillsArray = $_POST['DutiesDesc'];
//foreach($SkillsArray as $key =>$value )
{
$Sentence = $SkillsArray[0]." ".$SkillsArray[1]." ".$SkillsArray[2]." ".$SkillsArray[3]." ".$SkillsArray[4]." ".$SkillsArray[5]." ".$SkillsArray[6]." ".$SkillsArray[7 ]." ".$SkillsArray[8]." ".$SkillsArray[9]." ".$SkillsArray[10];
}
//Get the applicants score
for($i=0;$i<11;$i++)
{
if ($SkillsArray[$i] == $Text[$i])
{
$Score = $Score+$Val[$i];
}
}
} //**** The following is the HTML part of the code(form)
<form action = "A.php" method ="POST" enctype="multipart/form-data">
<label for="Position">Position:</label><input type="type" name="Position" size="35" /><br />
</p>
<p>
<!-- <label for="DutiesDesc">Duties Description: </label><textarea name="DutiesDesc" cols="30" rows="5" /></textarea>--> <br />
Job Description
<select name="DutiesDesc[]" size=5 multiple="multiple">
<option value="<?php echo $Arow['TextF1']?>"><?php echo $Arow['TextF1']?></option>
<option value="<?php echo $Arow['TextF2']?>"><?php echo $Arow['TextF2']?></option>
<option value="<?php echo $Arow['TextF3']?>"><?php echo $Arow['TextF3']?></option>
<option value="<?php echo $Arow['TextF4']?>"><?php echo $Arow['TextF4']?></option>
<option value="<?php echo $Arow['TextF5']?>"><?php echo $Arow['TextF5']?></option>
<option value="<?php echo $Arow['TextF6']?>"><?php echo $Arow['TextF6']?></option>
<option value="<?php echo $Arow['TextF7']?>"><?php echo $Arow['TextF7']?></option>
<option value="<?php echo $Arow['TextF8']?>"><?php echo $Arow['TextF8']?></option>
<option value="<?php echo $Arow['TextF9']?>"><?php echo $Arow['TextF9']?></option>
<option value="<?php echo $Arow['TextF10']?>"><?php echo $Arow['TextF10']?></option>
<option value="<?php echo $Arow['TextF11']?>"><?php echo $Arow['TextF11']?></option>
</select><br />
The values in the array $SkillsArray do not have the same index as your $Text array. If you select the 3rd (lets say the value is "c") and 5th (e.g. "e") item in your list, the array $SkillsArray will contain the following:
$SkillsArray[0] = "c";
$SkillsArray[1] = "e";
So what you need is a simple search over your $SkillsArray, since you can't rely on the indexes being conform with your $Text array.
if (is_array($SkillsArray)) {
for($i = 0; $i < count($Text); $i++) {
if (array_search($Text[$i], $SkillsArray) !== false) {
$Score += $Val[$i];
}
}
}
Please do remember to check, if $SkillsArray is indeed an array. If the user doesn't check any skills, the function array_search will return false for every item, which will result in the maximum score.
Documentation for functions: count, array_search, is_array
The
for($i=0;$i<11;$i++)
{
for($j=0;$j<11;$j++)
{
if (($SkillsArray[$i] == $Text[$j]))
{
$Score = $Score+$Val[$j];
}
}
This required a second loop in order to work properley:)

Categories