使用PHP / SQL - 我怎么可以点击列表中的一个项目,显示assoicated到单击项目的项目另一个列表 [英] Using PHP/SQL - How can I click an item in a list to display another list of items assoicated to the clicked item

查看:161
本文介绍了使用PHP / SQL - 我怎么可以点击列表中的一个项目,显示assoicated到单击项目的项目另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/5914993/how-can-i-refresh-a-detail-select-list-when-my-master-select-changes-using-ajax">How我可以刷新一个细节选择列表时,我的主人使用选择的变化AJAX

我只是在工作一段时间后,又走了学习PHP和环顾四周后,认为我需要用JS / Ajax来做到这一点,我打算更多地了解后,我得到更加舒适用PHP。我想学习如何做到这一点的一些事情我工作了。

我有我显示为一个链接列表的父项的表格。当父项被点击我想点击的父的子项显示在另一份名单。我可以得到2列表中显示简单的查询,我只是不知道如何让页面/ SQL查询,点击时更新。

 &LT; PHP的要求(connection.inc.php'); ?&GT;

&LT; D​​IV ID =名单&GT;
    &LT; H3&GT;列表&LT; / H3&GT;
     &LT; PHP
    $名单=请求mysql_query(SELECT * FROM表)
    或死亡(mysql_error());
    而($信息= mysql_fetch_array($清单))
    {
    回声&所述; A HREF = \#\&gt;中$信息['LISTNAME']。&所述; / a取代;&所述峰; br /&gt;中;
    }
    ?&GT;
&LT; / DIV&GT;

&LT; D​​IV ID ='listItems中的'&GT;
    &LT; H3&GT;的名单,其中,?PHP的$父= 2;回声$父&GT?;项目&lt; / H3&GT;
     &LT; PHP
    $时listItems =请求mysql_query(SELECT * FROM listItems中的WHERE父母= $父)
    或死亡(mysql_error());
    而($信息= mysql_fetch_array($ listItems中))
    {
    回声$信息['了itemname'。&LT; BR /&gt;中;
    }
    ?&GT;
&LT; / DIV&GT;
 

解决方案

code一边(mysql_ * == MUCHO德precato)的问题,您需要检查,如果该请求是AJAX和输出时listItems。

我用jQuery来简化Ajax请求。

 &LT; PHP
需要('connection.inc.php');

/ * AJAX请求* /
如果(空($ _ SERVER ['HTTP_X_REQUESTED_WITH'])及!&安培;
        用strtolower($ _ SERVER ['HTTP_X_REQUESTED_WITH'])=='xmlhtt prequest')
{
    $查询= sprintf的(SELECT * FROM listItems中的WHERE父=%d的,
            mysql_real_escape_string($ _ REQUEST ['父']));
    $时listItems =请求mysql_query($查询)
            或死亡(mysql_error());
    的printf(&LT; H3&GT;清单%D项目&lt; / H3&GT;',$ _REQUEST ['父']);
    而($信息= mysql_fetch_array($ listItems中))
    {
        回声$信息['了itemname'。&LT; BR /&gt;中;
    }
    出口;
}

/ *正常的请求* /
?&GT;

&LT; D​​IV ID =名单&GT;
    &LT; H3&GT;列表&LT; / H3&GT;
    &LT; PHP
    $名单=请求mysql_query(SELECT * FROM表)
            或死亡(mysql_error());
    而($信息= mysql_fetch_array($清单))
    {
        回声&所述; A HREF = \#\&gt;中$信息['LISTNAME']。&所述; / a取代;&所述峰; br /&gt;中;
    }
    ?&GT;
&LT; / DIV&GT;

&LT; D​​IV ID ='listItems中的'&GT;
&LT; / DIV&GT;

&LT;脚本的src =// ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
&LT;脚本&GT;
jQuery的(功能($)){
     $('#名单)。代表('A','点击',函数(){
         $('#listItems中)的负载(window.location.pathname,{父:$(本)的.text()})。
         返回false;
     });
}
&LT; / SCRIPT&GT;
 

Possible Duplicate:
How can I refresh a detail select list when my master select changes using AJAX

I'm just working learning PHP again after some time away and after looking around think I need to use JS/ajax to make this happen which I intend to learn more about after I get more comforable with PHP. I would like to learn how to do this for some thing I am working on now.

I have a table of parent items which I display as a list of links. When a parent item is clicked I want the child items of the clicked parent to be displayed in another list. I can get the 2 lists to display with simple queries I just don't know how to get the page/sql query to update when clicked.

<?php require ('connection.inc.php'); ?>

<div id="lists">
    <h3>Lists</h3>
     <?php      
    $lists = mysql_query("SELECT * FROM lists") 
    or die(mysql_error());
    while($info = mysql_fetch_array( $lists )) 
    {  
    echo "<a href=\"#\">".$info['ListName']."</a><br />";
    } 
    ?>      
</div>  

<div id='listitems'>
    <h3>List <?php $parent=2; echo $parent?> Items</h3> 
     <?php
    $listitems = mysql_query("SELECT * FROM listitems WHERE parent=$parent") 
    or die(mysql_error()); 
    while($info = mysql_fetch_array( $listitems )) 
    { 
    echo $info['itemName']."<br />";
    }   
    ?>
</div>

解决方案

Code issues aside (mysql_* == mucho deprecato), you need to check if the request is AJAX and output listitems.

I used jquery to simplify the ajax request.

<?php
require ('connection.inc.php');

/* AJAX request */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
        strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
    $query = sprintf('SELECT * FROM listitems WHERE parent=%d',
            mysql_real_escape_string($_REQUEST['parent']));
    $listitems = mysql_query($query)
            or die(mysql_error()); 
    printf('<h3>List %d Items</h3>', $_REQUEST['parent']);
    while($info = mysql_fetch_array( $listitems )) 
    { 
        echo $info['itemName']."<br />";
    }
    exit;
}

/* Normal request */
?>

<div id="lists">
    <h3>Lists</h3>
    <?php      
    $lists = mysql_query("SELECT * FROM lists") 
            or die(mysql_error());
    while($info = mysql_fetch_array( $lists )) 
    {  
        echo "<a href=\"#\">".$info['ListName']."</a><br />";
    } 
    ?>      
</div>  

<div id='listitems'>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
jQuery(function($)){
     $('#lists').delegate('a', 'click', function(){
         $('#listitems').load(window.location.pathname, {parent: $(this).text()});
         return false;
     });
}
</script>

这篇关于使用PHP / SQL - 我怎么可以点击列表中的一个项目,显示assoicated到单击项目的项目另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆