jquery从数据中选择索引并显示sql内容

时间:2012-02-11 11:37:35

标签: jquery mysql database

我不知道如何让它发挥作用。我解释一下。

我有一些带jquery脚本的HTML代码。

HTML

<div id="accordion">
    <h3><a class="0" href="#">aparamenta modular</a></h3>
    <div>
    <div class="cat_content_ex">
        t&eacute;rmicos, diferenciales, etc ...
    </div>
    </div>
    <h3><a class="1" href="#">iluminaci&oacute;n</a></h3>
    <div>
    <div class="cat_content_ex">
        luminarias std y sistemas de LED, etc ...
    </div>
    </div>
</div>
<div id="target"></div> 

脚本

<script type="text/javascript">
    $('h3 > a').live('click', function(){
        var currentClass = $(this).attr('class');
        alert ( currentClass, 'alert window');
        if(currentClass == '0') {
            <?php
                include "./connection_catalogs.php";                
                $SQL = "SELECT * FROM first_table";
                $result = mysql_query($SQL);
                while ($row = mysql_fetch_array($result)) { ?>
                    $('#target').html($(<?php echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";?>)); 
                }
        } else if(ctg == '1') {
            <?php
                include "./connection_catalogs.php";                
                $SQL = "SELECT * FROM second_table";
                $result = mysql_query($SQL);
                while ($row = mysql_fetch_array($result)) { ?>
                    $('#target').html($(<?php echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";?>)); 
                }
                }
            ?>
        }
});
</script>

用户点击手风琴的第一个标题,并通过执行echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";

显示名为'first_table'的sql表的内容

用户点击accordion的第二个标题,并通过执行echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";

显示名为'second_table'的sql表的内容

3 个答案:

答案 0 :(得分:2)

$('#accordion.h3')选择id="accordion" class="h3"的元素。

将其更改为$('#accordion > h3'),选择<h3>元素,其中父元素为id="accordion"

答案 1 :(得分:1)

while ($row = mysql_fetch_array($result)) { ?>
    document.write(<?php echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";?>) 
}

答案 2 :(得分:0)

最后我用ajax制作了。

当用户点击链接时调用jscript函数

HTML

<div id="accordion">
    <h3><a onClick= MuestraCatalogo(1) href="#">aparamenta modular</a></h3>
    <div>
    <div class="cat_content_ex">
        t&eacute;rmicos, diferenciales, etc ...
    </div>
    </div>
    <h3><a onClick= MuestraCatalogo(2) href="#">iluminaci&oacute;n</a></h3>
    <div>
    <div class="cat_content_ex">
        luminarias std y sistemas de LED, etc ...
    </div>
    </div>
</div>
<div id="target"></div> 

脚本

function MuestraCatalogo(str)
{
if (str=="")
  {
  document.getElementById("cat_rcpt").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("cat_rcpt").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","auth/peticion_catalogo.php?q="+str,true);
xmlhttp.send();
}

peticion_catalogo.php

<?php
    $q=$_GET["q"];
    include "../connection_catalogs.php";
    if($q == 1){
            $SQL = "SELECT * FROM aparamenta_modular";
    } else if($q == 2){
            $SQL = "SELECT * FROM control_industrial";
    }
    $result = mysql_query($SQL);
    echo "<table class='ctgPage'>";
    echo "<tr>";
    echo "<td class='ctgCabPage' style='width:150px'>FABRICANTE</td>";
    echo "<td class='ctgCabPage' style='width:300px'>CAT&Aacute;LOGO</td>";
    echo "<td colspan='2' class='ctgCabPage' style='width:150px'>ENLACE</td>";
    echo "</tr>";
    while ($row = mysql_fetch_array($result)) {
        echo "<td class='ctgCabData' style='width:150px'>" .$row['fabricante']. "</td>";
        echo "<td class='ctgCabData' style='width:300px'>" .$row['titulo']. "</td>";
        if($row['filetype']=="pdf") {
            $url_icon = "http://www.domain.com/img/pdf.png";
        } else if($row['filetype']=="xls") {
            $url_icon = "http://www.domain.com/img/xls.png";
        } else if($row['filetype']=="rar") {
            $url_icon = "http://www.domain.com/img/rar.png";
        } 
        echo "<td colspan='2' class='ctgCabData' style='width:150px'><a href='" .$row['url']. "'><img src='http://www.domain.com/img/download.png' alt='descarga'></a>&nbsp;&nbsp;<img src='" .$url. "' alt='type'></td>";
        echo "</tr>"; 
    }
    echo "</table>";
    mysql_close($con);  
?>