所以我想做的是使用手风琴按字母顺序显示数据库中的数据。这是我的代码:
<head>
<title>HeadCount - Create New Project</title>
<link href="jquery-ui/jquery-ui.css" rel="stylesheet" />
<script src="jquery-ui/jquery.js"></script>
<script src="jquery-ui/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#myAccordion"). accordion({heightStyle:"content"});
$(".source li").draggable({helper:"clone"});
$("#cart").droppable({drop:function(event,ui){
$("#items").append($("<li></li>").text(ui.draggable.text()));
}});
});
</script>
<style>
#myAccordion
{
width:400px;
float:left;
margin:25px;
}
li
{
margin-top:5px;
}
#cart
{
width:300px;
height:400px;
border:2px solid black;
float:left;
}
</style>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
</head>
<body>
<h2>Items</h2>
<div id="myAccordion">
<?php for($i=321; $i<347; $i++)
{
echo "<h3>".chr($i)."</h3>";
echo '<ul class="source">';
$sql = "SELECT username FROM user WHERE username LIKE '".chr($i+32)."%' ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$name= $row["username"];
echo"<li class='ui-state-highlight'>". $name ."</li>";
//echo "<br>". $name. "<br>";
}
} else
{
echo "0 results";
}
'</ul>';
}
?>
</div>
<div id="cart">
<h2>Project</h2>
<ol id="items"></ol>
</div>
</body>
现在我试图手动添加每个字母并从数据库中逐字逐句选择。但我想知道是否有更容易的方法来做到这一点。 thanx求助。
因此,在编辑代码后,其名称按字母排序,但现在显示在相同的手风琴选项卡下
答案 0 :(得分:2)
是的,您可以简单地使用for循环来执行此操作...类似于:
您可以使用chr
函数来获取字符形式的char代码。
通常;
chr(256 + 65) = A and chr(256 + 97) = a
chr(256 + 66) = B and chr(256 + 98) = b
chr(256 + 67) = C and chr(256 + 99) = c
and so on ...
所以你可以尝试类似的东西:
<?php for($i=321; $i<347;$i++){
echo "<h3>".chr($i)."</h3>";
echo '<ul class="source">';
$sql = "SELECT username FROM user WHERE username LIKE '".chr($i+32)."%' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
$name= $row["username"];
echo"<li class='ui-state-highlight'>". $name ."</li>";
//echo "<br>". $name. "<br>";
}
} else
{
echo "0 results";
}
?>
</ul>
编辑在查看以下有关性能问题的评论后,您可以尝试以下操作:
$sql = "SELECT username FROM user ORDER BY username ASC";
$result = $conn->query($sql);
$row = mysql_fetch_row($result);
for($i=321; $i<347;$i++){
echo "<h3>".chr($i)."</h3>";
echo '<ul class="source">';
foreach($row as $checkrow) {
if($checkrow == chr(i) || $checkrow == chr(i+32)) {
echo"<li class='ui-state-highlight'>". $checkrow ."</li>";
unset($row[key($row)]);
}
}
?>
</ul>
答案 1 :(得分:1)
使用ORDER BY column_name
这将按字母顺序排列该列,您可以检索该排序列
例如:
SELECT * FROM table_name ORDER BY column_name ASC|DESC,
column_name ASC|DESC;
现在您将如何在显示
时将数据分成字母组您可以使用substr("Your string", 0);
和变量
substr - 返回字符串的一部分
例如
...
while($row = $result->fetch_assoc())
{
$name= $row["username"];
$first_alpha =substr($name, 0);
if( isset($test) && ($test!=null) )
{
if($first_alpha == $test)
{
echo"<li class='ui-state-highlight'>". $name ."</li>";
}else{
$test = $first_alpha;
echo "<h3>".$first_alpha."</h3>";
echo"<li class='ui-state-highlight'>". $name ."</li>";
}
}else
{
$test = $first_alpha;
echo "<h3>".$first_alpha."</h3>";
echo"<li class='ui-state-highlight'>". $name ."</li>";
}