for loop not suitable? [duplicate] - php

This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 6 months ago.
I have created a for loop.
I am trying to loop through lots of members and create a row for each member in a table.
It's looping through too many times.
Is it the right type of loop to use?
<?php
for ($i = 1; $i = count($u); $i++)
{
?>
<tr>
<?php
echo "<td>$i</td>";
?>
<?php
foreach ($u as $username)
{
echo "<td>$username</td>";
}
?>
<?php
foreach ($p as $points)
{
echo "<td>$points</td>";
}
?>
</tr>
<?
}
?>
$u and $p are arrays.
Thanks

You can shorten it a bit and remove the inner loops:
<php
for ($i = 1; $i <= count($u); $i++)
{
echo '<tr>';
echo "<td>$i</td>";
echo "<td>$u[$i]</td>";
echo "<td>$p[$i]</td>";
echo '</tr>';
}
?>

$i = count($u) in the loop is what's causing the problem:
for ($i = 1; $i = count($u); $i++)
On every iteration of the loop, you're assigning count($u) to $i using a single =. Should be
for ($i = 1; $i <= count($u); $i++)
or
for ($i = 0; $i < count($u); $i++)

<?php
$count = count($u);
for ($i = 1; $i < $count; $i++) {
echo '<tr>';
echo '<td>' . $i . '</td>';
foreach ($u as $username) {
echo '<td>' . $username . '</td>';
}
foreach ($p as $points) {
echo '<td>' . $points . '</td>';
}
echo '</tr>';
}
?>
You should count $u before you loop it.

Related

Looping through an array with line breaks every 6th element as a table?

I am trying to print an array so every 6th element is on a new line. I'm trying to do this in a table. I would prefer if it could be answered in PHP, but Javascripts Ok.
$array_control = array(
"1","5","6","2","1",
"2","1","6","4","3",
"3","2","5","6","6",
"4","3","1","5","4",
"6","4","2","3","6"
);
$arrayLength = count($array_control);
$i = 0;
while ($i < $arrayLength)
{
if ($i==0) {
echo "<tr>";
}
if (is_int($i/5)) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
echo "</tr>";
echo "<tr>";
}else{
echo '<td style="width:16%;">'.$array_control[$i-1].'</td>';
}
$i++;
}
Any help would be great!
The %-operator (modulus) solves this for you:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">';
if( ($i % 6) == 0){
echo $array_control[$i-1] . '</td></tr><tr>';
} else{
echo $array_control[$i-1] . '</td>';
}
}
echo '</tr></table>';
or if you feel sassy:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">' . $array_control[$i-1], ( ($i % 6) == 0 ) ? '</td></tr><tr>' : '</td>';
}
echo '</tr></table>';
A for loop seems better, and maybe the mod operator is more clear.
echo '<tr>';
for ($i < = 0; i < $arrayLength; ++ $i) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
if ($i % 6 == 5) {
echo '</tr>';
echo '<tr>';
}
}
echo '</tr>';
This isn't quite ideal, because you'll get an empty '' at the end, but the browser should hide that. A little more logic can be added if you need to eliminate that too.
Further, with the array structure you're showing, you could eliminate the $arrayLength variable entirely, and instead use:
foreach ($array_control as $i => $val) {
echo '<td style="width:16%;">'.$val.'</td>';

Break PHP into 3 columns and display one by one

I'm trying to break a PHP array into 3 columns (has to be columns, not rows) so it would look something like this:
Item 1 Item 5 Item 9
Item 2 Item 6 Item 10
Item 3 Item 7 Item 11..............
Item 4 Item 8
Update to Meldin answer
$cols = 3; // Number of columns
$len = ceil(count($items) / $cols);
echo '<ul class="floatleft">';
for ($i = 0; $i < count($items); $i++) {
if ($i % $len == 0)
echo '</ul><ul class="floatleft">';
echo "<li>" . $items[$i] . "</li>";
}
echo '</ul>';
Also you can try this using two foreach() loops
$len = ceil(count($items)/3);
$itm = array_chunk($items,$len,true);
foreach($itm as $cols){
echo '<div class="floatleft">';
echo '<ul>';
foreach($cols as $col){
echo '<li>'.$col.'</li>';
}
echo '</ul>';
echo '</div>';
}
Use this
echo '<ul class="floatleft">';
for($i=0;$i<count($items);$i++){
if($i%4==0)
echo '</ul><ul class="floatleft">';
echo "<li>".$items[$i]."</li>";
}
echo '</ul>';
Try this:
$total = 10;
$cols = 3;
$rows = ceil($total/$cols);
echo "<table>";
for($i = 1; $i <= $rows; $i++) {
echo "<tr>";
for($j = 1; $j <= $cols; $j++) {
$value = $i + 4*($j-1);
if ($value <= $total) {
echo "<td>".$value."</td>";
}
}
echo "</tr>";
}
echo "</table>";

display first set of result in row

I want to iterate an array of data using foreach loop.
However, I want the first 4 results to display two items per row.Thereafter I want the rest of the data to display I item per row
i found a possible solution here
$data = range(1, 30);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 2; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 2; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
how the problem with this code is that it displays all the data in rows of two.i however only want the first 4 results to display that way.
You can do something like this:
$data = range(1, 30);
for($count = 0; $count < count($data); ++$count){
if($count < 4){
if($count % 2 == 0){
echo "<tr><td>" . $data[$count] . "</td>";
}else{
echo "<td>" . $data[$count] . "</td></tr>";
}
}else{
echo "<tr><td>" . $data[$count] . "</td></tr>";
}
}
Here's the demo
You're using the already incremented $i variable twice.
$i is never < 2
Add this instead
for($j = 0; $j < 2; $j++) {
echo "\t<td>-</td>\n";
}
Test here
Try with -
$data = range(1, 30);
for($count = 0; $count < count($data); $count++)
{
echo "<tr>";
if($count < 2) {
for($i = 1; $i <= 2; $i++) {
echo "<td>" . $data[$count] . "</td>";
if($i == 1) {
echo "</tr><tr>";
}
}
} else {
echo "<td>" . $data[$count] . "</td>";
}
echo "</tr>";
}
What about something like this?
echo '<table border=1>';
$data = range(1, 30);
for($count = 0; $count < count($data); $count++){
echo "<tr>\n";
if($count < 4) {
echo "\t<td>$data[$count]</td>\n";
$count++;
echo "\t<td>$data[$count]</td>\n";
} else {
echo "\t<td colspan=2>$data[$count]</td>\n";
}
echo "</tr>\n";
}
echo '</table>';

Each 3rd Increment Loop PHP

Hi im looking for a way to breakout of a loop every 3 increments to echo a static string.this script echos a "test" after each increment. i want a test after each 3rd increment. any ideas ?
my loop:
<?php
$i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i+3) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}
?>
try below Code
<?php
for($i=1;$i<=100;$i++)
{
if($i%3==0)
{
echo $i."test"."<br>";
}
}
?>
use % operator in if loop,
i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i%3 == 0) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}

PHP Looping through output incorrectly with while and nested for loops

Attempting to generate table columns and rows based on input. With smaller numbers it appears to work appropriately, for example inputting 2 rows and 3 cols. But when inputting slightly larger numbers like 5 for rows will output an incorrect amount of rows sometimes looping indefinitely.
if(isset($_POST['submit'])) {
$rows = (int)$_POST['row_num'];
$cols = (int)$_POST['col_num'];
$n = 1;
$e = 0;
echo '<table id="">';
while(($e < $rows) && ($e < $cols)) {
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($i = 0; $i < $cols; $i++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
echo '</tr>';
}
$e++;
}
echo '</table>';
}
Firstly, you don't need the While loop, it's pointless and probably causing issues.
Secondly, you're using $i for both your rows and your columns, you can't use the same variable for both. Use $i for one and $j for the other. This should work:
if(isset($_POST['submit'])) {
$rows = (int)$_POST['row_num'];
$cols = (int)$_POST['col_num'];
$n = 1;
echo '<table id="">';
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($j = 0; $j < $cols; $j++) {
echo '<td><input type="text" name="field_' . $n++ . '"></td>';
}
echo '</tr>';
}
echo '</table>';
}
scope variable $i problem here :
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($i = 0; $i < $cols; $i++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
}
i guess 2nd for condition change $i to $j :
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($j = 0; $j < $cols; $j++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
}

Categories