table headers not aligning properly - php

I am outputing 4 columns from a mysql query, but using the code below doesnt align each column wiht the headers, I guess is due the fact this are static declared against the dynamic rows . Can someone advise a way to align the headers properly with each fetched column ,,,
$tableStyle = "padding: 5px;border:1px";
$tdStyle = "padding:5px ";
$thStyle = "padding:5px; align:center ";
echo '<table style="' . $tableStyle . '" cellpadding="7" cellspacing="7">';
echo "<tr> <th>Quiz Title </th><th> Score </th><th>Maximum Score </th><th>Finished On </th></tr>";
$row = $database->loadRowList();
foreach($row as $valuearray)
{
echo '<tr style=" align="center">';
foreach($valuearray as $field)
{
echo "<td>$field</td>";
}
echo "</tr>";
}
echo "</table>";

This line is wrong:
echo '<tr style=" align="center">';
I think you want:
echo '<tr style="text-align:center;">';

Are you using Joomla? The loadRowList() suggests this. If you are, then use loadAssocList() instead, which returns the field names as well as the field values. You can do then a separate loop to output your column headers and guarantee they match up with the data fields.
docs for the function here: http://help.joomla.org/content/view/509/60/
you'd do something like:
$rows = $database->loadAssocList();
echo '<tr>';
foreach(array_keys($rows[0]) as $header) {
echo "<th>$header</th>";
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach($row as $value) {
echo "<td>$value</td>";
}
echo '</tr>';
}

Related

Change table row styleclass under certain mysql conditions

I have the following table created with a foreach loop.
**foreach($data2['wow_accounts']['0']['characters'] as $key => $item) {
echo '<tr>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $item['realm']['name'];
echo '</td>';
echo '<td>';
echo '<button class="btnSelect">Select</button>';
echo '</td>';
echo '</tr>';
}
echo '</table>';**
I want that the script checks inside of the table "y4qt2_jsn_users" of the mysql database and under a certain id number, if the "params" entry is "ja". If this is true then this certain table row should get another tr class.
My idea is something like that, but how can i combine this with my foreach table loop?
$result = mysqli_query("SELECT params FROM xxx_users WHERE params = 'ja' AND id= '$id'");
if(mysqli_num_rows($result) == 0) {
// row not found, just loop without a certain <tr class=""
} else {
// row found, give this row a <tr class=""
}
Here is a screenshot of what I want. If the mysql condition "params=ja" is true the whole row should get a new tr styleclass, not just a cell.
Screenshot
Screenshot-Database
Assuming I understand correctly, this should work. Please sanitize database queries (especially if there is user input at any point). But as mentioned in the comments, I don't know where $id is set, and actually presume it's a value in the $item array, so maybe replaced with $item['Id']
foreach($data2['wow_accounts']['0']['characters'] as $key => $item) {
$result = mysql_query("SELECT params FROM y4qt2_jsn_users WHERE params = 'ja' AND
id= '$id'");
if(mysql_num_rows($result) == 0) {
$class_string = '';
} else {
$class_string = ' class="my-additional-class"';
}
echo '<tr'.$class_string.'>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $item['realm']['name'];
echo '</td>';
echo '<td>';
echo '<button class="btnSelect">Select</button>';
echo '</td>';
echo '</tr>';
}
There are a lot of ways to clean this up, and if you wanted fewer lines of code you can replace the entire if else block and the $class_string variable by using ternary operators inline. The goal was to make it easy for you to read and simple to understand. (I also prefer very verbose code myself)

DataTables SQL Server UI Output

I have the following PHP code that connects to MS SQL SERVER and shows the data.
Can you show me by a simple example on how to convert the output to DataTables?
<?php
include("conn.php");
$sql = "SELECT * from contact";
$result = sqlsrv_query($conn, $sql);
while($value=sqlsrv_fetch_array($result))
{
echo "$value[ID]", " ... $value[Name]", ", $value[Address]", "<br>";
}
?>
All you need to do is build an HTML table from your array result (this assumes an associative array):
<?php
echo '<table id="MainTable">';
echo '<thead>';
echo '<tr>';
foreach (array_keys($results[0]) as $key) {
echo "<th>$key</th>";
}
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($result as $row) {
echo '<tr>';
foreach (array_keys($row) as $key) {
echo '<td>$row[$key]</td>';
}
echo '</tr>';
}
echo '</tbody>';
?>
And then activate DataTable on it with javascript either linked in a separate file or in the <head> element:
$(function () {
$('#MainTable').DataTable();
}
See DataTables Documentation

How to format the php code to display this better

I have this code:
$sql = 'SELECT * from page';
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($result)) {
echo '<table><tr>';
foreach ($rows[0] as $columnName => $value) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '<tr>';
}
echo '</table>';
}
This code is working fine. But since my table is huge, it is appearing to be very very clumsy - almost unreadable. And I don't know how to make it appear better. I tried adding spaces and tabs but to no use. I can't understand how to do it. I got this code from my friend. Can anyone modify the code so as to add at least a tab space between every column. Help would be really appreciated.
You can for example do the following:
instead of
echo '<table><tr>';
use
echo '<table border="1"><tr>';
It will put border on your table and it will be easier to differentiate between cells

While loop inside for loop not working while fetching mysql rows

<?php
echo '<table>';
for($i=1;$i<=12;$i++)
{
echo '<tr>';
while($row5=mysql_fetch_array($result5))
{
if($row5[3]=='monthly')
echo '<td>'.$row5[3].'</td>';
else if($row5[3]=='quarterly')
echo '<td rowspan="3">'.$row5[3].'</td>';
else if($row5[3]=='halfyearly')
echo '<td rowspan=""="6">'.$row5[3].'</td>';
else
echo '<td rowspan="12">'.$row5[3].'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
This code is printing only one row instead of 12 rows. Please help me. I am doing this for managing student fees. I am stuck at the logic.
Create an array with sql result before :
$data = array();
while( $row5 = mysql_fetch_array($result5) )
$data[] = $row5;
Then replace this : while($row5=mysql_fetch_array($result5))
foreach ( $data as $row5 ) {
if($row5[3]=='monthly')
echo '<td>'.$row5[3].'</td>';
// ...
}
PS : Use mysqli_* instead of mysql_* which is deprecated

php mysql query to fetch field names and its data into a table in one run

I have tried to prepare a data table where any list query is passed its results would be sown in a table.
I have used 2 if conditions and foreach loop. Now i have doubt that this is would slow down if bulk records are fetched.. Can anyone suggest me much better way to do this..
Query as follows
echo "<table width='100%' align='center' border=1 style='text-align:center; vertical-align:center; border-collapse:collapse; font-size:80%' class='main'>";
$i=0;
while($row=mysqli_fetch_array($result)){
echo "<tr>";
foreach($row as $rowvalues => $cellvalues){
if (!is_numeric($rowvalues)){
if($i==0){
echo "<th>".$rowvalues."</th>"; // For Table header
}else{
echo "<td>".$cellvalues."</td>"; // For field values
}
}
}
echo "</tr>";
$i++;
}
echo "</table>";
maybe this is a bit faster? (reduce the amount of echos)
$output = "<table width='100%' align='center' border=1 style='text-align:center; vertical-align:center; border-collapse:collapse; font-size:80%' class='main'>";
$i=0;
while($row=mysqli_fetch_array($result)){
$output .= "<tr>";
foreach($row as $rowvalues => $cellvalues){
if (!is_numeric($rowvalues)){
$output .= ($i == 0) ? "<th>".$rowvalues."</th>" : "<td>".$cellvalues."</td>";
}
}
$output .= "</tr>";
$i++;
}
echo $output."</table>";
Create a php pagination to avoid slowing down of fetching records in your database.

Categories