[ASK] Display data from selected dropdown list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cubluwt
Forum Newbie
Posts: 2
Joined: Tue Jan 21, 2014 4:21 am

[ASK] Display data from selected dropdown list

Post by cubluwt »

Hi, I'm new here. But many posts here that helped me out before. Thanks alot to this forum, but now I need some help from you guys.

First of all this is my database, you can copy and paste it to notepad and save as .sql file to import to mysql.

Code: Select all

-- phpMyAdmin SQL Dump

-- version 3.2.4

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Jan 21, 2014 at 01:14 AM

-- Server version: 5.1.41

-- PHP Version: 5.3.1



SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";





/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8 */;



--

-- Database: `db_cobadulu`

--



-- --------------------------------------------------------



--

-- Table structure for table `tbl_barang`

--



CREATE TABLE IF NOT EXISTS `tbl_barang` (

  `id_barang` int(11) NOT NULL AUTO_INCREMENT,

  `id_kategori` int(11) NOT NULL,

  `id_kalsifikasi` int(11) NOT NULL,

  `nama_barang` varchar(128) NOT NULL,

  PRIMARY KEY (`id_barang`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;



--

-- Dumping data for table `tbl_barang`

--



INSERT INTO `tbl_barang` (`id_barang`, `id_kategori`, `id_kalsifikasi`, `nama_barang`) VALUES

(1, 2, 3, 'Samsung S4'),

(2, 3, 2, 'Vas Bunga'),

(3, 4, 1, 'Mug Porselen');



-- --------------------------------------------------------



--

-- Table structure for table `tbl_kategori`

--



CREATE TABLE IF NOT EXISTS `tbl_kategori` (

  `id_kategori` int(11) NOT NULL AUTO_INCREMENT,

  `nama_kategori` varchar(128) NOT NULL,

  PRIMARY KEY (`id_kategori`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;



--

-- Dumping data for table `tbl_kategori`

--



INSERT INTO `tbl_kategori` (`id_kategori`, `nama_kategori`) VALUES

(1, 'Elektronik'),

(2, 'Pecah Belah'),

(3, 'Gift'),

(4, 'Kerajinan');



-- --------------------------------------------------------



--

-- Table structure for table `tbl_klasifikasi`

--



CREATE TABLE IF NOT EXISTS `tbl_klasifikasi` (

  `id_klasifikasi` int(11) NOT NULL AUTO_INCREMENT,

  `nama_klasifikasi` varchar(128) NOT NULL,

  PRIMARY KEY (`id_klasifikasi`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;



--

-- Dumping data for table `tbl_klasifikasi`

--



INSERT INTO `tbl_klasifikasi` (`id_klasifikasi`, `nama_klasifikasi`) VALUES

(1, 'Ringan'),

(2, 'Berat'),

(3, 'Cukup Berat'),

(4, 'Sangat Berat');



/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
And then this is the script I have:

Code: Select all

<script languange="Javascript1.2">
function pilih(id){
	location.replace("index.php?id="+id);
}
</script>

<?php
$host="localhost";
$user="root";
$pass="";
$db="db_cobadulu";

//koneksi 
$koneksi=mysql_connect($host,$user,$pass);
mysql_select_db($db,$koneksi);

if($_GET['']!==""){
	$id=$_GET['id'];
	
	
	$query=mysql_query("SELECT b.*, nama_barang, p.nama_kategori, kl.nama_klasifikasi FROM tbl_barang AS b
						LEFT JOIN tbl_kategori AS p ON b.id_kategori=p.id_kategori
						LEFT JOIN tbl_klasifikasi AS kl ON b.id_klasifikasi=kl.id_klasifikasi
						WHERE id_barang='$id'");
	
	?>
	<table border="1">
	<tr><th>No</th><th>Nama Barang</th><th>Kategori</th><th>Klasifikasi</th>
	<?php
	//while($row=mysql_fetch_array($query) or die (mysql_error())){
	while($row=mysql_fetch_array($query)){
		?>
		<tr><td><?php echo $c=$c+1;?></td><td><?php echo $row['nama_barang'];?></td><td><?php echo $row['nama_kategori']; ?></td><td><?php echo $row['nama_klasifikasi']; ?></td>
		</tr>
		<?php
	}
	?></table><?php
}

?>
<html>
<head><title>Gudang Barang</title></head>
<body>
<p>&nbsp;</p>
<table border="0">
<tr>
</tr>
<tr>
	<td width="195" valign="top">
	<select name="id" id="id" onChange="pilih(this.value)">
		<option value="0" selected="selected">Pilih Kategori</option>
		<?php 
		$query_limit=mysql_query("select * from tbl_kategori");
		
		while($row=mysql_fetch_array($query_limit))
		{
			?><option value="<?php  echo $row['id_kategori']; ?>"><?php  echo $row['nama_kategori']; ?></option><?php 
		}
		?>
	</select>	
	</td>
	<td width="195" valign="top">
	<select name="id" id="id" onChange="pilih(this.value)">
		<option value="0" selected="selected">Pilih Klasifikasi</option>
		<?php 
		$query_limit=mysql_query("select * from tbl_klasifikasi");
		
		while($row=mysql_fetch_array($query_limit))
		{
			?><option value="<?php  echo $row['id_klasifikasi']; ?>"><?php  echo $row['nama_klasifikasi']; ?></option><?php 
		}
		?>
	</select>	
	</td>
</tr>
</table>
</body>
</html>
The problem is, the output is misserable. Yes it did showing the data from two tables. But it's a mess. What I'm saying is, I intend to show/display the data from table "tbl_barang" that connected with two tables "tbl_kategori" and "tbl_klasifikasi" by choosing data from the dropdown list. But not all the data came out, and also when I was putting the "submit" button it doesn't really work at all.

I was looking the old posts here, I've tried googling it but no results. Most of the results is "How to displaying data from database to dropdown list", but what I needed is "How to display data from selected item/data from the dropdown list". I need ur help guys...I cannot sleep, it's always in my mind...

Thank you so much for the helps.

Best regards,

Kris
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: [ASK] Display data from selected dropdown list

Post by Celauran »

Looking at it quickly, your JS redirect always uses ?id= which feeds a query WHERE id_barang = . The select lists, however, are feeding the JS id_kategori or id_klasifikasi. Shouldn't these dropdowns perform different functions? Changing the value of the category dropdown should show me items connected to that category, no? Instead, it's taking the category ID and trying to find an item with the same item ID.
cubluwt
Forum Newbie
Posts: 2
Joined: Tue Jan 21, 2014 4:21 am

Re: [ASK] Display data from selected dropdown list

Post by cubluwt »

Celauran wrote:Looking at it quickly, your JS redirect always uses ?id= which feeds a query WHERE id_barang = . The select lists, however, are feeding the JS id_kategori or id_klasifikasi. Shouldn't these dropdowns perform different functions? Changing the value of the category dropdown should show me items connected to that category, no? Instead, it's taking the category ID and trying to find an item with the same item ID.
Thank you for the replies, I get it...I get it....thank you so much, you're right... :D
Post Reply