display php foreach values in div - php

I have
foreach ($a as $key => $value) {
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
which displays result one below other,
like
1234
5678
2010-05-20
5678
1590
2010-05-19
but i want it in a table like structure like
1234 5678 2010-05-20
5678 1590 2010-05-19
how can i do that?

You could keep track with for example modulus if you should begin a new row or not.

EDITED after reformatting of the question
I think what you need is simply something on the line of:
echo '<table><tr>';
$n = 0;
foreach ($a as $key => $value)
{
if ($n==3)
{
$n = 0;
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$n++;
}
echo '</tr></table>';

You can do like:
$counter = 0;
foreach ($a as $key => $value) {
$counter++;
if (($counter % 3) === 0)
{
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
else
{
echo "<DIV id='container' style='float:left;'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
}

Kind of what nico was saying:
foreach ($a as $key => $value)
{
echo '<div style="width:33%;float:left">'.$value.'</div>';
}
Or what don was saying:
$i = 0;
$open = false;
echo '<table>';
foreach( $a as $key => $value )
{
if( 0 == $i % 3 ) {
echo '<tr>';
$open = true;
}
echo "<td>$value</td>";
if( 2 == $i % 3 ) {
echo '</tr>';
$open = false;
}
$i++;
}
if( $open )
echo '</tr>';
echo '</table>';

Simply use like this,
foreach ($a as $key => $value) {
echo "<DIV id='container' style='display:inline'>
<DIV id='middle' style='display:inline'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
add the break statement depending on your second line.

By default, div's don't float next to each other. You could give all the div's, except every third one float: left in the style. However it's okay to use table's if your displaying a table.
If you can derive from the value of $key wether to start a new row or not you can do something like:
echo '<table><tr>';
foreach ($a as $key => $value) {
if($key == 'somevalue'){
//start a new row if the $key allow
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
}
echo '</tr></table>';
If you can't derive from the value of $key wether to start a new row or not, use a counter, like so:
echo '<table><tr>';
$cnt = 0;
foreach ($a as $key => $value) {
if($cnt % 3 == 0){
//start a new row if this row contians 3 cells
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$cnt++;
}
echo '</tr></table>';

Related

I want to echo something only on the first time foreach runs

I want to echo something only on the first time foreach() runs!
let's say if I have an array $i
$i = array(............);
and I want to use it in foreach like...
foreach($i as $key => $value) {
echo '<h1>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
while this foreach runs the first time only i want to add class="show" to the h1 tag.
How to do that?
Update code:
$i=['hello','world'];
foreach($i as $key => $value) {
$class= ($key == 0)?'show':'';
echo '<h1 class="'.$class.'">'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
Output:
<div>
<h1 class="show">0</h1><p>hello</p>
<h1 class="">1</h1><p>world</p>
</div>
You'll need to add a var to track that
$first = 0;
foreach($i as $key => $value) {
$show=($first == 0) ? 'h1 class=show' : 'h1';
echo '<$show>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
$first++;
}
There are many ways to do it, but this might be easier to read
if you have single dimension array then you can use
$i=['test1','test2','test3'];
foreach($i as $key => $value) {
if($key == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
}
if you have an associative array
$i=array('a'=>'test1','b'=>'test2','c'=>'test3');
$j = 0;
foreach($i as $key => $value) {
if($j == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
$j++;
}
output will be

apply css to specific element in array

I am fetching data of employees from mysql database into array and displaying in table.
everything is working but i want to apply a condition in array that
If salary is greater then 30,000 then change its color to red .
Tried
$q = "select name, salary from array";
$res = mysqli_query($link, $q);
$arr = array();
while ($data = mysqli_fetch_assoc($res)) {
$arr[] = $data;
}
echo '<table class="table table-hover">
<th>Name</th><th>Salary</th>';
$keys = array_keys($arr);
foreach ($keys as $key => $value) {
echo "$value , ";
}
for ($i=0; $i < count($arr); $i++) {
echo '<tr>';
foreach ($arr[$keys[$i]] as $key => $value) {
if ($arr[$keys[$i]['salary']] > 34000) {
echo "<td style='color: red; background-color:pink;'> $key=>$value </td>";
}else{
echo "<td> $key=>$value </td>";
}
}
echo "</tr>";
}
echo '</table>';
this is how my table looks
i want to change color of salary if it is greater than 30,000...
Change
if ($arr[$keys[$i]['salary']] > 34000) {
to
if ($value > 30000) {
I think you just got a little lost when processing an array which contains another arrays. A simple foreach loop is the simplest way of dealing with this.
echo '<table class="table table-hover"><th>Name</th><th>Salary</th>';
foreach ($arr as $row) {
echo '<tr>';
// using a ternary operator here rather than an IF
$style = $row['salary'] > 30000
? ' style="color:red;background-color:pink;"'
: '';
echo "<td>{$row['name']}</td>";
echo "<td $style>{$row['salary']}</td>";
echo '</tr>';
}

Error create multi columns in php not use table

I have a sample list 1,2,3,4,5,6,7,8
I want create 3 colums with format
1 4 7
2 5 8
3 6
This is my code
$columns = 3;
$rows = ceil(count($lists) / $columns);
foreach ($lists as $key => $value) {
if($key % $rows == 0) {
echo '<div class="col-md-4">';
}
echo $value;
if($key % $rows == 0) {
echo '</div>';
}
}
This is result error
<div class="col-md-4">1</div>
2
3
<div class="col-md-4">4</div>
5
6
<div class="col-md-4">7</div>
8
How to fix it for result:
<div class="col-md-4">1 2 3</div>
<div class="col-md-4">4 5 6</div>
<div class="col-md-4">7 8</div>
This solution works for your last edit:
<div class="col-md-4">1 2 3</div>
<div class="col-md-4">4 5 6</div>
<div class="col-md-4">7 8</div>
So, code is:
$lists = array(1,2,3,4,5,6,7,8);
$columns = 3;
$rows = ceil(count($lists) / $columns);
echo '<div class="col-md-4">';
foreach($lists as $key => $value) {
if ($key % $rows == 0 && $key != 0) {
echo '</div><div class="col-md-4">'.$value.' ';
} else {
echo $value.' ';
}
}
echo '</div>';
... because you're only adding the div to every third value. I think this is what you want
$columns = 3;
$rows = ceil(count($lists) / $columns);
foreach ($lists as $key => $value) {
if($key % $rows == 0) {
echo '<div class="row">';
}
echo '<div class="col-md-4">' . $value . '</div>';
if($key % $rows == 0) {
echo '</div>';
}
}
It's a lot simpler to achieve what you're after. Say you have the following list:
$list = '1,2,3,4,5,6,7,8';
All you'd have to do is explode your string (list) into an array and chunk that into groups of 3. Now that simplifies what you are required to do when printing out your rows:
$chunks = array_chunk(explode(',', $list), 3);
foreach($chunks as $row){
echo "<div class='row'>";
foreach($row as $value){
echo "<div class='col-md-4'>{$value}</div>";
}
echo "</div>";
}
Example/Demo
References
array_chunk()
explode()
Assuming you need fixed 3 columns, here the code (you may have to fix the code based on your column requirement)
$columns = 3;
$lists = array(1,2,3,4,5,6,7,8);
$rows = ceil(count($lists) / $columns);
$i = 0;
while($i < $rows)
{
echo "<div class=col-xs-4>".#$lists[$i]."</div>";
echo "<div class=col-xs-4>".#$lists[$i+$rows]."</div>";
echo "<div class=col-xs-4>".#$lists[$i+$rows+$rows]."</div>";
$i++;
}

Limit number of sub_menu items to 4

I am trying to limit the number of sub_menu items (li) to maximum of 4. I'm no php developer but gave it a go with some code as provided below.
This is the existing code, which will just keep displaying it all, no limit set right now.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
foreach ($sub_menu_array as $sub_menu_row) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Here is what I tried but it ended up displaying nothing instead.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Syntax of PHP foreach statement has two different variants
foreach (array_expression as $value){
statement
}
foreach (array_expression as $key => $value)
statement
When you chanched code from
foreach ($sub_menu_array as $sub_menu_row) {
to
foreach ($sub_menu_array as $sub_menu_row => $v) {
You also changed values what appropriated to $sub_menu_row. For example:
$sub_menu_array = array ('a','b');
In the first variant at first iteration
$sub_menu_row=='a'
and in second variant
$sub_menu_row==0 #array's element key
$v=='a' #value
There are two solutions
Simply remove '=> $v'
Change $sub_menu_row to $v inside foreach statement
You have changed foreach, you should now use $v as a value.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($v['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
This should be enough for the menu part:
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
if($i < 4) {
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
$i++;
}
echo '</ul></nav>';
Insert this line before displaying the menu
$sliced_sub_menu_array = array_slice($sub_menu_array, 0, 4);
and then
foreach ($sliced_sub_menu_array as $sub_menu_row => $v) {
// displaying here
}
Use this code .
<?php
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if ($i == 3)
{
break;
}
$i++;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
?>

Looping through an array of arrays, changing output on a given line(s)

This is what im using to loop through an array of arrays.
$csvpre = explode("###", $data);
$i = 0;
$bgc = 0;
foreach ( $csvpre AS $key => $value){
$info = explode("%%", $value);
$i++;
if($i == "1"){
echo "<tr bgcolor=#efefef><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
} else {
if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
echo "<tr $bgcgo><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
$bgc++;
}
}
How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array.
Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>
You mean like this:
<?php
.......
echo "<tr $bgcgo><td></td>";
$j = 0; //you need a counter
foreach ( $info as $key => $value ) {
$j++;
if ($j != 30) {
echo "<td>$value</td>";
} else {
echo "<textarea>$value</textarea>";
}
}
echo "</tr>";

Categories