How to display two table columns per row in php loop
$i=0;
foreach ($x as $key=>$value)
{
if (fmod($i,2)) echo '<tr>';
echo '<td>',$value,'</td>';
if (fmod($i,2)) echo '</tr>';
$i++;
}
this will output TR (row) each second time
ps: i haven't tested the code, so maybe you will need to add ! sign before fmod, if it doesn't output TR on first iteration, but on second iteration in the beginning...
How to display two table columns per row in Codeigniter?
I think you are having one dimensional array and want to show two records in each row of table.
You can chunk your array with `array_chunk' and iterate on resulted array,
You can try something like:
$x = array_chunk($x, 2);
foreach ($x as $value) {
echo '<tr>';
echo '<td>'.$value[0]->image.'</td>';
echo '<td>'.$value[1]->image.'</td>';
echo '</tr>';
}
PHP foreach loop in HTML table 2 per row
echo '<table><tr>';
$i=0;
foreach ($users as $key => $name){
if($i%2==0 && $i!=0)
{
echo '</tr><tr><td width="50%" align="center">'.$name.'</td>';
}
else
echo '<td width="50%" align="center">'.$name.'</td>';
$i++;
}
echo '</tr></table>';
Try this
How to display two table columns per row in Angular 2 loop (Angular2 equivalent of PHP's array_chunk)
https://plnkr.co/edit/umL80bh0WKr8aPEueCxZ?p=preview
You can create pipe to get pairs values:
@Pipe({ name: 'pairs' })
export class PairsPipe implements PipeTransform {
transform(array) {
return array.reduce((result, item, index) => (
index % 2 ? result : [...result, [item, array[index + 1]]]
), []);
}
}
add pipe to Module:
@NgModule({
imports: [ BrowserModule ],
declarations: [ App , PairsPipe],
bootstrap: [ App ]
})
and use in *ngFor:
<tr *ngFor="let item of data | pairs">
Split 1 Loop Into 2 Column in PHP
Use modulo to conditionally split your elements into two different groups:
<?php
for ($i = 0; $i < 10; $i++) {
if ($i % 2 == 0) {
echo "<div class='left'>$i</div>";
} else {
echo "<div class='right'>$i</div>";
}
}
?>
And then use CSS to float
the columns next to each other:
.left {
float: left;
}
.right {
float: right;
}
.left, .right {
width: 50%;
}
How to restrict number of cells per row in a html table from database with PHP
As, you want to display item with images. Try CSS Grid to display it. That way you will have more control over its layout regarding different screen sizes.
Here I am using, bootstrap grids, you can use different if you want.
$sql = $db->query('SELECT * FROM store_items WHERE store_cat = "Apparel"');
if(is_object($sql) && $sql->num_rows > 0)
{
echo '<div class="row">'; //Bootstrap Row
while($row = $result->fetch_array())
{
echo "<div class= 'col-md-4'>"; //Bootstrap Column
echo '<img src="" alt="" height="180" /><br />'.$row['store_name'];
echo '<br /><br />\$'.$row['store_price'].'<br />'.$row['store_desc'];
echo '<br /><href="">Add to Cart</a>';
echo '</div>';
}
echo '</div>';
}
In the code above, we are dividing the available width in three equal parts.
It's just styling issue your code is fine. If you just want to achieve this, avoid unnecessary codes or logics that will only slow down performance.
This way you will have more option, you can style images and tags below it. There is other bootstrap classes for image thumbnails.
How can i get two items in a foreach loop in PHP?
You can use array_chunk to split array to group 2 item, then use foreach again
foreach (array_chunk($input_array, 2) as $group) {
// Start Group
foreach ($group as $item) {
// Item
}
// End group
}
Update with HTML
<div class="owl-carousel">
<?php foreach (array_chunk($input_array, 2) as $group) : ?>
<div class="owl-item">
<?php foreach ($group as $item) : ?>
<div class="item">
<!-- Code of item -->
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
Related Topics
How to Get User Image with Twitter API 1.1
What Does [\S\S]* Mean in Regex in PHP
How to Redirect a 404 Error in a Custom 404 Page Using Codeigniter
How to Capture Search Term from Google Search
Efficient Way to Pass Variables from PHP to JavaScript
PHP Put a Space in Front of Capitals in a String (Regex)
How to Decode Numeric HTML Entities in PHP
How to Find Error Log Files for PHP
Multidimensional Array to String
Search and Replace Value in PHP Array
How to Get All Checkbox Variables Even If Not Checked from HTML to PHP
PHP Array Printing Using a Loop
Sending Data Using Post in Python to PHP
Accented Characters in MySQL Table