I'm trying to match results from this PHP code into this Bootstrap HTML structure.
What I'm able to achieve so far is printing items from database in 3-column layout, but I don't known how to combine this results with/ info my bootstrap html "structure/ table". I think I need to work with "id" and increment it after every "echo" (h4)? But I'm very new to PHP. Thank you for any practical advices.
PHP CODE:
// Run query on connection
$query = "SELECT
product.id,
product.name as product_name,
product.price,
product.type
FROM pizza_project.product
INNER JOIN pizza_project.product_type on product.type = product_type.id
where product.type in (1,2)
order by product.type ASC, product.price ASC";
$result = $con->query($query);
?>
<table>
<tr>
<?php
$split = 0;
$id = $row['id']; // just an idea I think I need to start with
if (mysqli_num_rows($result) > 0) {
foreach ($result as $row) {
echo '<td>' . $row['product_name'] . ' ' . $row['id'] . '</td>';
$split++;
if ($split%3 == 0) {
echo '</tr> </tr>';
}
}
}
?>
</tr>
</table>
<?php
$itemsInRow = 0;
foreach ($result as $row) {
if ($itemsInRow === 0)
echo "<div class=\"row text-center\">";
echo "<div class=\"col-md-4\">";
echo "<span class=\"fa-stack fa-4x\">";
echo "<img class=\"product-image\" src=\"" . $row['img'] . "\">";
echo "</span>";
echo "<h4 class=\"service-heading\">" . $row['headline'] . "</h4>";
echo "<p class=\"text-muted\">" . $row['text'] . "</p>";
echo "</div>";
if ($itemsInRow === 0)
echo "</div>";
if ($itemsInRow % 3 === 0) {
$itemsInRow = 0;
continue;
}
$itemsInRow++;
}
Is this what you are trying to achieve? I will improve my answers if its the right way. This is not tested.
Related
I am trying to pass a variable from a page to another page so that i can display more information on the other page by getting the idea. I am not really sure what is going wrong and because I use bluemix it doesn`t display an error it just gives me the page 500 error. Here is my code:
<?php
$strsql = "select id, name, problem, urgency, technology from idea WHERE status='saved'";
if ($result = $mysqli->query($strsql)) {
// printf("<br>Select returned %d rows.\n", $result->num_rows);
} else {
//Could be many reasons, but most likely the table isn't created yet. init.php will create the table.
echo "<b>Can't query the database, did you <a href = init.php>Create the table</a> yet?</b>";
}
?>
<?php
echo "<tr>\n";
while ($property = mysqli_fetch_field($result)) {
echo '<th>' . $property->name . "</th>\n"; //the headings
}
echo "</tr>\n";
while ( $row = mysqli_fetch_row ( $result ) ) {
$idea_id= $row['id'];
echo "<tr>\n";
for($i = 0; $i < mysqli_num_fields ( $result ); $i ++) {
echo '<td>' . "$row[$i]" . '</td>';
}
echo "</tr>\n";
}
$result->close();
mysqli_close();
?>
There's a small bug in your code. Your echo <a href ="... line should be like this,
echo '<td>' . $row[$i] . '</td>';
I am trying to display status of employee for each date in a month in the given application.
i have divided data in two tables.
Now, if you can see , My tables are displaying dates correctly.
But i want to start new table for each month. i.e. whenever 1st date of new month begins, i want to start the procedure of rendering dates for the month again.
So i will be able to distinguish them by their months.
I hope i have explained the problem correctly.
Please provide guidance on this .
Thanks in advance.
My code for the given problem :
<?php
$name=$_SESSION['sess_username'];
$dateofattendance=$_SESSION['sess_date'];
$time="00-00-00";
$status="absent";
$counter=0;
$conn = new mysqli('localhost', '', '', 'test');
$sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table class='main'><tbody><tr><td >";
// create the opening table
echo "<div align='left'><table class='sep1'style='float:left;'; border='black' cellpadding='5' ><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead>tbody>";
while($row = $result->fetch_assoc())
{
// create the row
echo "<tr><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td>";
if($row["status"]=='present')
{
echo "<td ><span class='label label-success'>". $row["status"]."</span></td>";
}
else
{
echo "<td><span class='label label-danger'>". $row["status"]."</span></td>";
}"
</tr>";
$counter++;
// when the counter is 15, close this table and open another
if($counter == 15)
{
echo "</td><td>"; // move to the next cell in our wrap table
echo "</tbody></table></div> ";
echo "<table class='sep2'style='float:left;'border='black'cellpadding='5'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";
}
}
// close the last table
echo "</tbody></table>";
// close our wrapper table
echo "</td></tr></tbody></table>";
}
$conn->close();
?>
Instead of having a counter and creating a new table when it hits 15, you want to have a variable that stores the month number of the last row rendered, and check if the month number of the next row to be rendered matches. If it doesn't match, generate a new table.
The following code is produces the results you want. I've tested it with a database table with identical structure (table named "test_employees").
Note, the code outputs valid HTML and I re-formatted the echo statements and added newline chars to make the both the PHP and HTML source code more readable.
This code could be cleaned up considerably, so it's by no means good production code, but again, it demonstrates the logic you are after.
One item to note, the code only works within a given year. Additional code would be required if you wanted to display tables across multiple years.
If this helps you, please mark my answer.
I can also spend more time to refactor this code if you want. Let me know and I'll reply faster the next time.
<!DOCTYPE html>
<html>
<head>
<title>table test</title>
</head>
<body>
<?php
$name=$_SESSION['sess_username'];
$dateofattendance=$_SESSION['sess_date'];
$time="00-00-00";
$status="absent";
$counter=0;
$conn = new mysqli('localhost', '', '', 'test_employees');
$sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>IN</th>";
echo "<th>OUT</th>";
echo "<th>Status</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$first_iteration = 1;
$row = $result->fetch_array(MYSQLI_ASSOC);
$last_month_num = split('-', $row["dateofattendance"])[1];
while($row = $result->fetch_assoc())
{
$current_month_num = split('-', $row["dateofattendance"])[1];
if ($first_iteration == 1) { // do nothing the first time around
$first_iteration = 0;
}
else if ($current_month_num == $last_month_num) { // only close row in current table
echo "</tr>";
}
else { // close table and start new table
echo "</tr>";
echo "</table>";
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>IN</th>";
echo "<th>OUT</th>";
echo "<th>Status</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
}
echo "<tr>" . "\n";
echo "<td>" . "\n" . $row["name"] . $row["dateofattendance"] . "\n" . "</td>" . "\n";
echo "<td>" . "\n" . $row["timeofattendance"] . "\n" . "</td>" . "\n";
echo "<td>" . "\n" . $row["timeofdeparture"] . "\n" . "</td>" . "\n";
if($row["status"]=='present')
{
echo "<td><span class='label label-success'>". $row["status"]."</span></td>";
}
else
{
echo "<td><span class='label label-danger'>". $row["status"]."</span></td>";
}
$counter++;
$last_month_num = split('-', $row["dateofattendance"])[1];
}
echo "</tr>";
echo "</table>";
}
$conn->close();
?>
</body>
</html>
I am trying to put extracted mysql data into a table using php. The issue is that I am able to put the fetched data into either all rows or all columns. What I would like to do is put the fetched data into a column format and as soon as 4 columns are used, the next data should start on the next row and so on.
Currently I am using the logic below, however I am getting everything in a column.
The goal is to limit the data to a page size so the data can be printed.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
while($row = mysql_fetch_array($result))
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?></div>
If I understand your problem correctly you will want to do something like:
Keep a counter to see which number record you're looking at ($i)
$i = 0; // Your counter
while($row = mysql_fetch_array($result))
{
// ...
On every 4th iteration of the loop output something like </tr><tr>.
if ($i % 4 == 0 && $i > 0) // Will return true if `$i` is a multiple of 4 and greater than 0
{
echo "</tr><tr>"; // Output your HTML to start a new row
}
One final note: As others will be pointing out. You should avoid using the MySQL extension. You should use MySQLi or PDO instead.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
$i = 1;
while($row = mysql_fetch_array($result))
{
if($i == 4)
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
$i=0;
}
$i++;
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?>
</div>
I'm having trouble trying to get a nested < ul > inside of a while loop. I'm not sure if this even possible so i'm open to alternatives.
Here's a quick image of what my database looks like and what i'm trying to achieve.
Here's my sql
SELECT *
FROM drinks_category, drinks_lookup, drinks
WHERE drinks.drink_id = drinks_lookup.drink_id
AND drinks_lookup.drinks_category_id = drinks_category.drinks_category_id
ORDER BY drinks_category.drinks_category_title
Here's my output php
$result = $conn->query($sql) or die(mysqli_error());
$last_category = 0;
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
}
echo "<p>" . $row['drink_name'] . "</p>";
$last_category = $row['drinks_category_id'];
}
Im using mysqli and php. Thanks in advance!
Update your while loop to the following:
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
if($last_category != 0) echo '</ul>';
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
echo "<ul>";
}
echo "<li>" . $row['drink_name'] . "</li>";
$last_category = $row['drinks_category_id'];
}
if($last_category != 0) echo "</ul>";
Instead of having 3 tables, you could just add a category_id to your drinks table, just to simplify things. Then you can do this:
SELECT drink_name, (SELECT drnk_category_title FROM drinks_category WHERE drink_category_id = drink_category // THE COLUMN I SUGGESTED //) AS title FROM drinks
And then you can loop the result and build the nodes you desire really easily
$result = $conn->query($sql) or die(mysqli_error());
$last_category = 0;
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
// close previous </ul>
if( $last_category != 0 ) echo "</ul>";
// new title
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
// new <ul>
echo "<ul>";
$last_category = $row['drinks_category_id'];
}
echo "<li>" . $row['drink_name'] . "</li>";
}
// close last </ul>
if( $last_category != 0 ) echo "</ul>";
EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.