Javascript MYSQL下拉列表

Javascript MYSQL Dropdown

本文关键字:下拉列表 MYSQL Javascript      更新时间:2023-09-26

如果a)这个问题已经得到回答,我已经搜索了,但无法使用我找到的答案,请提前道歉;b)我不是一个非常熟练的程序员,正在努力学习。。。

为了尽可能短。。。

  • 我有一个功能齐全的网站,有一个可搜索和可排序的数据库(这是一位同事很久以前为我建立的,所以我特别困惑)
  • 我想添加一个从数据库填充的下拉筛选器
  • 代码引用了javascript和PHP。据我所知,PHP为我填充了表数据,javascript触发了"show xx entries"过滤器和搜索框,这两个功能都很棒

我的问题是,我无法获得任何下拉列表的代码,我想添加到工作中。我可以得到下拉列表来填充,但实际上不能过滤数据:(

<?php

$dbhost = 'localhost';
$dbuser = 'database';
$dbpass = 'password;
$dbname = 'marchmadness';
$table = 'leaderboard';
//connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Leaderboard</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <link rel="icon" type="image/png" href="/favicon.ico" />
   <link rel="stylesheet" type="text/css" href="assets/css/main.css">
    <link rel="stylesheet" type="text/css" href="assets/css/listing.css" media="all" />
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript" language="javascript" src="assets/js/jquery.dataTables.min.js"></script> 
    <script type="text/javascript">
$(document).ready(function() {
            /* This triggers the cool filtering stuff, without this it's just a normal table of data */
            $('table').dataTable({
                "iDisplayLength": 10
            });
        });
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

}
    </script>

</head>
<body id="listing">
<div id="wrapper">
<div id="topbox" align="center">
<img src="_Images/maddnessheader.png" width="539" height="296" align="center" /></div>
&nbsp; <br/>
&nbsp; <br/>
&nbsp; <br/>
&nbsp; <br/>
&nbsp; <br/>
&nbsp; <br/>
<?
  $sql="SELECT Title FROM leaderboard"; 
$result=mysql_query($sql); 
$options=""; 
while ($row=mysql_fetch_array($result)) { 
    $id=$row["leaderboard"]; 
    $thing=$row["Title"]; 
    $options.="<OPTION VALUE='"$id'">".$thing.'</option>';
} 
?>

<SELECT NAME=thing> 
<OPTION VALUE=0>Choose 
<?=$options?> 
</SELEC

T>

  <table align="center">
<thead>
    <tr>
        <th>Title</th>
        <th>Name</th>
        <th>Call Coding (%)</th>
        <th>FizzBack SAT Score (%)</th>
    </tr>
</thead>

<tbody>

<?php

    //get the information from the database
    $result = mysql_query("SELECT * FROM `$table`;") or die(mysql_error());
    while($row = mysql_fetch_array($result)) {
        echo '<tr>';
        // print out the data from the database. Notice how the text inside $row[] matches up with the headers in phpmyadmin
        echo '<td>' . htmlentities($row['Title']) . '</td>';
        echo '<td>' . htmlentities($row['Name']) . '</td>';
        echo '<td>' . htmlentities($row['CC']) . '</td>';
        echo '<td>' . htmlentities($row['FZB']) . '</td>';

        echo "</tr>'n";
    }
?>

</tbody>
</table>

</div>
</body>
</html>

我想我必须操作javascript来添加我需要的内容,因为这就是工作下拉列表和搜索框的来源。。。但我的javascript:-''如果有人能帮我,我会非常感激。如果需要,我也可以共享现有的Java:D

干杯!

试试看,我编辑了一些看起来不太正确的东西,并添加了一些有望过滤内容的代码。有点猜测,因为我从未使用过数据表,也不知道数据类型是什么。

<?php
    $dbhost = 'localhost';
    $dbuser = 'database';
    $dbpass = 'password';
    $dbname = 'marchmadness';
    $table = 'leaderboard';
    //connect to the database
    $db = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());
?>
<!doctype html>
<html lang="en">
    <head>
        <title>Leaderboard</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="icon" type="image/png" href="/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="assets/css/main.css">
        <link rel="stylesheet" type="text/css" href="assets/css/listing.css" media="all" />
        <script type="text/javascript" src="assets/js/jquery.min.js"></script>
        <script type="text/javascript" language="javascript" src="assets/js/jquery.dataTables.min.js"></script> 
        <script type="text/javascript">
            var table; // used to store a reference later
            $.fn.dataTablesExt.afnFiltering.push(
                function( oSettings, aData, iDataIndex ) {
                    if (aData[0].toLowerCase() == $('#title_filter').val().toLowerCase()) {
                        return true;
                    } else {
                        return false;
                    }
                }
            );
            $(document).ready(function() {
                /* This triggers the cool filtering stuff, without this it's just a normal table of data */
                table = $('table').dataTable({ "iDisplayLength": 10 }); // this starts the datatable stuff and returns a reference to it
            });
            $("#title_filter").onchange(function() { 
                table.fnDraw(); // forgot this line - whoops
            });
            function MM_preloadImages() { //v3.0
                var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
                var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
                if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
            }
        </script>
    </head>
    <body id="listing">
        <div id="wrapper">
            <div id="topbox" align="center">
                <img src="_Images/maddnessheader.png" width="539" height="296" align="center" /></div>
<?php
    $sql="SELECT Title FROM leaderboard"; 
    $result=mysql_query($sql); 
    $options=""; 
    while ($row=mysql_fetch_array($result)) { 
        $id=$row["leaderboard"]; 
        $thing=$row["Title"]; 
        $options.="<option value='"$thing'">".$thing.'</option>';
    } 
?>
                <select id="title_filter"> 
                    <?=$options?> 
                </select>
                <table align="center">
                    <thead>
                        <tr>
                            <th>Title</th>
                            <th>Name</th>
                            <th>Call Coding (%)</th>
                            <th>FizzBack SAT Score (%)</th>
                        </tr>
                    </thead>
                <tbody>
<?php
    //get the information from the database
    $result = mysql_query("SELECT * FROM `$table`;") or die(mysql_error());
    while($row = mysql_fetch_array($result)) {
        echo '<tr>';
        // print out the data from the database. Notice how the text inside $row[] matches up with the headers in phpmyadmin
        echo '<td>' . htmlentities($row['Title']) . '</td>';
        echo '<td>' . htmlentities($row['Name']) . '</td>';
        echo '<td>' . htmlentities($row['CC']) . '</td>';
        echo '<td>' . htmlentities($row['FZB']) . '</td>';
        echo "</tr>'n";
    }
?>
                </tbody>
            </table>
        </div>
    </body>
</html>