Jump to content


Photo

php select problem


  • Please log in to reply
15 replies to this topic

#1 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 11 July 2006 - 07:26 PM

Can anyone help I need to select data from a database using two drop down boxes I want the first selection which is for a manufacturer to show all the option but only show the products made by them in the second drop down box instead of all products?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 11 July 2006 - 07:45 PM

I voted "help". No not that one, the other one.

if your db structure is similar to this, with integer id fields,
[pre]
manufacturer      product
-------------      ---------------
mfrID    <---+    prodID
mfrName      |    prodDesc
              +--- mfrID
[/pre]
then this will help.

|baaSelect| generate js and php code for dynamic linked dropdowns

Or another approach is
|AJAX/xmlhttp| article and sample application

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 11 July 2006 - 08:05 PM

Thank you very much for your reply

This is the code but I don't know what to add to the sqlPRODNAME to only show the data for that particular manufacturer.

// the sql query to select the data for displaying later

$sqlMANUFACTURERS = "SELECT MANID, MANNAME FROM man order by MANNAME";

$sqlPRODNAME = "SELECT manid, manno, manname,  prodno, proddesc from prodname, man where manno = manid order by proddesc;";

<form action="weld2.php" method="get">

<hr>
<tr><td colspan = 3><h3 align = "center"><i><b>PRODUCT DELETION</b></i></h3></td><tr>
<br>
<br>
<br>
<br
<tr><td colspan = 2><h2 align=center><i>This screen allows users to add products to W.E.L.D.</h2></td></tr>
<tr><td><b>Manufacturer:</b></td>
<?

//initiate the $result variable and populate it with the data from the $sqlMANUFACTURERS table

$result  = mysql_query($sqlMANUFACTURERS, $dblink)
or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");


// mysql_fetch_array fetches the results from the query a row at a time each time it's called
// the result is returned as an array that can be referenced either by field name or by it's index
// this data is then put into a drop down box popualted by the updated manufacturers list

echo '<td><select name="Manufacturer">';
while ($row = mysql_fetch_array ($result))
// loop through the rows outputing them as html table rows

  {
echo '<option value="' . $row["MANID"] . '">' . $row["MANNAME"] . "</option>";
  }

echo '</select></td></tr>';

?>

<tr><td><b>Product Name:</b></td>

<?

//initiate the $result variable and populate it with the data from the $sqlPRODNAME table

$result  = mysql_query($sqlPRODNAME, $dblink)
or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");


// mysql_fetch_array fetches the results from the query a row at a time each time it's called
// the result is returned as an array that can be referenced either by field name or by it's index
// this data is then put into a drop down box popualted by the updated manufacturers list

echo '<td><select name="Product Name">';
while ($row = mysql_fetch_array ($result))
// loop through the rows outputing them as html table rows


  {
echo '<option value="' . $row["prodno"] . '">' . $row["proddesc"] . "</option>";

  }
echo '</select></td></tr>';
?>

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 11 July 2006 - 08:57 PM

If you use the baaSelect class, all you need is

<?php

include 'baaSelect.php';

$sel = new baaSelect();
$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');
$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');
?> 

<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>baaSelect Sample</title>
<meta name="author" content="Barand">

<?php $sel->makeScript() ?>

</head>

<body>
    <form>
  	<table>
   <tr>
      <td>
         Manufacturer
      </td>
      <td>
         <?php $sel->makeSelect('manufacturer'); ?>
      </td>
   </tr>
   <tr>
      <td>
         Product
      </td>
      <td>
         <?php $sel->makeSelect('product'); ?>
      </td>
   </tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
?>
</body>
</html>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 13 July 2006 - 07:51 PM

If you use the baaSelect class, all you need is

<?php

include 'baaSelect.php';

$sel = new baaSelect();
$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');
$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');
?> 

<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>baaSelect Sample</title>
<meta name="author" content="Barand">

<?php $sel->makeScript() ?>

</head>

<body>
    <form>
  	<table>
   <tr>
      <td>
         Manufacturer
      </td>
      <td>
         <?php $sel->makeSelect('manufacturer'); ?>
      </td>
   </tr>
   <tr>
      <td>
         Product
      </td>
      <td>
         <?php $sel->makeSelect('product'); ?>
      </td>
   </tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
?>
</body>
</html>



#6 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 13 July 2006 - 07:55 PM

thanks very much but being a newie I need a little more assistance, I copied and pasted the code you kindly sent but got an error message


Warning: main(baaSelect.php) [function.main]: failed to open stream: No such file or directory in C:\Program Files\xampp\htdocs\delprod.php on line 32

Warning: main() [function.include]: Failed opening 'baaSelect.php' for inclusion (include_path='.;C:\Program Files\xampp\php\pear\') in C:\Program Files\xampp\htdocs\delprod.php on line 32

Fatal error: Class 'baaSelect' not found in C:\Program Files\xampp\htdocs\delprod.php on line 34

The actual code as it stands currently is:

Could you please, please tell me where I am going wrong?


<?php

//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();

require('C:\Program Files\xampp\htdocs\config.php');

require('C:\Program Files\xampp\htdocs\functions.php');

//this is group name or username of the group or person that you wish to allow access to
// - please be advise that the Administrators Groups has access to all pages.
if (allow_access(Administrators) != "yes")
{
include ('C:\Program Files\xampp\htdocs\no_access.html');
exit;
}
?>




<?
$dbuser  = ''; // your database server user name
$dbhost  = 'localhost'; // name of the sql server
$dbpass  = ''; // your database server password
$dbname  = 'weld'; // the name of the database to connect to

include 'baaSelect.php';

$sel = new baaSelect();
$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');
$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');
?>

<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>baaSelect Sample</title>
<meta name="author" content="Barand">

<?php $sel->makeScript() ?>

</head>

<body>
    <form>
  <table>
  <tr>
      <td>
        Manufacturer
      </td>
      <td>
        <?php $sel->makeSelect('manufacturer'); ?>
      </td>
  </tr>
  <tr>
      <td>
        Product
      </td>
      <td>
        <?php $sel->makeSelect('product'); ?>
      </td>
  </tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
?>
</body>
</html>

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 July 2006 - 08:35 PM

Have you downloaded baaSelect.php and put it in your includes folder?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 13 July 2006 - 08:59 PM

sorry for my lack of knowledge but how do I do that please?

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 July 2006 - 09:42 PM

Click on the baaSelect link in my sig below
On the new page, click the "Download class" link and save the zip file on your PC
Unzip and put baaSelect.php in your includes folder
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 10:18 PM

you have the patience of a saint, barand.

#11 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 14 July 2006 - 07:36 PM

barand thank you very much for your help, I am sorry if the questions I asked seemed stupid but sometimes to learn you have to ask someone with greater knowledge than yourself. Once again thanks for your help.

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 July 2006 - 07:39 PM

Ancient proverb: The only stupid question is the one that wasn't asked
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 14 July 2006 - 08:02 PM

Barand another question please what does this mean?


Manufacturer
No Database Selected


Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\baaSelect.php on line 283
Product
No Database Selected


Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\baaSelect.php on line 283
?>

#14 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 July 2006 - 08:10 PM

After this code

$dbuser  = '';      // your database server user name
$dbhost  = 'localhost';      // name of the sql server
$dbpass  = '';      // your database server password
$dbname  = 'weld';      // the name of the database to connect to

you need

$connection = mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname, $connection) or die (mysql_error());

The easiest thing to do is put all the above code in a file, say, "db.php" then at start of every script that uses the database

include 'db.php';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#15 witham

witham
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 14 July 2006 - 08:42 PM

Barand thanks so very much for your help I have learned a great deal from this problem, hopefully one day I will be able to give to this forum instead of merely taking.
Thanks again

#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 July 2006 - 11:20 PM

As a comparison for you, this is the same problem using ajax.

Save this code, which provides a selection of products for a particular manufacturer as "prod_select.php"
<?php

  include 'db.php';
  	
  $manid = $_GET['man'];
  
  $sql = "SELECT prodno, proddesc FROM prodname
  		WHERE manno='$manid'
  		ORDER BY proddesc";
  $res = mysql_query($sql) or die(mysql_error());
  echo "<select name='product'>";
  echo "<option value='0'>- select product -</option>";
  while (list($id, $desc) = mysql_fetch_row($res)) {
    echo "<option value='$id'> $desc</option>";
  }
  echo "</select>";
?>

Now save this main script as "ajax1.php" and run it.

<?php
include 'db.php';

function manOptions($current) {
	$sql = "SELECT manid, manname
			FROM man
			ORDER BY manname";
	$res = mysql_query($sql) or die(mysql_error());
	$str = '';
	while (list($id, $name) = mysql_fetch_row($res)) {
		$selected = $id==$current ? 'selected' : '';
		$str .= "<option value='$id' $selected>$name</option>";
	}
	return $str;
}
?>
<html>
<head>
<title>AJAX sample</title>
<meta name="author" content="Barand">
<SCRIPT>
	var xmlhttp=false;
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }

	 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}

	function handleMessages (url, resultID) {
	  var obj;
	  xmlhttp.open("GET", url, true);
	  xmlhttp.onreadystatechange=function() {
	       if (xmlhttp.readyState==4) {
	            obj = document.getElementById(resultID);
	            obj.innerHTML = xmlhttp.responseText;
	       }
	  }
	 xmlhttp.send(0);
	 return 0;
	}
	
	function manChange(val) {
		var url = "prod_select.php?man=" + val;
		handleMessages (url, "prod");
	}
</SCRIPT>
</head>
<body>
<form>
	<table>
	<tr>
	  <td>
	     Manufacturer
	  </td>
	  <td>
	     <select name='manufacturer' onchange='manChange(this.value)'>
	     <option value='0'>- select mfr -</option>
	     <?php echo manOptions($man) ?>
	     </select>
	  </td>
	</tr>
	<tr>
	  <td>
	     Product
	  </td>
	  <td>
	  	 <DIV id='prod'>
	     <select name='product'>
	     	<option>- select product -</option>
	     </select>
	     <DIV>
	  </td>
	</tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users