Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking a form has values in the DB 1

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
GB
I have a page that displays a table where the rows in the table are generated from a database. The table contains 10 questions as below

Code:
<form method='post' action='' id='form' name='form'>
			
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
<th>Column 6</th>
<th>Column 7</th>
</tr>

<?php
$result = $this->Query("select * from mytable");
$position = 1;

while($row = $this->FetchArray($result[0]))
{
echo "	<input type='hidden' name='q_".$position."' value='".$row['id']."' />
<tr>
<td>".$row['sg_code']."</td>
<td>".$row['sg_question']."</td>
<td><select name='SG".$position."score'><option value=''>- Please select -</option><option value='3'>3</option><option value='2'>2</option><option value='1'>1</option></select></td>
<td><input type='checkbox' name='sg".$position."witnessed' /></td>
<td><select name='SG".$position."'><option value=''>- Please select -</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select></td>
<td><input type='checkbox' name='sg".$position."pdp' disabled /></td>
<td><input type='checkbox' name='sg".$position."redflag' disabled /></td>
</tr>";

$position++;
}
?>

<tr>
<td colspan='7' height='6'>
Comments (optional)<br /><br />
<textarea name='sgcomments' class='textfieldlarge'></textarea>
</td>
</tr>
<tr>
<td colspan='7' align='right'>
<input type='submit' name='submit' value='Save' class="button">		
</td>
</tr>
</table>
</form>

When this is filled in it saves to another table in the database.

What I want is for someone to be able to come back to this form, see the values currently input and change them if need be.

Had the questions in the form been hard coded I would have simply done something like

Code:
<?php if ($row['SG1'] == '1') { echo "selected "; } ?>

for each option value in the dropdown so my form would be as such

Code:
<td><select name='SG1'><option value=''>- Please select -</option>
<option value='1' <?php if ($row['SG1'] == '1') { echo "selected "; } ?>
>1</option>
<option value='2'<?php if ($row['SG1'] == '2') { echo "selected "; } ?>
>2</option>
<option value='3'<?php if ($row['SG1'] == '3') { echo "selected "; } ?>
>3</option>
<option value='4'<?php if ($row['SG1'] == '4') { echo "selected "; } ?>
>4</option>
...
</select></td>

but the questions are generated from a table in the database. How do I mark the questions as selected when I grab them from the database, then check to see whether the form has already been filled in?

Thanks
 
your question is not very clear but perhaps this will give you an idea

Code:
[COLOR=#990000 ]<[/color]form method[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'post'[/color] action[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]''[/color] id[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'form'[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'form'[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab]
[COLOR=#990000 ]<[/color]table[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]tr[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]th[COLOR=#990000 ]>[/color]Column [COLOR=#993399 ]1[/color][COLOR=#990000 ]</[/color]th[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]th[COLOR=#990000 ]>[/color]Column [COLOR=#993399 ]2[/color][COLOR=#990000 ]</[/color]th[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]th[COLOR=#990000 ]>[/color]Column [COLOR=#993399 ]3[/color][COLOR=#990000 ]</[/color]th[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]th[COLOR=#990000 ]>[/color]Column [COLOR=#993399 ]4[/color][COLOR=#990000 ]</[/color]th[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]th[COLOR=#990000 ]>[/color]Column [COLOR=#993399 ]5[/color][COLOR=#990000 ]</[/color]th[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]th[COLOR=#990000 ]>[/color]Column [COLOR=#993399 ]6[/color][COLOR=#990000 ]</[/color]th[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]th[COLOR=#990000 ]>[/color]Column [COLOR=#993399 ]7[/color][COLOR=#990000 ]</[/color]th[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]</[/color]tr[COLOR=#990000 ]>[/color]

[COLOR=#990000 ]<?php[/color]
[COLOR=#009900 ]$result[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]Query[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]"select * from mytable"[/color][COLOR=#990000 ]);[/color]
[COLOR=#009900 ]$position[/color] [COLOR=#990000 ]=[/color] [COLOR=#993399 ]1[/color][COLOR=#990000 ];[/color]

[b][COLOR=#0000FF ]while[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]FetchArray[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$result[/color][COLOR=#990000 ]))[/color] [COLOR=#990000 ]:[/color] [COLOR=#990000 ]?>[/color]
[COLOR=#990000 ]<[/color]tr[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]input type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'hidden'[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'q_<?php echo $position;?>'[/color] value[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'<?php echo $row['[/color]id[COLOR=#FF0000 ]'];?>'[/color] [COLOR=#990000 ]/>[/color]
[tab][tab][COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$row[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'sg_code'[/color][COLOR=#990000 ]];?>[/color]
[tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$row[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'sg_question'[/color][COLOR=#990000 ]];[/color] [COLOR=#990000 ]?>[/color]
[tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]select name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'SG<?php echo $position;?>score'[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]for[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$i[/color][COLOR=#990000 ]=[/color][COLOR=#993399 ]4[/color][COLOR=#990000 ];[/color] [COLOR=#009900 ]$i[/color][COLOR=#990000 ]>[/color][COLOR=#993399 ]0[/color][COLOR=#990000 ];[/color][COLOR=#009900 ]$i[/color][COLOR=#990000 ]--):[/color] [COLOR=#990000 ]?>[/color]
[tab][tab][tab][COLOR=#990000 ]<[/color]option [COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$row[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'SG'[/color][COLOR=#990000 ].[/color] [COLOR=#009900 ]$position[/color] [COLOR=#990000 ].[/color][COLOR=#FF0000 ]'score'[/color][COLOR=#990000 ]][/color] [COLOR=#990000 ]==[/color] [COLOR=#009900 ]$i[/color] [COLOR=#990000 ]?[/color] [COLOR=#FF0000 ]'selected="selected"'[/color][COLOR=#990000 ]:[/color] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];?>[/color]   value[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'<?php echo $i==4 ? '' : $i;?>'[/color][COLOR=#990000 ]><?php[/color] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$i[/color][COLOR=#990000 ]==[/color][COLOR=#993399 ]4[/color] [COLOR=#990000 ]?[/color] [COLOR=#FF0000 ]"- Please select -"[/color] [COLOR=#990000 ]:[/color] [COLOR=#009900 ]$i[/color][COLOR=#990000 ];?></[/color]option[COLOR=#990000 ]>[/color]
[tab][tab][tab][COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]endfor[/color][/b][COLOR=#990000 ];[/color] [COLOR=#990000 ]?>[/color]
[tab][tab][COLOR=#990000 ]</[/color]select[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]input type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'checkbox'[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'sg<?php echo $position; ?>witnessed'[/color] [COLOR=#990000 ]/>[/color]
[tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]select name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'SG<?php echo $position;?>'[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]for[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$i[/color][COLOR=#990000 ]=[/color][COLOR=#993399 ]0[/color][COLOR=#990000 ];[/color] [COLOR=#009900 ]$i[/color][COLOR=#990000 ]<[/color][COLOR=#993399 ]10[/color][COLOR=#990000 ];[/color][COLOR=#009900 ]$i[/color][COLOR=#990000 ]++):[/color] [COLOR=#990000 ]?>[/color]
[tab][tab][tab][COLOR=#990000 ]<[/color]option [COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$row[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'SG'[/color][COLOR=#990000 ].[/color] [COLOR=#009900 ]$position[/color][COLOR=#990000 ]][/color] [COLOR=#990000 ]==[/color] [COLOR=#009900 ]$i[/color] [COLOR=#990000 ]?[/color] [COLOR=#FF0000 ]'selected="selected"'[/color][COLOR=#990000 ]:[/color] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];?>[/color]value[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'<?php echo $i==0 ? "" : $i;?>'[/color][COLOR=#990000 ]><?php[/color] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$i[/color][COLOR=#990000 ]==[/color][COLOR=#993399 ]0[/color] [COLOR=#990000 ]?[/color] [COLOR=#FF0000 ]"- Please select -"[/color] [COLOR=#990000 ]:[/color] [COLOR=#009900 ]$i[/color][COLOR=#990000 ];?></[/color]option[COLOR=#990000 ]>[/color]
[tab][tab][tab][COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]endfor[/color][/b][COLOR=#990000 ];[/color] [COLOR=#990000 ]?>[/color]
[tab][tab][COLOR=#990000 ]</[/color]select[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]input type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'checkbox'[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'sg<?php echo $position;?>pdp'[/color] disabled [COLOR=#990000 ]/>[/color]
[tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]input type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'checkbox'[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]'sg<?php echo $position;?>redflag'[/color] disabled [COLOR=#990000 ]/>[/color]
[tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]</[/color]tr[COLOR=#990000 ]>[/color][COLOR=#FF0000 ]";[/color]
[COLOR=#FF0000 ]<?php[/color]
[COLOR=#FF0000 ][tab]$position++;[/color]
[COLOR=#FF0000 ]endwhile;[/color]
[COLOR=#FF0000 ]?>[/color]

[COLOR=#FF0000 ]<tr>[/color]
[COLOR=#FF0000 ]<td colspan='7' height='6'>[/color]
[COLOR=#FF0000 ]Comments (optional)<br /><br />[/color]
[COLOR=#FF0000 ]<textarea name='sgcomments' class='textfieldlarge'></textarea>[/color]
[COLOR=#FF0000 ]</td>[/color]
[COLOR=#FF0000 ]</tr>[/color]
[COLOR=#FF0000 ]<tr>[/color]
[COLOR=#FF0000 ]<td colspan='7' align='right'>[/color]
[COLOR=#FF0000 ]<input type='submit' name='submit' value='Save' class="[/color]button[COLOR=#FF0000 ]">[tab][tab][/color]
[COLOR=#FF0000 ]</td>[/color]
[COLOR=#FF0000 ]</tr>[/color]
[COLOR=#FF0000 ]</table>[/color]
[COLOR=#FF0000 ]</form>[/color]
 
Many thanks for this, could you please explain (or let me know what to google) how this bit works

Code:
<?php for ($i=4; $i>0;$i--): ?>
<option <?php echo $row['SG'. $position .'score'] == $i ? 'selected="selected"': '';?> value='<?php echo $i==4 ? '' : $i;?>'><?php echo $i==4 ? "- Please select -" : $i;?></option>
<?php endfor; ?></select>

It is working in the fact that it loops through the questions and outputs the select options, but I need to get it to check that the value in the database and select the value it finds.

Thanks again
 
Sorry scrap that it is working great, I was calling $row{} twice so it wasn`t picking the results form the DB.

If OI could just ask one more question, I have been using

if { } else { }

in my php, I noticed you use : and ?. What is the diference and which is best to use?

Thanks
 
the ? : is the logic for what is known as the 'ternary operator'. in pcode it looks like this

value equals {expression} ? {true} : {false}

this is useful for things like I showed above
Code:
[b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$value[/color] [COLOR=#990000 ]==[/color] [COLOR=#009900 ]$anothervalue[/color] [COLOR=#990000 ]?[/color] [i][COLOR=#9A1900 ]/* if true */[/color][/i] [COLOR=#FF0000 ]' selected="selected" '[/color] [COLOR=#990000 ]:[/color] [i][COLOR=#9A1900 ]/* if false */[/color][/i] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];[/color]

there is no semantic difference between the above and
Code:
[b][COLOR=#0000FF ]if[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$value[/color] [COLOR=#990000 ]==[/color] [COLOR=#009900 ]$anothervalue[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ]:[/color]
 [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#FF0000 ]' selected="selected" '[/color][COLOR=#990000 ];[/color]
[b][COLOR=#0000FF ]else[/color][/b][COLOR=#990000 ]:[/color]
 [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];[/color]
[b][COLOR=#0000FF ]endif[/color][/b][COLOR=#990000 ];[/color]
(of its alternatives using curly braces or, in this instances, using no braces or even using no else statement.
Code:
[b][COLOR=#0000FF ]if[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$value[/color] [COLOR=#990000 ]==[/color] [COLOR=#009900 ]$anothervalue[/color][COLOR=#990000 ])[/color] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#FF0000 ]' selected="selected"'[/color][COLOR=#990000 ];[/color]
[b][COLOR=#0000FF ]else[/color][/b] [b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];[/color]

there may well be a parsing advantage in using the ternary operator, but I have never tested it.

in recent versions of php you have been able to be very lazy and do this
Code:
[b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$string[/color] [COLOR=#990000 ]?:[/color] [COLOR=#009900 ]$otherstring[/color][COLOR=#990000 ];[/color]
which would return the value of $string if it were equivalent to boolean true or $otherstring otherwise. I don't use this, however, as my IDE flags this incorrectly as an error.

so it comes down mostly to a question of style. I use if ... else ... endif when, essentially, the code that goes between them is more than a simple assignment. perhaps more than a line.

and from another style perspective, and because of the advantage of MVC models, I am increasingly using php break ins from html rather than large strings within html. my IDE nicely deals with colourisation this way, so making it easier to spot semantic errors. it definitely slows down parsing and execution but only marginally and that is a small price to pay for readable/manageable templating code.

more on there ternary operator can be found here
 
Thank you very much jpadie, if I could give you multiple stars I would do.

I have come from using asp for 10 years and am learning the ropes with php now that asp is pretty much dead.
 
Would you be able to let me know what this bit is doing?

<?php for ($i=4; $i>0;$i--): ?>

Thanks again, bookmarked this post for future reference :)
 
sure

a normal for loop iterates over the internals until a condition is met.

the pseudo code is
for ( initial value; ending condition; loop conditions)

what we have done is set the initial value of $i to 4, told the loop to keep running whilst $i is greater than zero and lastly told $i to decrement (reduce by one) each iteration.

you can do other funky things with for outside of the normal (for($i=0; $i< 10; $i++) ). Check out this page for more info.
 
Thank you, that makes perfect sense. I should have known that really :-\, it was the $i-- that threw me as in asp I used to always count upwards as in your second example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top