944,126 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2434
  • PHP RSS
Jul 21st, 2007
0

Listbox is not displaying the correct information if at all

Expand Post »
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; 
$sql = "SELECT `policy` AS 'policy' FROM `Premiums` where ID < 10";
 
  /* 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>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Golffor1 is offline Offline
6 posts
since Jul 2007
Jul 21st, 2007
0

Re: Listbox is not displaying the correct information if at all

>$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.
Last edited by iamthwee; Jul 21st, 2007 at 10:13 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 24th, 2007
1

Re: Listbox is not displaying the correct information if at all

Click to Expand / Collapse  Quote originally posted by 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.

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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 24th, 2007
0

Re: Listbox is not displaying the correct information if at all

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
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Nov 8th, 2007
0

Re: Listbox is not displaying the correct information if at all

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['value_field'];
$sql .= " from " . $options['table'];
$result = mysql_query($sql)or die("error in SQL");
echo '<select name="', $options['value_field'],'" size="1">';
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row[0] == $options['highlight_id']) {
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];
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knish is offline Offline
19 posts
since Oct 2007
Nov 8th, 2007
0

Re: Listbox is not displaying the correct information if at all

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

$sql = "SELECT `policy` AS 'policy' FROM `Premiums`limit 10;
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to hide .php extension
Next Thread in PHP Forum Timeline: Cannot send mail to yahoo





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC