【问题标题】:jquery select index from data and show sql contentjquery从数据中选择索引并显示sql内容
【发布时间】:2012-03-03 15:17:37
【问题描述】:

我不知道我怎样才能让它工作。我解释一下。

我有一些带有 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 "&lt;p class='ctg_fab'&gt;" .$row['fabricante']. " : " .$row['titulo']. " : &lt;a href='" .$row['url']. "'&gt;descarga&lt;/a&gt; : tipo - " .$row['filetype']. "&lt;/p&gt;";显示名为'first_table'的sql表的内容

用户单击手风琴的第二个标题并通过执行echo "&lt;p class='ctg_fab'&gt;" .$row['fabricante']. " : " .$row['titulo']. " : &lt;a href='" .$row['url']. "'&gt;descarga&lt;/a&gt; : tipo - " .$row['filetype']. "&lt;/p&gt;";显示名为'second_table'的sql表的内容

【问题讨论】:

  • 能否请您澄清一下,问题是什么?
  • 是的,我愿意。问题是那行不通。我希望用户单击每个手风琴的标题,根据单击的标题,它将显示 sql 表或其他表中的每个内容。
  • 用户点击h3后,您希望内容如何/在哪里显示?

标签: jquery mysql database


【解决方案1】:

我终于用 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);  
?>

【讨论】:

    【解决方案2】:

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

    将其更改为$('#accordion &gt; h3'),它会选择一个&lt;h3&gt; 元素,该元素的父元素为id="accordion"。

    【讨论】:

    • 是的,没关系,但脚本既不返回警报也不返回 sql 数据... :(
    【解决方案3】:
    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>";?>) 
    }
    

    【讨论】:

    • 不知道为什么要使用document.write函数...?
    • 它是否有效?因为您混合了 php 和 javascript,结果是,这两个概念都混淆了(让您感到困惑)。 php 的 echo 只是将结果打印到 javascript 代码中,而不是在 document/html 中。 document.write 是 javascipt 在文档中显示它的函数。不管怎样,尽量让你的代码更简单,更容易阅读,这样就不会再发生混淆了:)
    猜你喜欢
    • 2013-02-19
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多