How to format the php code to display this better - php

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

Related

Create a bidimensional array in php

I confess that I am a beginner. My goal is to be able to summarize in a two-dimensional table the amount per month (in column) for each class (in line). After doing a lot of research, I arrived at this stage in my project.
My problem is , I get the result with months repeating in columns like here.
Here is my code.
This is my sql code : "SELECT classe, mois, sum_ec FROM journal"
This is my php code :
<?php
$tableau = array();
$tblClasse = array();
$rt = mysqli_query($db, $req_sit);//execute la requete
while ($row = $rt->fetch_assoc()){ //forme le tableau
$tableau[$row['classe']][$row['mois']] = $row['sum_ec'];
if (!in_array($row['classe'],$tblClasse)) {
$tblClasse[] = $row['mois'];
}
}
echo '<table border="1">
<tr>
<th> </th>';
foreach ($tblClasse as $classe) {
echo '<th>' . htmlspecialchars($classe) . '</th>';
}
echo '</tr>';
foreach ($tableau as $mois=>$value) {
echo '<tr>';
$new_line = TRUE;
foreach ($tblClasse as $classe) {
if ($new_line) {
echo '<td>' . $mois . '</td>';
$new_line = FALSE;
}
$display = isset($value[$classe]) ? $value[$classe] : " ";
echo '<td>' . $display . '</td>';
}
echo '</tr>';
}
?>
Can someone tell me what mistake I made?
The problem is in this part of your code:
if (!in_array($row['classe'],$tblClasse)) {
$tblClasse[] = $row['mois'];
}
You are adding months (Fev, Mar) to the array and trying to check if it contains classes (PS, MS etc.) which is impossible, and this condition always returns true, so Feb is added to the $tblClasse array at each iteration. You must either check months or classes and add months or classes to the array respectively. And if you want the array to contain unique values (for example, months), use keys instead of values:
if (!array_key_exists ($row['mois'],$tblClasse)) {
$tblClasse[$row['mois']] = 1;
}
Where 1 can be replaced with any other value.
In the further part of your code use the keys, not values of $tblClasse array to form the table heading.
foreach (array_keys ($tblClasse) as $mois) {
echo '<th>' . htmlspecialchars($mois) . '</th>';
}
Eureka! It worked. I just had to change the line for the values ​​to "array_keys"
foreach (array_keys($tblClasse) as $classe) {
if ($new_line) {
echo '<td>' . $mois . '</td>';
$new_line = FALSE;
}
as well and boom! It's perfect. Thank you very much my dear. You have given me a lot of happiness.

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)

How do I display the data from the last to the first with a multidimensional array in php

So I'm new to coding and I was looking at arrays and I want to know if it's possible to display the last piece of data from an array first and the first piece of data to be last. This is the code I have.
<?php
$trans = array
(
array("10/3/22",1,54),
array("10/48/32", 54,54),
array("3/29/2018", 54, 128.84)
);
echo '<table border="1">';
echo '<tr><th>Date</th><th>Before Balance</th><th>After Balance</th></tr>';
foreach ($trans as $tran) {
echo '<tr>';
foreach( $tran as $data ) {
echo '<td>' . $data . '</td>';
}
echo '</tr>';
}
echo '</table>';
Use
array_reverse($array);
For more info http://php.net/manual/en/function.array-reverse.php
use array_reverse as follows in your forloop
foreach (array_reverse($trans) as $tran) {
echo '<tr>';
foreach( $tran as $data ) {
echo '<td>' . $data . '</td>';
}
echo '</tr>';
}

New line after multiple columns

Hi all i'm not very experienced with programming so this is probably easy to achieve.
I am pulling data from a mysql table using php i want to display the output like so:
$row[1] $row[2] Line break
$row[3] $row[4] Line break
$row[5] $row[6] Line break
And so on
Any help would be appreciated thanks
$user_table = select_table( "users", "user_name ASC" );
$rows = array( );
while( $row = mysqli_fetch_assoc( $user_table ) ) {
$rows[] = $row;
}
$user_groups = array_chunk( $rows, 2 );
foreach ( $user_groups as $user_group ) {
echo "<tr>";
foreach( $user_group as $row ) {
echo "<td class=\"user-box\">{$row["user_name"]}</td>";
}
echo "</tr>";
}
This is my working code. Thanks Tim
Loop through your results and start counting again after 2.
here's an example using a simple array and a table so you can see how it works
<?php
$results = array('one','two','three','four');
echo '<table border="1">';
foreach(array_chunk($results,2) as $row) {
echo '<tr>';
foreach($row as $value) {
echo '<td>'.$value.'</td>';
}
echo '</tr>';
}
echo '</table>';

table headers not aligning properly

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>';
}

Categories