Create table with PHP and populate from MySQL
Here is a full example of what you're looking for:
- pull some data from mysql using php
- put that data into an html table
- apply alternating colored rows to the table
For the styling I cheat a little and use jquery which I find a bit easier then what you're trying to do.
Also, remember $row[field] is case sensitive. So $row[id] != $row[ID].
Hope this helps:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
tr.header
{
font-weight:bold;
}
tr.alt
{
background-color: #777777;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('.striped tr:even').addClass('alt');
});
</script>
<title></title>
</head>
<body>
<?php
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("MyDatabase",$server);
$query = mysql_query("select * from employees");
?>
<table class="striped">
<tr class="header">
<td>Id</td>
<td>Name</td>
<td>Title</td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[ID]."</td>";
echo "<td>".$row[Name]."</td>";
echo "<td>".$row[Title]."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
Here's the table code only using PHP to alternate the styles like you're trying to do in your example:
<table class="striped">
<tr class="header">
<td>Id</td>
<td>Title</td>
<td>Date</td>
</tr>
<?php
$i = 0;
while ($row = mysql_fetch_array($query)) {
$class = ($i == 0) ? "" : "alt";
echo "<tr class=\"".$class."\">";
echo "<td>".$row[ID]."</td>";
echo "<td>".$row[Name]."</td>";
echo "<td>".$row[Title]."</td>";
echo "</tr>";
$i = ($i==0) ? 1:0;
}
?>
</table>
How to populate table from SQL data using PHP
It would be easier to answer your question if I could see the exact structure of your data, but I'll give this a whirl anyway and hopefully you can see where i'm going with it.
What I would do is create an array and fill in the information as you go through the results. Once you have the new resulting array, build your table.
$resultsArray = array();
while ($row4 = mysqli_fetch_array($result1)){
$weekDay = $row4['day'];
$resultsArray[$weekDay][] = $row4['shift'];
}
Now you have an array that contains all the shifts organized by day. Looping through this array will allow you to create your table the way you want.
This is untested so may have some mistakes in it, but here is an example...
for ($x = 0; $x <= 10; $x++) {
echo "<tr>";
echo "<td>".$resultsArray['Sunday'][$x]."</td>";
echo "<td>".$resultsArray['Monday'][$x]."</td>";
echo "<td>".$resultsArray['Tuesday'][$x]."</td>";
echo "<td>".$resultsArray['Wednesday'][$x]."</td>";
echo "<td>".$resultsArray['Thursday'][$x]."</td>";
echo "<td>".$resultsArray['Friday'][$x]."</td>";
echo "<td>".$resultsArray['Saturday'][$x]."</td>";
echo "</tr>";
}
Note : Keep in mind you'll have to change this portion $x <= 10
to fit your actual array size.
Populate dropdown from Another MySQL table - php
This is assuming you have an object $student
which is the row from students
corresponding to the current user.
$dropdown_query = "SELECT * FROM course";
$courses = mysqli_query($con, $dropdown_query);
echo '<select name="course">';
while ($course = mysqli_fetch_array($courses)) {
echo "<option value='{$course['id']}'".($student['course']==$course['id'] ? ' selected="selected"' : '').">{$course['course_name']}</option>";
} // while
echo '</select>';
how to populate data in mysql
Just unset $table
variable before ending the loop
while($row=mysql_fetch_array($order_query))
{
$x++;
$table = $section->addTable('myOwnTableStyle'.$x);
....
unset($table);
}
Related Topics
How Validate Unique Email Out of the User That Is Updating It in Laravel
How to Call a Python Script from PHP
When and Why Should $_Request Be Used Instead of $_Get/$_Post/$_Cookie
Pdo Exception Questions - How to Catch Them
Tablegateway with Multiple from Tables
New MySQLi(): How to Intercept an 'Unable to Connect' Error
Will PHP Script Be Executed After Header Redirect
PHP MySQL - Insert New Record into Table with Auto-Increment on Primary Key
Recursive Function to Get All the Child Categories
Do PHP Pdo Prepared Statements Need to Be Escaped
Syntax Error, Unexpected T_Encapsed_And_Whitespace, Expecting T_String or T_Variable or T_Num_String