PDA

View Full Version : Giúp đỡ về DropDown



mouse1232002
08-05-2009, 15:30
mình đang làm trang wed bán điện thoại. lam tới phần tim kiếm bị bí mong các a e giup đỡ.
có 4 dropdownlis1,2.3.4.drop1.value=nhà sản xuất.drop2.value=tên diện thoại.drop3.value=giá(từ 1trieu đến).drop4.value=giá đến(nó giống phan tim kiếm trên trang thế giới di dộng do).Mong a e giúp đỡ huong giả quyết va cách lam. thanks a lot!!!!!!!!!!!!

Đề nghị đọc nội quy để tránh bị xóa bài.

kogankid
08-05-2009, 16:34
là bạn muốn tìm cách làm cái dropdownlist hay là tìm kiếm vậy bạn.
dropdownlist thì xài ajax ấy, giá thì cố định rùi, có cái nhà sản xuất với tên điện thoại thui, chọn nhà sản xuất thì xài ajax cho nó load sản phẩm xuống là được mà,
nếu bạn xài php thì tớ có code demo cái này, hay không thì cậu phát triển ra thui, tớ không dám sure, hehe

file index.php


<table border=0 cellpadding=0 cellspacing="0" align="center" width="40%">
<form>
<tr><td colspan="2" height="25"></td></tr>
<tr><td colspan="2" class="textheading">AJAX Dropdown to Dropdown</td></tr>
<tr><td colspan="2" height="25"></td></tr>
<tr><td class="text" width="50%">Select category : </td><td>
<?php
require_once("get.php");
$obj = new AjaxDropdown();
$arr = $obj->getArray(0);
$strRet = '<option value="0">--Select--</option>';
foreach ( $arr as $row )
{
$strRet .= '<option value="'.$row->id.'">'.$row->Name.'</option>';
}
?>
<select name="selCat" class="text" onChange="javascript:Ajax.Request('get.php?method=getXML&param=',this.value, Ajax.Response);">
<?php echo $strRet; ?>
</select>
</td></tr>
<tr><td colspan="2" height="5"></td></tr>
<tr><td class="text">Sub categories :</td><td>
<select name="selSubCat" id="selSubCat" class="text">
<option value="0">--Select--</option>
<option value="12">test</option>
</select>
</td></tr>
</form>
</table>


get.php


<?php

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'testdb');

class AjaxDropdown
{
var $table;

function AjaxDropdown()
{
// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
$this->table = "tblcategories";
}

function dbConnect()
{
DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}

function getXML($id)
{
$this->dbConnect();
$query = "SELECT * FROM $this->table where pid = {$id} ORDER BY id asc";
$result = mysql_db_query (DB_NAME, $query, LINK);

$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
$xml .= '<categories>';
while($row = mysql_fetch_array($result))
{
$xml .= '<category>';
$xml .= '<id>'. $row['id'] .'</id>';
$xml .= '<fname>'. $row['Name'] .'</fname>';
$xml .= '</category>';
}
$xml .= '</categories>';
mysql_close();
return $xml;
}

function getArray($id)
{
$this->dbConnect();
$query = "SELECT * FROM $this->table where pid = {$id} ORDER BY id asc";
$result = mysql_db_query (DB_NAME, $query, LINK);
$arr = array();
while($row = mysql_fetch_object($result))
{
$arr[] = $row;
}
mysql_close();
return $arr;
}
}

if ( @$_GET['method'] == 'getXML' )
{
header("Content-Type: application/xml; charset=UTF-8");
$obj = new AjaxDropdown();
echo $obj->getXML(@$_GET['param']);
}


file ajax.js


var Ajax = new Object();

Ajax.Request = function(url,id, callbackMethod)
{
if ( id == 0 )
{
return;
}
Page.getPageCenterX();

Ajax.request = Ajax.createRequestObject();
Ajax.request.onreadystatechange = callbackMethod;
Ajax.request.open("POST", url+id, true);
Ajax.request.send(url);
}

Ajax.Response = function ()
{
if(Ajax.CheckReadyState(Ajax.request))
{
document.getElementById('selSubCat').length = 0;
document.getElementById('selSubCat').options[0] = new Option("--Select--","0");

var response = Ajax.request.respon***ML.documentElement;
var _data = response.getElementsByTagName('category');
if(_data.length == 0)
{
document.getElementById('selSubCat').options[0] = new Option("No sub category available",'0');
}
var i
for ( i = 0 ; i < _data.length ; i ++ )
{
document.getElementById('selSubCat').options[i] = new Option(response.getElementsByTagName('fname')[i].firstChild.data,response.getElementsByTagName('id ')[i].firstChild.data);
}
}
}

Ajax.createRequestObject = function()
{
var obj;
if(window.XMLHttpRequest)
{
obj = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
obj = new ActiveXObject("MSXML2.XMLHTTP");
}
return obj;
}

Ajax.CheckReadyState = function(obj)
{
if(obj.readyState < 4) {
document.getElementById('loading').style.top = (Page.top + Page.height/2)-100;
document.getElementById('loading').style.left = Page.width/2-75;
document.getElementById('loading').style.position = "absolute";
//document.getElementById('loading').innerHTML = "<table border=0 cellpadding=0 cellspacing=1 width=160 bgcolor=gray><tr><td align=center class=loading height=45 bgcolor=#ffffff><img src='snake_transparent.gif'/>&nbsp&nbspLoading..</td></tr></table>";
}
//if(obj.readyState == 1) { document.getElementById('loading').innerHTML = "Loading..."; }
//if(obj.readyState == 2) { document.getElementById('loading').innerHTML = "Loading..."; }
//if(obj.readyState == 3) { document.getElementById('loading').innerHTML = "Loading..."; }
if(obj.readyState == 4)
{
if(obj.status == 200)
{
//document.getElementById('loading').innerHTML = "<table border=0 cellpadding=0 cellspacing=1 width=160 bgcolor=gray><tr><td align=center class=loaded height=45 bgcolor=#ffffff>Data is loaded successfully..</td></tr></table>";
setTimeout('Page.loadOut()',2000);
return true;
}
else
{
document.getElementById('loading').innerHTML = "HTTP " + obj.status;
}
}
}

var Page = new Object();
Page.width;
Page.height;
Page.top;

Page.loadOut = function ()
{
document.getElementById('loading').innerHTML ='';
}
Page.getPageCenterX = function ()
{
var fWidth;
var fHeight;
//For old IE browsers
if(document.all)
{
fWidth = document.body.clientWidth;
fHeight = document.body.clientHeight;
}
//For DOM1 browsers
else if(document.getElementById &&!document.all)
{
fWidth = innerWidth;
fHeight = innerHeight;
}
else if(document.getElementById)
{
fWidth = innerWidth;
fHeight = innerHeight;
}
//For Opera
else if (is.op)
{
fWidth = innerWidth;
fHeight = innerHeight;
}
//For old Netscape
else if (document.layers)
{
fWidth = window.innerWidth;
fHeight = window.innerHeight;
}
Page.width = fWidth;
Page.height = fHeight;
Page.top = window.document.body.scrollTop;
}


database


create database testdb;

use testdb;

CREATE TABLE `tblcategories` (
`id` int(11) NOT NULL,
`Name` varchar(100) collate latin1_general_ci NOT NULL,
`pid` int(11) NOT NULL
);


có gì add nick y!m kogankid 8 chơi

mouse1232002
08-05-2009, 21:41
Thank a lot! nhưng mình k rành ajax này lam.Bạn có hướng nào khác k bạn. mình đang rât cần.

kogankid
09-05-2009, 00:09
ack, mình hướng dẫn rõ ràng về ajax đó rùi, bạn code nguyên dàn code xong sửa lại trong trường hợp của bạn thui mà
nếu ko xài ajax ko lẻ mỗi lần user chọn nhà sản xuất là phải refresh trang để hiện ra sản phẩm, vậy kỳ lắm,
có gì thì post thẳng lên, db thế nào, xài framework gì hay viết thuần, mình sửa cho rùi copy về xài,
ngại thì add nick yahoo mình rùi mình giúp cho, sẵn lòng, sharing is happiness, keke.
cái này phải xài ajax thui bạn à, ai cũng làm thế chỉ là chỉnh sửa sao cho tốt, xài ajax gì để bảo mật và chạy nhanh thui à,
có gì cứ liên hệ mình nhá, cần thì add nick yahoo rùi có gì ún café mình chỉ cho rõ cũng đc, ko sao cả, vui vẻ thui, cuối tuần rùi mà, keke

mouse1232002
09-05-2009, 01:06
Minh viết bằng asp, database tao bang sql. Bạn thử vô trang thế giới di động. minh lam fan tìm kiếm giống như vậy đó. minh định dùng store viết rôi dùng điều kiện lấy lên mà k được.thanks a lot!!!!!

kogankid
09-05-2009, 06:02
rúi xương rùi
bạn viết bằng asp hay asp.net
cái bạn cần là cái gì, bạn nói rõ ràng đi,
mình nói rõ rùi mà bạn,
1. phần show dropdownlist giá từ với giá đến là thông tin tĩnh, cái này không nói tới
2. phần chọn nhà sản xuất, show ra sản phẩm thuộc nhà sản xuất thì dùng ajax, bạn viết theo như cái code mình đưa là được, sửa lại phần truy xuất database và include file theo như asp hay asp.net (file index.php và get.php), còn file ajax.js thì để y vậy xài thoy.

bạn nói rõ ra tý, bạn xài store để truy xuất là store ra sao, cụ thể để anh em còn giúp bạn nhé.

chúc bạn cuối tuần vui vẻ