-1

Apologies in advance if a) this question has already been answered, I did search but am not able to use the answers I've found and b) I am not the a very skilled programmer and am trying to learn...

To make this as short as possible...

  • I have a fully functioning site with a searchable and sortable database (that a co-worker pretty much build for me a long time ago, hence why I am extra confused)
  • I would like to add a dropdown filter that populates from the database
  • The code references javascript and PHP. From what I understand, the PHP populates the table data for me and the javascript triggers the 'show xx entries' filter and the search box - which are both awesome

My issue is I cannot get any code for the dropdown I would like to add to work. I can get the dropdown to populate but not actually filter the data :(

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

I think I would have to manipulate the javascript to add what I need, as that's where the the working dropdown and search box come from... but I suck a javascript :-\ If anyone could help I would REALLY appreciate it. I can share the existing Java as well if needed :D

Cheers!

4

1 回答 1

0

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

<?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>
于 2013-02-22T01:54:29.760 回答