Hi all,
I have two database tables (I don't think it is a database problem,
by the way) data_series and data_sets. data_sets is a child of
data_series. I want my PHP form to display a scrolling list of all the
data series in the database. The user will then select a subset of
these, press a submit button, and be shown a scrolling list of all the
child data sets of the selected data series.
Problem is, when I code this following an example in PHP programming,
the data series are displayed properly in a <select> named
'data_series'. The scrolling list called 'data_sets' is drawn properly.
But when the program tries to retrieve the selected data_series via
$_GET[;data_series'] it gets the label 'Data Sets' of the second
scrollling list in the series. I have looked at this for an hour now,
trying various tweaks of the code, and cannot figure out what is wrong.
Here is my php code for the page:
<?php
error_reporting(E_ALL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Set</title>
</head>
<?php
require_once("./config_file.php");
require_once("./lib_mem.php");
require_once("./lib_mem2.php");
require_once("./lib_delete.php");
?>
<body>
<div align="center">
<h2>Select Data Set</h2>
</div>
<form id="series_select" name="series_select" method="GET" action="<?php echo $_SERVER['PHP_SELF']?>">
<?php
if (array_key_exists('get_data_sets', $_GET)){
?>
<?php
$data_series_id_list = implode($_GET['data_series'], ',');
echo "data_series_id_list = $data_series_id_list\n";
$select_data_set_cmd = "SELECT ds_data_series_id, ds_name ".
"FROM data_sets ".
"WHERE ds_data_series_id IN (" . $data_series_id_list . ")".
"ORDER BY ds_name";
echo "select_data_set_cmd = $select_data_set_cmd\n";
echo_scrolling_list_from_database ('data_set',
'',
'Data Sets',
15,
0,
$pg,
$select_data_set_cmd,
array());
?>
<p> </p>
<p>
<?php
echo_a("./edit_data_set.php", "Edit data set");
?>
</p>
<p>To edit the data for a data set, go to the main menu and select Data >>Enter New or Edit Existing.</p>
<p>
<?php
echo_a("./display_data_set_example.html","Display data set and data");
?>
</p>
<p align="left">
<?php
echo_a("./index.html", "Home");
?>
</p>
<?php
}
else {
$select_data_series_cmd = "SELECT sr_data_series_id, sr_name FROM data_series ORDER BY sr_name";
echo_scrolling_list_from_database('data_series',
'',
'Data Series',
15,
1,
$pg,
$select_data_series_cmd,
array());
echo_submit('get_data_sets', 'Get Data Sets');
}
?>
</form>
</body>
</html>
The functions I call from the lib_mem2 file are:
// echo_scrolling_list_tag
// echo_scrolling_list_head
// echo_option
// these functions together are used to produce the HTML code to construct
// a scrolling list from items selected from a database table. The select cmd
// is given as one of the parameters to the echo_scrolling_list function. The first
// column selected in the list must be the primary key id of the table. The remaining
// selected values are formatted into a colon separated string. They appear as the rows
// of the scrolling list. The id is stored as the value of the row.
// If multiple selections are enabled the scrolling list returns an array. Else it
// returns a scalar.
//
// echo_scrolling_list_tag
// constructs html tag for scrolling list
// Parameters:
// name name of scrolling list (should be name of primary id of table)
// class CSS class
// label label for scrolling list
// size number of rows to display
// multiple_OK true if multiple selections are allowed
// Action: echo the <select name=...> tag for a scrolling list
//
function echo_scrolling_list_tag($name,
$class,
$label,
$size,
$multiple_OK){
$for_phrase = 'for="' . $name . '"';
echo "<label " . $for_phrase . ">" . $label . "</label>";
$multiple_OK_braces = $multiple_OK ? "[]" : '';
$name_phrase = 'name="' . $name . $multiple_OK_braces . '"';
// call with $name[] to return array if multiple, else return
// single value
$class_phrase = 'class="' . $class . '"';
$size_phrase = 'size=' . $size;
$multiple_phrase = $multiple_OK ? 'multiple' : '';
echo "name_phrase = $name_phrase, multiple_phrase = $multiple_phrase";
return '';
}
// echo_option_tag
// constructs html tag for option of a scrolling list
// Parameters:
// id_colon_display string formatted as id : value. id is primary key of table value
// was taken from
// default_ids array of ids of rows to be highlighted when scrolling list is displayed
// Action: construct the <option> tag for a row in a scrolling list. The id will be the html 'value' of
// the tag. The value given in the id_colon_display parameter will be displayed as the row entry.
// If the id of a row is in the default_ids array, it will be marked 'selected'
function echo_option_tag($id_colon_display,
$default_ids){
$id_display = array();
ereg('^([^:]+) : (.*)$', $id_colon_display, $id_display);
$id = $id_display[1];
$display = $id_display[2];
$selected = in_array($id, $default_ids)? 'selected' : '';
echo "<option value=".$id." ".$selected.'/>'.$display."</option>\n";
return '';
}
//
// echo_scrolling_list_from_database
// Parameters:
// name name of scrolling list (should be name of primary id of table)
// class CSS class
// label label for scrolling list
// size number of rows to display
// multiple_OK true if multiple selections are allowed from the list
// pg open database connection
// select_options_cmd SQL statement used to retrieve values for the rows
// default_ids ids of rows to be highlighted when scrolling list is displayed
// Action: display scrolling list with formatted rows of values retrieved by select_cmd.
function echo_scrolling_list_from_database(
$name,
$class,
$label,
$size,
$multiple_OK,
$pg,
$select_options_cmd,
$default_ids){
echo_scrolling_list_tag($name,
$class,
$label,
$size,
$multiple_OK);
$id_colon_displays = do_select_and_format_many($pg, $select_options_cmd);
foreach ($id_colon_displays as $id_colon_display){
echo_option_tag($id_colon_display, $default_ids);
}
echo "</select>";
}
Thanks!
Mary Anderson
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php