I use the YII Framework and I would like to put the results of a MySQL query in a table in index.php.
The 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 at the moment:
<?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 documentation.
Feel free to ask in the forum,
should you have any questions.</p>-->
<table>
<caption>État des systèmes</caption>
<tr>
<th>Catégorie</th>
<th>Nom</th>
<th>É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>.
Does anyone know how to do it without jQuery?
Since you are using Yii framework you can use CGridView component. 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
To connect to the database and the connection was defined in the config 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.
Link: 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
I am not so sure of the YII framework but this should definitely help you out.
Let the result of the mySql query be in a variable $result
now , start with while/for loop like this :
<?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.
Related
Im using a custom MVC in PHP
I have two tables.
Controles and professors
professors table contains an id of course --> id_professor
controles table contains a field called --> id_professors in which I store a list of profs ids this way ==> 1;2;5;9
In my datatable Im retrieving a list of controles from controles table
for the Model, Im using this class
public function fetchControlesListe(){
stmt = $this->db->prepare("SELECT * FROM controles ");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
Controller
liste_controles.php
class Liste_Controles extends Controller {
function __construct(){
parent::__construct();
}
$this->view->fetchCtrlListe= $this->model->fetchControlesListe();
$this->view->render('liste_controles/index');
}
VIEW
<table>
<thead>
<tr>
<th>N°</th>
<th>Module</th>
<th>Salle</th>
<th>Surveillants</th>
</tr>
</thead>
<tbody>
<?php foreach($this->fetchCtrlListe AS $key=>$value): ?>
<tr>
<td><?php echo $controle['id_controle']; ?></td>
<td><?php echo $controle['intitule_module']; ?></td> <
<td><?php echo $controle['intitule_salle'];?></td>
<td>
<?php foreach($this->prof as $profs): ?>
HERE, I would like to retrieve the list of profs from professors
table and the field id_profs in professors table stores an
array, as i said before, containing a list of ids (Ex: 1;2;3;4;5)
I would like to loop through that array and retrieve the name of
professor from professors table if the id_professor Exists in that array
. I know I have to create another METHOD but The PROBLEM is i don't know how to pass parameters in the method and call it from the controller to pass it to the view . and the param would be $controle['id_professor']
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
The result would be like this:
EDIT:
I know that is not a good idea to store lots of ids as an array, but
my question I just want to know how to do a foreach loop inside
another foreach loop using another method with a paramater that will
be passed from the first loop
There isn't one solution but what you can do is, because you know that id_professors is a string composed of ids separated by commas, you can retrieve an array of identifiers this way :
$ids = explode(",", $profs[id_professors']);
and then
<?php foreach($this->prof as $profs): ?>
<?php $ids = explode(",", $profs['id_professors']); ?>
<?php foreach($ids as $id): ?>
//DO STUFF with $id
<?php endforeach; ?>
<?php endforeach; ?>
Notice that the entire thing is not very beautiful because of opening/closing php tags constantly but I hope you'll make a wonderful templates system for your MVC framework :)
From there, it would not be that nice to access the database directly from the view so a better (not perfect) solution would be to provide the view an array made of professors like this one :
$this->view->profs_list = array(
1 => "Prof 1",
2 => "Prof 2",
//etc.
);
with the key of the array being the profs' identifier.
Then you can retrieve their name through the loop :
<?php foreach($this->prof as $profs): ?>
<?php $ids = explode(",", $profs['id_professors']); ?>
<?php foreach($ids as $id): ?>
<span><?php echo $this->profs_list[$id]; ?></span><br />
<?php endforeach; ?>
<?php endforeach; ?>
In my php page I am getting a list of cities and events dynamically from a database. Then I'm passing this sql data to jquery to render a table with the data. Sample table created by jquery could be like:
<tr>
<td id='cityMumbai'>Mumabai</td>
<td id='program1Start>11/11/11</td>
<td id='program2Start'>11/15/11</td>
<tr>
Now I've come to know that DOM can be used to manipulate table structure in php.
I saw various reference, and found it is being used to create a new page. Means the created paged is saved physically.
Possible Pseudo Code:
{
<span id="eventRows">
<? $object = new DOMDocument;//it should pick this specific instance ?>
<? while($row = mysql_fetch_array($rProgList,MYSQL_ASSOC)):?>
<? // write code to check if current cityname exitst?
$td = $object->getElementById($row['cityname']);
if(!$td.exists): ?>
<tr><td id="<? echo $row['cityname'];?>"><? echo $row['cityname'];?></td></tr>
<? else : ?>
<?
//code to insert only event information in the current city object
$td.append("dfkjsdflkjasdhflkjsfh");
?>
<? endif; ?>
<? endwhile; ?>
</span>
}
Can I use DOM in PHP to modify the page output, only... replacing the task jquery is doing using PHP. Not to modify a physical copy of it.
Fairly new and inexperienced with PHP, and I'm stuck.
I have a feature that creates a new sub page that is related to a main page, eg. main page 1 has sub page 1 a, 1b and 1c, and main page 2 has sub page 2a, 2b, 2c and so forth. You gain access to the given sub pages through links in a menu on each of the main pages. I hope you're still with me here.
I have succeeded in getting the menu to link to one of the created sub pages, but what I now need is to get a link dynamically created in the menu whenever I create a new sub page. Main pages and their "id" is connected to the related sub pages via the column "mainid" that corresponds to eachother (eg. index.php?id=5&mainid=5)
The link menu is created by using $_GET.
I have this page: localhost/site/index.php?id=1&mainid=1 with the following code that i'm struggling with:
<table width="90%" border="0" cellpadding="5"><tbody>
<tr>
<td>
<?php
$id = $mysqli->query("SELECT id FROM page_content WHERE mainid =" . $_GET['mainid'])->fetch_object()->id;
$name = $mysqli->query("SELECT name FROM page_content WHERE mainid =" . $_GET['mainid'])->fetch_object()->name;
?>
<a href="index.php?id=<?php echo $id; ?>&mainid=<?php echo $_GET['mainid']; ?>">
<?php
print $name;
?></a></td>
<td> </td>
</tr>
This creates a link to content of the sub page. The content of this sub page is stored in id=10 and mainid=5 (thus creating the link index.php?id=10&mainid=5
So far so good.
But how do I get links to be dynamically created on a main page whenever I create a new sub page belonging to that given main page?
Help is much appreciated
Thanks
I'm not sure what you are tying to do, but i guess you want to make a loop from database.
To do that, you have to modify your code because right now is not safe, and not good.
use this one instead:
<table width="90%" border="0" cellpadding="5"><tbody>
<tr>
<?php
/* create a prepared statement */
$query = $mysqli->prepare("SELECT id,name FROM page_content WHERE mainid = ?");
/* bind parameters */
$query->bind_param("i", $_GET['mainid']);
$query->execute();
$menu_result = $query->get_result();
/* now you can fetch the results into an array */
while ($menu = $menu_result->fetch_assoc()) {
echo <<<HTML
<td>
{$menu['name']}
</td>
<td> </td>
HTML;
}
?>
</tr>
</tbody>
</table>
This is my first code in php, so my problem might be so obvious. sorry if it is so easy :)
What I'm trying to do is that I am selecting some rows from my data base using
$rrows = Select ( "*" , $tbl_SubForum , null, "p");
$rrows->setFetchMode(PDO::FETCH_CLASS, 'subForum');
I know this works fine.
Each row has the description of a sub forum, containing title and id. I am trying to show sub forum titles in table cells using this code:
<table cellpadding=50px cellspacing=20px BORDER=0>
<?php
$i=0;
while($rrow = $rrows->fetch()){
var_dump($rrow);
?>
<tr>
<td class='subforum' id='subforum1'>
<?php echo $rrow["title"]; ?><br>
Sub forum manager<br>
Posts: 200<br>
Active users: 50<br>
</td>
</tr>
<?php
$i++;
}
?>
the line echo $rrow["title"]; doesn't work and so the page is empty, except for the result of the first var_dump
First var_dump of the first $rrow shows:
as you can see, there actually is a title field in the array and there is only one var_dump so the while loop doesn't work anymore!
why is this happening?
Because $rrow is an object rather than an array, you have to use $rrow->title to access its data member.
I am coming from Asp.net world, where everything is so modular that it creates headaches and nausea sometimes and you forget what you were actually doing.
Here in PHP I was trying to study how to display tabular data, and the solution was very simple but that looked very hasty, because if I write lots of code then it will become haywire.
So my question is what standard should I follow to work with repeating data, if there is any? Please guide me to some quality material to study because I tried but did not find anything. I have studied this link.
[Edit]
The way asp.net render tabular data is by using GridView. here you just create an instance of the GridView class and send it the data retrieved from the database and remain job is done behind the scene without messing the C# or VB code with html or writing the same logic again and again. I mean it is nice example of code resuability and I want to utilize something similar here in php to write the repeating code once and then inherit it if I want it somewhere else.
I myself have migrated from ASP.NET to php and one word of advice is forget what ever you learnt in ASP.net as ASP.net abstracts a lot of thing in web development to a point you forget that you are working with basic html/jscript and http. but again, this is just a personal opinion.
Ragarding your question on how to display a table and populate it with data. Have a look at this example. Here
<?php
$people = array(
array('Tom', '16'),
array('John', '21'),
array('Kate', '19'),
array('Luke', '25')
)
?>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<td>Name</td><td>Age</td>
</tr>
<?php foreach($people as $person){ ?>
<tr>
<td><?php echo $person[0]; ?></td>
<td><?php echo $person[1]; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
I create a 2d array contain data about People and their age. Due to simplicity sake, im using a hard coded array and not retrieving data from a database.
I then use a while loop to loop through the data in the array and for each tuple, I add a <tr> in the table. This is something like a repeater in ASP.NET
DEMO
Just noticed you want a modularized version. Here is an example:
index.php
<?php
include('inc.datasource.php');
$people = DataSource::getUsers();
?>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<td>Name</td><td>Age</td>
</tr>
<!-- Render rows with data -->
<?php foreach($people as $person){ ?>
<tr>
<td><?php echo $person[0]; ?></td>
<td><?php echo $person[1]; ?></td>
</tr>
<?php } ?>
<!-- End render rows with data -->
</table>
</body>
</html>
inc.database.php
<?php
class DataSource{
public static function getUsers(){
//Connect to database
//Retrieve user data
//Return user data
//Simulating the above steps for simplicity sake
$people = array(
array('Tom', '16'),
array('John', '21'),
array('Kate', '19'),
array('Luke', '25')
);
return $people;
}
}
?>
If you want to completly seperate your php from your html. I suggest you use one of the php mvc frameworks. I personally like using Laravel and CodeIgniter