如何在数据表中显示数据库中的数据

时间:2017-01-18 13:40:16

标签: javascript php mysql twitter-bootstrap datatables

我遵循了一个与数据表相关的基本教程和引导程序,但它只适用于硬编码数据。

如果我想动态显示它,我如何让它工作?它正在显示它,但搜索和显示条目#并不起作用。当它实际显示来自我的数据库的3个数据时,它还说表中没有数据。

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Bootstrap 101 Template</title>

        <!-- Bootstrap -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href=" //maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet">



      </head>
    <body>
        <br/>
        <hr/>
        <?php
        require_once("/dao/CategoryDAO.php");
        require_once("/dao/TopicDAO.php");

        $category = new CategoryDAO();
        $topic = new TopicDAO();
        $allCategories_arr = $category->getAllCategories();
        $allTopics_arr = $topic->getAllTopicTitles();

        ?>
        <div class="container">
            <div class="row">
                <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th>Category ID</th>
                        <th>Category Name</th>
                        <th>Action</th>
                    </tr>
                    <?php
                    foreach ($allCategories_arr as $ar) {
                        echo "<tr><th>".$ar['category_id']."</th><td>".$ar['categoryname']."</td><th><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a></th></tr>";
                    }
                    ?>
                    </thead>
                    <tbody>


                    </tbody>
                </table>


            </div>
        </div>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
        <script src="//code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#example').DataTable();
            } );
        </script>

    </body>
        </html>

3 个答案:

答案 0 :(得分:1)

快速修复,您应该在开始循环之前关闭<thead>标记,并在<tbody>内部<tbody>内显示<th>内的结果后不使用<tr>'s and <td>'s 1}}标记,您使用<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <title>Bootstrap 101 Template</title> <!-- Bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href=" //maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet"> </head> <body> <br/> <hr/> <?php require_once("/dao/CategoryDAO.php"); require_once("/dao/TopicDAO.php"); $category = new CategoryDAO(); $topic = new TopicDAO(); $allCategories_arr = $category->getAllCategories(); $allTopics_arr = $topic->getAllTopicTitles(); ?> <div class="container"> <div class="row"> <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%"> <thead> <tr> <th>Category ID</th> <th>Category Name</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach ($allCategories_arr as $ar) { echo "<tr>"; echo "<td>".$ar['category_id']."</td>"; echo "<td>".$ar['categoryname']."</td>"; echo "<td><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a>"; echo "</tr>"; } ?> </tbody> </table> </div> </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#example').DataTable(); } ); </script> </body> </html>

这是您的代码的外观;

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Bootstrap 101 Template</title>
        <!-- Bootstrap -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href=" //maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <br/>
        <hr/>
        <?php
            require_once("/dao/CategoryDAO.php");
            require_once("/dao/TopicDAO.php");

            $category = new CategoryDAO();
            $topic = new TopicDAO();
            $allCategories_arr = $category->getAllCategories();
            $allTopics_arr = $topic->getAllTopicTitles();

            ?>
        <div class="container">
            <div class="row">
                <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>Category ID</th>
                            <th>Category Name</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            foreach ($allCategories_arr as $ar) :?>
                        <tr>
                            <td><?php echo $ar['category_id'] ;?></td>
                            <td><?php echo $ar['categoryname'];?></td>
                            <td><a href="ManageSubCategory.php?catid="<?php echo $ar['category_id'];?>">Show Sub Categories</a>
                        </tr>
                        <?php
                            endforeach;


                            ?>
                    </tbody>
                </table>
            </div>
        </div>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
        <script src="//code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#example').DataTable();
            } );
        </script>
    </body>
</html>

或者应该是这样的

public class StreamListenerService extends Service implements MediaPlayer.OnPreparedListener,
        MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {

    public static MediaPlayer mMediaPlayer;
    private String streamUrl;
    public static boolean isPlaying = false;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        streamUrl = intent.getStringExtra("streamUrl");



        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.reset();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


        try {
            mMediaPlayer.setDataSource(streamUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }


        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnCompletionListener(this);

        isPlaying = true;

        return Service.START_STICKY_COMPATIBILITY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.d("StreamListenerService", "onDestroy");
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
            mMediaPlayer.reset();
            isPlaying = false;
        }
        super.onDestroy();

    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mMediaPlayer.start();
        isPlaying = true;
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {

        Toast.makeText(this, R.string.text_problem_in_playing_audio_stream, Toast.LENGTH_SHORT).show();

        mMediaPlayer.stop();
        mMediaPlayer.reset();

        //Toast.makeText(this, R.string.text_problem_in_playing_stream,Toast.LENGTH_SHORT).show();

        this.onDestroy();
        return false;
    }


    @Override
    public void onCompletion(MediaPlayer mp) {
        Toast.makeText(this, R.string.text_stream_finished, Toast.LENGTH_SHORT).show();
        mMediaPlayer = mp;
        mMediaPlayer.stop();
        mMediaPlayer.reset();
        this.onDestroy();
    }
}

答案 1 :(得分:0)

将此代码写入正文标记

  <?php
                foreach ($allCategories_arr as $ar) {
                    echo "<tr><td>".$ar['category_id']."</td><td>".$ar['categoryname']."</td><td><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a></td></tr>";
                }
                ?>

我希望它的工作

答案 2 :(得分:0)

<tbody>内设置php foreach而不是<thead>并将foreach中的<th>更改为<td>

<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th>Category ID</th>
                        <th>Category Name</th>
                        <th>Action</th>
                    </tr>

                    </thead>
                    <tbody>

<?php
                    foreach ($allCategories_arr as $ar) {
                        echo "<tr><td>".$ar['category_id']."</td><td>".$ar['categoryname']."</td><td><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a></td></tr>";
                    }
                    ?>
                    </tbody>
                </table>