Forum Moderators: coopster

Message Too Old, No Replies

Checkbox to retrieve field names

         

sfast

3:51 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



For PHP reports -

I need to take data from checkboxs that actually carry the names of table fields in the database.
After selecting from checkboxes, I only want to display the fields that were selected.

On form submit, I get the values of each field like this-

while (list ($key,$val) = @each ($_GET['fields'] )) {
echo $val.' ,';
}

But I have problems concatenating strings to get to get all the fields together.

Like fields = fields + $val

and then
echo fields

So that I can have select fields from table;

Is there any better method to follow the loop?

Thanks

joelgreen

4:45 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



<input type=checkbox value="tablefield1" name="flds[1]">
<input type=checkbox value="tablefield2" name="flds[2]">
<input type=checkbox value="tablefield3" name="flds[3]">

PHP

$cols = join(",", $_GET['flds']);

sfast

7:13 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



Thanks. I did not know about join function.

Now I have another problem.

Suppose $cols = address, city, zipcode.

what is the efficient way of displaying fields that are in $cols.

One way I can think of is - trying with if loops. For 3 fields, there will be not less than 6 ifs (3!)...

echo '<td height="19" colspan="2"><div align="center"> ADDRESS
</div></td> ' ;

echo '<td width="14%" height="19"><div align="center"> CITY </div></td> ' ;

echo '<td width="10%" height="19"><div align="center">ZIPCODE </div></td> ' ;

coopster

11:51 am on Jun 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Use a loop to read through the information and build the html:
foreach ($cols as $col) { 
echo
'<td width="14%" height="19">' .
'<div align="center">' .
htmlentities($col) .
'</div></td>'
;
}