New line after multiple columns - php

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

Related

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

Display sql tables in separate html table columns

I'm trying to display data from different mysql tables in an html table. The problem I have is that it currently displays both in the same column. I'd like to have two separate columns in the table one for each of the sql data.
This is my code:
global $wpdb;
$centr = $wpdb->get_results('SELECT meta_value FROM `wp__frm_item_metas` WHERE `item_id` = 2');
$namelist = $wpdb->get_results('SELECT name FROM `wp__frm_fields` WHERE form_id = 2');
echo "<table>";
if ( !empty( $centr ) ) {
foreach ($namelist as $key) {
# code...
echo "<tr><td>" . $key->name . "</tr></td>";
}
foreach ( $centr as $r ) {
echo"<tr><td>" . $r->meta_value . "</tr></td>";
}
}
echo "</table>";
I'd like the second echo to start in a new column, instead of displaying more rows in the same one.
I guess this is what you want
echo "<table>";
if ( !empty( $centr ) ) {
echo "<tr><td>";
foreach ($namelist as $key) {
echo "<table><tr><td>" . $key->name . "</td></tr></table>";
}
echo "</td><td>";
foreach ( $centr as $r ) {
echo"<table><tr><td>" . $r->meta_value . "</td></tr></table>";
}
echo "</td></tr>";
}
echo "</table>";

mysqli_fetch_array using a foreach loop

Ive got a pretty basic table named 'customers' with four columns:
ID (primary Auto Increment)
businessName
contactName
contactEmail
I call it with:
$result = mysqli_query($con, "SELECT * FROM customers");
and was using mysqli_fetch_array to display it on the page with a foreach loop:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
Which gives you an array like:
Array ( [id] => 1 [businessName] => Microsoft [contactName] => Bill Gates [contactEmail] => bill#microsoft.com ) Array ( [id] => 2 [businessName] => Amazon [contactName] => Jeff Bezos [contactEmail] => jeff#amazon.com )
Is it possible to display results differently based on which column (technically now position in the array) they are in? I would like to make a
a href="mailto:bill#microsoft.com"
link when it gets to contactEmail.
What if I wanted to display just one key or value instead of using foreach?
It doesn't seem possible to call
$row['contactEmail']
in the foreach loop
which confuses me since below that I am able to create a link to
$row['id']
If anyone has any ideas how to do this, or if there is a better way to be displaying this information, I'm open to suggestions, as I'm not very good at programming, especially PHP.
Yes! you can just add like this:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
foreach ($row as $key => $value) {
if($key == "contactEmail"){
$mailUrl = "mailto:".$value;
echo "<td>".$value."";
}
else{
echo "<td>" . $value . "</td>";
}
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
Yes, You can able to mysqli_fetch_array retrieve data directly using foreach loop. like bellow :
<?php
$result = mysqli_query($con, "SELECT * FROM customers");
?>
<table>
<tr>
<td>Business Name</td>
<td>Contact Name</td>
<td>#</td>
</tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>".$row['businessName']."</td>";
foreach ($row as $key => $value) {
$newValue = $key == "contactEmail" ? ''.$value.'' : $value;
echo "<td>" . $newValue . "</td>";
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
?>
</table>

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

Categories