For my work i build me a datatable with data from my database. I used SQL PHP and HTML.
I worked very long on it, because I did not know very much about this before.
NOw I need to doucument my work.
Would be very happy if someone could explain me the importance of mysqli_num_rows(...) and mysqli_fetch_assoc (...) in this case.
Part 1:
Code:
<?php
$sql_table_main = "SELECT id,name_Gleitzeitrahmen, name_Abweichungen, name_Mehrarbeitervolumen, name_Mehrarbeit1, name_Ausgleich, name_Mehrarbeit2, name_Personalmassnahmen, name_Ueberstunden, name_Ueberstunden_abzusehen, name_Klaerungsbedarfe1, name_Klaerungsbedarfe2 FROM landrat_dashboard";
$result_table_main = mysqli_query($con, $sql_table_main);
?>
Part 2:
<div class="card-content table-responsive table-maxheight" style="overflow:scroll;">
<table class="table table-hover table-mc-green table-bordered table-striped table-condensed">
<thead class="text-primary">
<th class="thbackground_green">ID</th>
<th class="thbackground_green">Gleitzeitrahmen</th>
<th class="thwidth thbackground_green">Abweichungen</th>
<th class="thwidth thbackground_green">Mehrarbeitervolumen</th>
<th class="thwidth thbackground_green">Mehrarbeit</th>
<th class="thwidth thbackground_green">Ausgleich</th>
<th class="thwidth thbackground_green">Mehrarbeit</th>
<th class="thwidth thbackground_green">Personalmaßnahmen</th>
<th class="thwidth thbackground_green">Überstunden im Rahmen?</th>
<th class="thwidth thbackground_green">Sind Überstunden abzusehen?</th>
<th class="thbackground_green">Klärungsbedarfe</th>
<th class="thwidth thbackground_green">Klärungsbedarfe Beschreibung</th>
</thead>
<tbody>
<?php
if(mysqli_num_rows($result_table_main) > 0){
while ($row = mysqli_fetch_assoc($result_table_main)) {
echo '<tr>';
echo '<td>'. $row['id'] .'</td>';
echo '<td>'. $row['name_Gleitzeitrahmen'] .'</td>';
echo '<td>'. $row['name_Abweichungen'] .'</td>';
echo '<td>'. $row['name_Mehrarbeitervolumen'] .'</td>';
echo '<td>'. $row['name_Mehrarbeit1'] .'</td>';
echo '<td>'. $row['name_Ausgleich'] .'</td>';
echo '<td>'. $row['name_Mehrarbeit2'] .'</td>';
echo '<td>'. $row['name_Personalmassnahmen'] .'</td>';
echo '<td>'. $row['name_Ueberstunden'] .'</td>';
echo '<td>'. $row['name_Ueberstunden_abzusehen'] .'</td>';
echo '<td>'. $row['name_Klaerungsbedarfe1'] .'</td>';
echo '<td>'. $row['name_Klaerungsbedarfe2'] .'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
in a nutshell!
mysqli_num_rows ==> Returns number of rows in the result set.
for more take a look of http://php.net/manual/en/mysqli-result.num-rows.php
mysqli_fetch_assoc ==> Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.
for more take a look of http://php.net/manual/en/mysqli-result.fetch-assoc.php
Feel Free to ask anything in comments
mysqli_num_rows- Returns the number of rows in the result set.
The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved.
The mysqli_fetch_assoc() function fetches a result row as an associative array.
Related
I am working on posting a CRUD Application to where the user logs in first. The application is made with 2 MySQL tables; one for logging in and the other for the CRUD values. This example hasn’t worked on my local environment using both XAMP/PC and MAMP/MAC.
When I uploaded it to my host it does work, however the CRUD values don’t populate when it the for each loop is specified. I concatenated the mysql_error(); to explain why this is not working on my host.
On line 34 in logincrud/main.php:
The code from the table in main.php:
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Number</th>
<th>Explanation</th>
<th>Date Accured</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM saftey ORDER BY id DESC';
foreach (($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['ins_n'] . '</td>';
echo '<td>'. $row['explanation'] . '</td>';
echo '<td>'. $row['date'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
The Add, Update, and Delete works on the host as I can see the records from the safety table on myphpadmin. The login/logoff is from a completely different table. Again, the local environment works with no issues.
I opened a help ticket with my host provider, but all I received is they do not troubleshoot Applications.
I have been looking for solutions for a few weeks and all I couldn’t find a solution. In the Chrome browser the console states the error: hp:1 Failed to load resource: the server responded with a status of 500 ()" Server issue? Aware of SQL Injection problems.
I uploaded a GITHUB project if it helps.
Thank you in advance.
I solved the problem why it was not working on my live site. Looking at this Stackoverflow article
The way the PHP Data Object has the forward arrow executes the code. The way this foreach works caused the value return false; in other words not in an array.
I reorganized the PHP Data Object into a variable known as $records.
foreach (($sql) as $row) {
to
$records = $pdo->query($sql);
foreach ($records as $row) {
changed the echo to print
}
Works now. Weird how echo is different than print in this situation.
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Number</th>
<th>Explanation</th>
<th>Date Accured</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM safety ORDER BY id DESC';
$records = $pdo->query($sql);
foreach ($records as $row) {
print '<tr>';
print '<td>'. $row['ins_n'] . '</td>';
print '<td>'. $row['explanation'] . '</td>';
print '<td>'. $row['date'] . '</td>';
print '<td width=250>';
print '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
print ' ';
print '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
print ' ';
print '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
print '</td>';
print '</tr>';
}
Database::disconnect();
I have look all day the whole internet and didn't find fetching data in tabular format. I have my short code and everything is ready, I have this code which is display data but not in good format as you can see in the screen shot I want data like this in WordPress page.
<?php
?>
<table border="1">
<tr>
<th>Char Item</th>
<th>Item Id</th>
<th>Item Description</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_orderlist" );
foreach ( $result as $print ) {
echo '<td>'. $print->char_item.'</td>';
echo '<td>'. $print->item_id.'</td>';
echo '<td>'. $print->Item_Description.'</td>';
}
?>
</table>
You forget to add tr tag. Try this:
echo '<tr>';
foreach ( $result as $print ) {
echo '<td>'. $print->char_item.'</td>';
echo '<td>'. $print->item_id.'</td>';
echo '<td>'. $print->Item_Description.'</td>';
}
echo '</tr>';
I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.
I'm completely new to PHP, so any hints on how to solve this will be much appreciated!
echo '<table>';
echo '<tr>';
echo '<th>';
echo '<td>Word</td>';
echo '<td>Frequency</td>';
echo '</th>';
echo '</tr>';
$response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");
while ($row = $response->fetch())
{
echo '<tr>';
echo '<td>'.$row['word'].'</td>';
echo '<td>'.$row['freq'].'</td>';
echo '</tr>';
}
echo '</table>';
$response->closeCursor();
A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.
echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
I prefer combining php and html
<table >
<thead>
<tr>
<th >Word</th>
<th >Frequency</th>
</tr>
</thead>
<?php
$response = $db->query("SELECT * FROM frequencies WHERE freq
BETWEEN 900 AND 910 ORDER BY freq");
?>
<tbody>
<?php
while ( $row = $response->fetch()) {
?>
<tr>
<td><?php echo $row['word']; ?></td>
<td><?php echo $row['freq']; ?></td>
</tr>
<?php }
$response->closeCursor();
?>
</tbody>
</table>
I have some code that runs reports from $results returning from mysql query.
The queries are relatively standard, but their total number is dynamic. So far I have static HTML where it has the same code repeated, but thats only if ok the number of queries == the number of markup repetitions.
So, I am trying to implement a new foreach loop that will determine how many "queries" there are, and only markup for each one, and then $total at the end.
To date I have had static php foreach loop like this, works well.
<div class="row">
<div class="col-xs-12">
<table class="table table-striped">
<thead>
<tr>
<th>Year</th>
<th>Month</th>
<th>Item</th>
<th class="text-right">Minimum</th>
<th class="text-right">Maximum</th>
<th class="text-right">Change</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $result) :
if ($result->id == 151){
echo '<tr>';
echo '<td>', $result->Year, '</td>';
echo '<td>', $result->Month, '</td>';
echo '<td class="text-centre">', $result->name, '</td>';
echo '<td class="text-right">', $result->min, '</td>';
echo '<td class="text-right">', $result->max, '</td>';
echo '<td class="text-right">', $result->cumulative, '</td>';
$thirdtotal += $result->change;
$total += $result->change;
echo '</tr>';
}
endforeach; ?>
Where I am stuck is, do i put the above inside another for each loop? My thought is, if a query exists, it will have an "id" field in the array, so for the number of "id", thats how many outputs you'll get.
<?php foreach ($results as $key => $value;) :
{
echo '<tr>';
echo '<td>' $value, '</td>';
echo '</tr>';
}
endforeach; ?>
This piece of test code doesnt work, am I using the key value correctly? Is it the correct approach to solve this problem and reduce the amount of php in the controller?
Thanks
I have this code that works fine in other pages that I made but doesn't work properly on my summary page.
<?php
//AAFES-date1
$sqlAAFES1 = "SELECT * FROM aafes WHERE dueDate ='$date1'";
$qAAFES1 = $pdo->prepare($sqlAAFES1);
$qAAFES1->execute(array($date1));
$dataAAFES1 = $qAAFES1->fetch(PDO::FETCH_ASSOC);
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
echo '<tr>';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
echo '</table>';
};
};
?>
as you can see the 2nd row doesn't align with the first row.
while on the other page, I used the same foreach code, but alignment is perfect. So I wanna ask what seems to be the problem with this one.
Don't close foreach loop after table. Put <tr></tr> inside foreach loop.
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<tr>';
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
};
echo '</table>';
};