Dynamically display MySQL query result on HTML page

I am using the YII Framework and I would like to put the results of a MySQL query into a table in index.php.

MySQL query is already good:

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:

<?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 a blank <td> </ td>.

Does anyone know how to do this without jQuery?

+4
source share
5 answers

To connect to the database and the connection was defined in the yii configuration. Two lines of code should be added to the top of the page.

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

:

   $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,
    ));

, , , Yii CGridView

0

YII, .

mySql $result

, while/for loop :

<?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 needed and add the elements of the table and th before and after respectively.

-2
source

Source: https://habr.com/ru/post/1531511/


All Articles