dousui8263 2013-06-15 18:43
浏览 41

用php将mysql数据添加到手风琴中

[code]

<?php
mysql_connect("","","") or die("Could not connect to localhost");
mysql_select_db("") or die( "Could not connect to database");

$names[] = mysql_query("SELECT * FROM list ORDER BY name ASC");

// 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>

I get two errors:

Warning: substr() expects parameter 1 to be string, resource given in C:\xampp\htdocs\test.php on line 14

Warning: ucfirst() expects parameter 1 to be string, resource given in C:\xampp\htdocs\test.php on line 49

If I put the names out of array how would I get the names into the accordion?

  • 写回答

2条回答 默认 最新

  • douzheng0702 2013-06-15 18:45
    关注

    Use mysql_fetch_* functions to handle the resources returned by mysql_query.

    $qry = mysql_query("SELECT * FROM list ORDER BY name ASC");
    
    while ($data = mysql_fetch_assoc($qry))
      echo $data['name']; // contains list.name from your db
    

    Besides, you'd prefer using PDO as mysql_* is deprecated since 5.3. Look about it there.

    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突