Hello everyone I have some code listed below, well with this code I it does not display anything in the listbox. When I have it displaying the stuff in the listbox it does not display the correct stuff...like less than 10. I have put the line of code in bold. Can someone look at it and tell what I am missing. My goal is to have two list boxes, but they are not going to be dependent on each other.

<!-- //to display a listbox from a database when something is clicked it is the item is then displayed underneath the listbox
 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
	//-----------------------------------
	//this is the connection string to connect to the database
	//do not delete or change anything on the next two lines
	//-----------------------------------
$db = mysql_connect("localhost", "brian", "Golffor1") or die(mysql_error());
mysql_select_db("brian_aTitle") or die (mysql_error);
$id='10';
	//	$query3="Select * from 'Premiums' where id < $id";
	$query3 = "SELECT `Premium` AS 'premium' FROM `Premiums` where ID < 10";
	$result3 = mysql_query($query3);
	$the_data = array();
	$the_string = "";

	while($data = mysql_fetch_row($result3))
		{
			$the_data[] = $data[0];
		}
	for ($x = 0; $x < count($the_data); $x++)
		{
			$the_string = $the_string . '"' . $the_data[$x] . '"';
			if ($x != (count($the_data) - 1)){ $the_string = $the_string . ", "; } 
		}
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
<!--//hide from old browsers
var Descriptions = new Array(<?PHP echo $the_string; ?>);
function ShowDesc(id)
{
document.listbox.description.value = Descriptions[id];
}

//end hide from old browsers-->
</script>
</head>

<body>
<form name="listbox">
  <? 
	  //start of the array
enhanced_list_box(array(
  'table'        => 'Premiums',
  'id_number'    => 'id',
  'value_field'  => 'Policy',
  'highlight_id' => 1));

//this display's the listbox so the user can select a service
 function enhanced_list_box($options){

//  $sql  = "select " . $options['id_number'];
 // $sql .= ", " . $options['value_field'];
 // $sql .= " from " . $options['table'];
 // $sql .= "where " . $options['id_number'] < $ID; 
[B]$sql = "SELECT `policy` AS 'policy' FROM `Premiums` where ID < 10";[/B]
 
  /* append any where criteria to the sql */
  if(isset($options['where_statement'])) {
    $sql .= " where " . $options['where_statement'] ;
  }//CLOSE OF IF STATEMENT

  $result = mysql_query($sql)
            or die(mysql_error());

echo '<select name="' . $options['id_number'] . '" size="1" onChange="ShowDesc(this.selectedIndex)">';

while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
  if($row[0] == $options['highlight_id']) {
    echo '<option value="', $row[0], '" SELECTED>',
         $row[1], '</option>';
  }//CLOSE OF IF STATEMENT 
  else {
    echo '<option value="', $row[0], '">',
         $row[1], '</option>';
  }//CLOSE OF ELSE STATEMENT
}//CLOSE OF WHILE STATEMENT

echo '</select>';
}
	  
	  
?>
<br>
<textarea name="description" readonly="TRUE" cols="40">Choose an item to view it's description.</textarea>
</form>
</body>
</html>
Member Avatar for iamthwee

>$sql = "SELECT `policy` AS 'policy' FROM `Premiums` where ID < 10";

This statement just doesn't make any sense in terms of SQL. And you got some strange (non standard ) apostrophe's there as well.

>$sql = "SELECT `policy` AS 'policy' FROM `Premiums` where ID < 10";

This statement just doesn't make any sense in terms of SQL. And you got some strange (non standard ) apostrophe's there as well.

Iamthwee, what are you talking about ? the back ticks `` denote a field name or table name within mySQL proper syntax.

PHP fills these in for you if you don't use them.

commented: yes +13

Golffor1, without going into your code too much, ihave noticed your using row[0] and row[1].

However in your SQL query you're only selecting 1 field, therefore you will only have row[0]

Cheers

Hi,

I am a newbie to PHP. what is the way to hold the selected value from the list. How does the return statement work in this context.

Brgds,

kNish

function enhanced_list_box_05($options){
$sql = "select " . $options;
$sql .= " from " . $options;
$result = mysql_query($sql)or die("error in SQL");
echo '<select name="', $options,'" size="1">';
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row[0] == $options) {
echo '<option value="' . $row[0] . '"SELECTED>' . $row[0] . '</option>';
} else {
echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
echo $row[0];
}
}
echo '</select>';
return $row[0];
}
Member Avatar for fatihpiristine

if you use functions in sql it ll make sense...
in query has no sense...

$sql = "SELECT `policy` AS 'policy' FROM `Premiums`limit 10;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.