简体   繁体   English

将MySQL查询的结果动态显示到HTML页面中

[英]Dynamically display the result of a MySQL query into an HTML page

I use the YII Framework and I would like to put the results of a MySQL query in a table in index.php. 我使用YII框架,我想将MySQL查询的结果放在index.php的表中。

The MySQL query is already good: MySQL查询已经很好了:

SELECT categories.name,
    systemes.name,
    systemes.etat_de_base,
    maintenances.name,
    maintenances.date,
    maintenances.duree 
FROM systemes_maintenances 
LEFT JOIN systemes 
ON systemes_maintenances.id_systemes = systemes.id_systemes 
LEFT JOIN categories 
ON systemes.id_categories = categories.id_categories 
LEFT JOIN maintenances 
ON systemes_maintenances.id_maintenances = maintenances.id_maintenances;

And my PHP page looks like this at the moment: 我的PHP页面目前看起来像这样:

<?php
/* @var $this SiteController */

$this->pageTitle=Yii::app()->name;
?>

<!--<h1>Welcome to <i><?php echo CHtml::encode(Yii::app()->name); ?></i></h1>

<p>Congratulations! You have successfully created your Yii application.</p>

<p>You may change the content of this page by modifying the following two files:</p>
<ul>
    <li>View file: <code><?php echo __FILE__; ?></code></li>
    <li>Layout file: <code><?php echo $this->getLayoutFile('main'); ?></code></li>
</ul>

<p>For more details on how to further develop this application, please read
the <a href="http://www.yiiframework.com/doc/">documentation</a>.
Feel free to ask in the <a href="http://www.yiiframework.com/forum/">forum</a>,
should you have any questions.</p>-->

<table>
    <caption>&Eacute;tat des systèmes</caption>
    <tr>
    <th>Cat&eacute;gorie</th>
    <th>Nom</th>
    <th>&Eacute;tat actuel</th>
    <th>Maintenance prévue</th>
    <th>Début de l'incident</th>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>



</table>

I want to display the results in the empty <td> </ td> . 我想在空<td> </ td>显示结果。

Does anyone know how to do it without jQuery? 没有jQuery,有谁知道如何做到这一点?

Since you are using Yii framework you can use CGridView component. 由于您使用的是Yii框架,因此可以使用CGridView组件。 This give nice set of features such as sorting, pagination and filtering. 这提供了一组很好的功能,例如排序,分页和过滤。 Check following link for example usage. 检查以下链接以获取示例用法。 http://www.yiiplayground.com/index.php?r=UiModule/dataview/gridView http://www.yiiplayground.com/index.php?r=UiModule/dataview/gridView

To connect to the database and the connection was defined in the config yii. 要连接到数据库,并在配置yii中定义了连接。 Must be added to the top of page two line of code. 必须添加到页面顶部的两行代码。

$sql = 'querySQL';
$connection=Yii::app()->db;
$dataReader=$connection->createCommand($sql)->query();

their is a tutorial How to display data in php from Mysql. 他们是一个教程如何在Mysql中显示PHP中的数据。

Link: http://hightechnology.in/how-to-display-data-in-php-from-mysql/ 链接: http//hightechnology.in/how-to-display-data-in-php-from-mysql/

may it will help you. 它可以帮到你。

Try sth like this: 试试这样:

   $query = "SELECT categories.name,
        systemes.name,
        systemes.etat_de_base,
        maintenances.name,
        maintenances.date,
        maintenances.duree 
        FROM systemes_maintenances 
        LEFT JOIN systemes 
        ON systemes_maintenances.id_systemes = systemes.id_systemes 
        LEFT JOIN categories 
        ON systemes.id_categories = categories.id_categories 
        LEFT JOIN maintenances 
        ON systemes_maintenances.id_maintenances = maintenances.id_maintenances";

    $count= Yii::app()->db->createCommand($query)->queryScalar();

    $dataProvider = new CSqlDataProvider($query, array(
           'totalItemCount'=>(int) $count,
           'keyField' => 'SOME_UNIQUE_ID_FROM_THE_SQL',
           'pagination'=>array( 'pageSize'=>30, ),
    ));

    $this->widget('zii.widgets.grid.CGridView', array(
      'id'=>'yourGrid',
      'dataProvider'=> $dataProvider,
    ));

You will have to customize the grid yourself, so that it displays only what you need, but you can find this in the Yii documentation of CGridView 您必须自己自定义网格,以便它只显示您需要的内容,但您可以在CGridView的Yii文档中找到它

I am not so sure of the YII framework but this should definitely help you out. 我对YII框架不太确定,但这绝对可以帮到你。

Let the result of the mySql query be in a variable $result 让mySql查询的结果在变量$ result中

now , start with while/for loop like this : 现在,以while / for循环开头,如下所示:

<?php

while ($row=mysqli_fetch_array($result)){
    //Do something with the $row variable data
?>
    <td>Some Data</td>
    <td>echo $row["SomeColoumn1"];</td>
    <td>echo $row["SomeColoumn2"];</td>
    <td>echo $row["SomeColoumn3"];</td>
    <td>echo $row["SomeColoumn4"];</td>
<?php    
}

?>

This should print all the rows of the table as required and add the table and th parts before and after accordingly. 这应该根据需要打印该表中的所有行之前和之后,相应的添加部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM