doupishan3309 2013-06-15 17:25
浏览 53
已采纳

如何在Jquery中制作一个用MySQL数据库显示数据的手风琴?

How would I make an accordion in JQuery that shows data from my mysql database using PHP. So I would like it to show the data by alphabetical order like this: enter image description here

... So until the user clicks on the "A" it doesn't show the data and same for b and so on..

So far I have this :

enter image description here

using Which is not in an accordion. Can someone please help me get started on this. It will be really appreciated.

[Code]

<div id="accordion">
<font face="Calibri" style="italic" size='10px' color="white">
  <?php
    mysql_connect("host","user","pass!") or die("Could not connect to localhost");
mysql_select_db("db") or die( "Could not connect to database");
?>
<?php
$result = mysql_query("SELECT * FROM table ORDER BY name ASC");
echo "<div class='scroll'>";
while ($row = mysql_fetch_array($result))
  {
   echo "<div style='margin: 0 auto;'>
                    <span style='display:inline-block; width:200px; text-align:left;'>" . ucwords($row['name']) . "</span>
                    <span style='display:inline-block; text-align:left;'>" . ucwords($row['number']) . "</span>
                </div>
                <br>";
  }
  echo "</div>";
?>
</div>
  • 写回答

4条回答 默认 最新

  • doujiyun8846 2013-06-15 17:52
    关注

    You can do something like this.

    $names = ['alex', 'adam', 'bob', 'bryan'];
    asort($names); // The list wasn't sorted, if you don't want sorting you can just remove this line.
    
    // Prepare list for accordion.
    $accordionData = [];
    foreach($names as $name) {
      $accordionData[substr($name, 0, 1)][] = $name;
    }
    
    // Print accordion, change the echoes to reflect your accordion html.
    foreach($accordionData as $index => $names) {
      echo strtoupper($index).'<br />';
      foreach($names as $name) {
        echo ucfirst($name).'<br />'; // ucfirst changes the first letter to upper case.
      }
    }
    

    output:

    A
    Adam
    Alex
    B
    Bob
    Bryan
    

    If your question included how to make the actual accordion, just try the code available here: http://jqueryui.com/accordion/

    And here is a working example based on jquery ui

    // You should replace this with your DB data.
    $names = ['alex', 'adam', 'bob', 'bryan'];
    
    // The list wasn't sorted, if you don't want sorting you can just remove this line.
    asort($names);
    
    // Prepare list for accordion.
    $accordionData = [];
    foreach($names as $name) {
      $accordionData[substr($name, 0, 1)][] = $name;
    }
    
    ?>
    <!doctype html>
    
    <html lang="en">
    <head>
      <meta charset="utf-8" />
      <title>jQuery UI Accordion - Collapse content</title>
      <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
      <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css" />
      <script>
      $(function() {
        $( "#accordion" ).accordion({
          collapsible: true,
          active: false
        });
      });
      </script>
    </head>
    <body>
    <div id="accordion">
    <?php
    // Print accordion, change the echoes to reflect your accordion html.
    foreach($accordionData as $index => $names) {
    ?>
    
      <h3><?php echo strtoupper($index); ?></h3>
      <div>
      <?php
      foreach($names as $name) {
      ?>
        <p><?php echo ucfirst($name); ?></p>
      <?php
      }
      ?>
      </div>
      <?php
    }
    ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)