php - Alphabet accordion from database jquery -


so im trying display data database in alphabetical order using accordion. here code:

<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 user username '".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> 

for trying add each letter manually , select letter letter database. wonder if there can easier way this. thanx ur help.

so after editing code names sorted letters, displaying under same accordion tab

yes simple use loop it... like: can use chr function character form char code.. usually;

chr(256 + 65) = , chr(256 + 97) = chr(256 + 66) = b , chr(256 + 98) = b chr(256 + 67) = c , chr(256 + 99) = c , on ... 

so can try like:

<?php for($i=321; $i<347;$i++){ echo "<h3>".chr($i)."</h3>"; echo '<ul class="source">';      $sql = "select username user username '".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> 

edit after looking @ comments below performance issues try following:

$sql = "select username user order 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 $checkrow) {         if($checkrow == chr(i) || $checkrow == chr(i+32)) {             echo"<li class='ui-state-highlight'>". $checkrow ."</li>";             unset($row[key($row)]);         }     }  ?>  </ul>      

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -