1)我有一个基本的Joomla表单,它有三个下拉菜单分别是Country, Town和Shop
2)我需要根据上面提供的下拉列表获取下拉列表。
我已经实现了从数据库中获取数据,并通过在registration.xml中编码创建下拉列表usercomponent
需要达到的目标:我正在尝试根据下拉菜单中选择的上述数据创建动态下拉菜单。
请帮助我,我正在努力工作,我找不到解决方案和代码突破
我在这里创建了一个基本的非joomla示例
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<?php
$username = "USERNAME";
$password = "PASSWORD";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("DATABASE", $dbhandle) or die("Could not select examples");
$result = mysql_query("SELECT * FROM `values`");
$values=array();
$counter=0;
while($row = mysql_fetch_array($result))
{
$values[$counter]=$row{'country'};
$counter++;
}
mysql_close($con);
?>
<?php
$i=0;
$countries=array();
foreach($values as $value) {
$exists=false;
if(count($countries)>0) {
foreach($countries as $something) {
if($something==$value) {
$exists=true;
}
}
}
if($exists==false) {
$countries[$i]=$value;
$i++;
}
}
?>
<select id="first-choice">
<option selected value="base">Please Select</option>
<?php
$k=0;
while($k<count($countries)) {
echo "<option value='".$countries[$k]."'>" . $countries[$k] . "</option>";
$k++;
}
?>
</select>
<br />
<select id="second-choice">
<option>Please choose from above</option>
</select>
<br />
<select id="third-choice">
<option>Please choose from above</option>
</select>
<script type="text/javascript">
$("#first-choice").change(function() {
$("#second-choice").load("getter.php?country=" + $("#first-choice").val());
});
$("#second-choice").change(function() {
$("#third-choice").load("getter.php?town=" + $("#second-choice").val());
});
</script>
</body>
</html>
文件名为getter.php
<?php
$username = "USERNAME";
$password = "PASSWORD";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("DATABASE", $dbhandle) or die("Could not select examples");
if($_GET['country']) {
$countries = mysql_real_escape_string($_GET['country']);
$query = "SELECT * FROM `values` WHERE country='".$countries."'";
$result = mysql_query($query);
$values=array();
$i=0;
while($row = mysql_fetch_array($result))
{
$values[$i]=$row{'town'};
$i++;
}
$i=0;
$towns=array();
foreach($values as $value) {
$exists=false;
if(count($towns)>0) {
foreach($towns as $town) {
if($town==$value) {
$exists=true;
}
}
}
if($exists==false) {
$towns[$i]=$value;
$i++;
}
}
$k=0;
echo "<option selected value='base'>Please Select an Option</option>";
while ($k<count($towns)) {
echo "<option value=".$towns[$k].">" . $towns[$k] . "</option>";
$k++;
}
} elseif ($_GET['town']) {
$town = mysql_real_escape_string($_GET['town']);
$query = "SELECT * FROM `values` WHERE town='".$town."'";
$result = mysql_query($query);
echo "<option selected value='base'>Please Select an Option</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row{'shop'} . "</option>";
}
}
?>
基于这里的教程。把这个拿起来。您需要将getter.php文件放入模型中,并通过JSON调用它(使用合适的控制器)。记住用JText替换所有可见的文本——这样就可以用语言文件制作了。您还需要使用JDocument::addScript
将jquery添加到标题中,或者如果您使用Joomla 3.0,那么JHtml::_('jquery.framework');
就足够了。此外,您必须使用JFactory::getDBO
我在这里放了一个基本的例子。
注意事项:您可能希望调用特定版本的JQuery,因为它不断更新。1.9.0工作得很好,但是我不知道当2.0.0出来的时候,这个链接上会承载什么,以及会包含什么样的浏览器兼容性。
假设数据库结构为
id country town shop
。当然,我怀疑您的数据库方案是否那么简单。因此,请记住根据需要进行调整。
希望这能让你开始学习:)