1
phillipd
Is there a way to...
  • 2004/11/10 2:08

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


I've created a matrix of checkboxes with standard Smarty code, 26 of them to be exact. As our GIS people add more and more data and layers to the database I have to add more checkboxes. It's rather time consuming to add more and I'd like to try to put the checkbox names and handles in a DB table and create the array as I iterate through a result set from a DB query.

1) Is it possible to put things like XOOPS checkboxes in an array? If so how is the assignment done?

2) If item one is possible, Can I then pass the array of checkboxes to smarty and iterate through it in a template as I do an array of strings?

Thanks

Doug P

2
phillipd
Re: Is there a way to...
  • 2004/11/10 3:42

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


I got a nicely formatted (rows and columns) set of checkboxes like this but now how do I read them after posting the form?

PHP:

$sql = 'SELECT name FROM '.$xoopsDB->prefix('gis_checkbox').'';
$result = $xoopsDB->query($sql);
list($opt) = $xoopsDB->fetchRow($result);

$testform = new XoopsThemeForm("", "testform", "setlayers.php");
while (list($name) = $xoopsDB->fetchRow($result)) {
$tray = new XoopsFormElementTray("");
$check = new XoopsFormCheckBox("",$name,0);
$check->addOption("0",$name);
$tray->addElement($check);

$testform->addElement($tray);

}
$testform->assign($xoopsTpl);
$xoopsTpl->display('db:'.$xoopsOption['template_main']);

Template:

<{$testform.javascript}>
<{$testform.title}>
>


<{counter assign=elementno start=0 print=false}>
<{foreach item=element from=$testform.elements}>
<{if $elementno % 4 == 0}>

<{/if}>

<{counter}>
<{/foreach}>

"><{$element.body}>


3
Mithrandir
Re: Is there a way to...

I would suggest something like this:
$sql 'SELECT id, name FROM '.$xoopsDB->prefix('gis_checkbox').'';
$result $xoopsDB->query($sql);

$testform = new XoopsThemeForm("""testform""setlayers.php");
$tray = new XoopsFormElementTray("");
$check = new XoopsFormCheckBox("",'checkbox');

while (list(
$id$name) = $xoopsDB->fetchRow($result)) {
    
$check->addOption($id,$name);
    
$check->setValue($id); //set this to be checked
}
$tray->addElement($check);

$testform->addElement($tray);

$testform->assign($xoopsTpl);

Then in the receiving page, you could have:
$checked_boxes $_POST['checkbox']; //array of id => name with only the checked boxes 
foreach ($checked_boxes as $id => $name) {
    
//do processing of checked boxes
}


This is off the top of my head, though, so may not be working fully and may not be exactly as you want it to be.

4
phillipd
Re: Is there a way to...
  • 2004/11/11 6:40

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


The checkboxes appear to be too problematic because I can only get the selected ones so I have decided to try a XoopsFormSelect. This doesn't suffer from the same problem I hope. My recieving script should be able to iterate through the the whole select list I hope and discover the state of each item in the list???

After creating the xoopsformselect How do you programatically select items in the list (without clicking them)?

What does setvalue() do? What "value" am I setting?

What does getvalue() do? What "value am I getting?


Thanks

Doug P

5
phillipd
Re: Is there a way to...
  • 2004/11/11 7:04

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


I'm doing this (Below) and I have the "status" column of the db set to 1's where I want the item selected but it doesn't seem to select any of the formselect items:

$select_defaults = new XoopsFormSelect("","defaults","","29",true);
$tray_defaults = new XoopsFormElementTray(_MM_LAYERS, " ");
$tray_defaults->addElement($select_defaults);

$sql = 'SELECT id,status,name FROM '.$xoopsDB->prefix('gis_layers').' ORDER BY id';
$result = $xoopsDB->query($sql);

while(list($id,$status,$name) = $xoopsDB->fetchRow($result)) {
$select_defaults->addOption($name,$name);
$select_defaults->setValue($status);
echo "$status-";
}


DB table looks like:

mysql> describe xoops_gis_layers;
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(15) unsigned | | PRI | NULL | auto_increment |
| status | varchar(255) | | | 1 | |
| name | varchar(255) | | | | |
+--------+------------------+------+-----+---------+----------------+
+----+--------+------------------------------+
| id | status | name |
+----+--------+------------------------------+
| 1 | 0 | Locale |
| 2 | 0 | Buildings |
| 3 | 0 | Businesses |
| 4 | 0 | Institutions |
| 5 | 0 | Physicians |
| 6 | 0 | Hospitals |
| 7 | 0 | Recreation Area |
| 8 | 0 | Petroleum Facilities |
| 9 | 0 | Petroleum Interconnects |
| 10 | 0 | Transmission Lines |
| 11 | 0 | Hydroelectric Plant |
| 12 | 0 | Power Plants |
| 13 | 0 | Electric Substations |
| 14 | 0 | NRC Facilities |
| 15 | 1 | Highways |
| 16 | 0 | Streets (Local) |
| 17 | 0 | Streets |
| 18 | 1 | Railroads |
| 19 | 1 | Rivers and Streams |
| 20 | 0 | Pipelines |
| 21 | 0 | DOE Site Buildings |
| 22 | 0 | DOE Areas |
| 23 | 0 | NRC Emergency Planning Zones |
| 24 | 0 | Zip Code (area) |
| 25 | 0 | Counties |
| 26 | 0 | DOE. Sites |
| 27 | 0 | Parks |
| 28 | 0 | Landmarks |
| 29 | 0 | Airports |
| 30 | 0 | Major Water |
| 31 | 1 | Lakes and Reservoirs |
| 32 | 0 | Urban Places |
| 33 | 0 | Federal Land (areas) |
| 34 | 1 | States |
| 35 | 1 | Countries |
| 36 | 1 | Oceans |
+----+--------+------------------------------+

Thanks

Doug P

6
Mithrandir
Re: Is there a way to...

Try moving $tray_defaults->addElement($select_defaults); to below the other code

7
phillipd
Re: Is there a way to...
  • 2004/11/11 15:26

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


If you mean:

while(list($id,$status,$name) = $xoopsDB->fetchRow($result)) {
$select_defaults->addOption($name,$name);
$select_defaults->setValue($status);
echo "$status-";
}

$tray_defaults->addElement($select_defaults);

It made no difference.

Am I using setValue correctly? Will it set the value just for the option I just added? If I do a $select_defaults->setValue(0) below the "while" loop all the entries in the select list get highlighted. This would indicate I can't highlight an individual element in the list.

Doug P

8
Mithrandir
Re: Is there a way to...

OK, do this:
while(list($id,$status,$name) = $xoopsDB->fetchRow($result)) {
        
$select_defaults->addOption($id,$name);
        if (
$status) {
            
$select_defaults->setValue($id);
        }
    }
    
$tray_defaults->addElement($select_defaults);

9
phillipd
Re: Is there a way to...
  • 2004/11/11 16:41

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


OK, that works, but now I'm totally confused. Why am I doing a setValue($id)? $id changes with each iteration of the while loop. Why aren't I setting a value of "1" or "0" depending on if I want the list item highlighted or not. I must have a basic misunderstanding of what setValue does.

Now in my recieving script, how do I determine if a element in the list is selected?

Doug P (I really appreciate the help)

10
Mithrandir
Re: Is there a way to...

Quote:
Why am I doing a setValue($id)? $id changes with each iteration of the while loop

setValue($id) inserts the value of $id into an array of pre-selected values on the XoopsFormSelect object, so you are not as much setting the value as you are adding to the value.

Quote:
Now in my recieving script, how do I determine if a element in the list is selected?

With the XoopsFormSelect element named "defaults" and the form method used being POST, you can access the selected values in the receiving script with
$array_of_selected_ids $_POST['defaults'];

Login

Who's Online

123 user(s) are online (24 user(s) are browsing Support Forums)


Members: 0


Guests: 123


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits