table printing one more cell instead of new line - php

I am trying to limit the printing of table cells to 3 per row. This worked in one example, but clearly not working when I tried to use the same code somewhere else in the site. This is the code:
$n=3;
echo "<table cellpadding='10' cellspacing='10' style='margin-right:-70px;'><tr>";
$users_count = count($users);
for($i=0; $i<$users_count;$i++)
{
$temp = array();
$temp = $users[$i];
echo "<td>";
echo "<div id='kitchen_box'>";
echo "<div id='kitchen_box_details'>";
echo "<h4>".$temp->fullname . "</h4><br>";
if(strcmp($temp->address, '') == 0)
echo $temp->city;
else
echo $temp->address.", ".$temp->city;
echo "</div>";
echo "<div id='kitchen_box_pic'><img id='kitchen_image' src='".$temp->profilepic."' /></div>";
echo "</div>";
echo "</td>";
if($i != 0){
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
echo "</table>";
I can't see why this wouldn't work! I would really appreciate support on the matter :)

There are several issues with your code. But your main issue is that you're using a zero-based increment, but doing a 1-based check. So, a table of your the results of an $i!=0 && $i%$n==0 goes like this:
$i $result
0 false
1 false
2 false
3 true
So, you see, the result closes the row after the fourth, not the third cell. To fix this, change the line to:
if($i % $n == $n-1 && $i != $users_count-1){
You should also include a closing </tr> tag with your closing </table> tag.
Incidentally, you shouldn't give the same ID to multiple elements on a page. Each of your kitchen_box and kitchen_box_div DIV tags will have the same ID. If you want this for CSS, use classes. Otherwise, you might try adding the value of $i to each ID.
Nitpicking on request:
The line $temp = array(); seems a little pointless, especially since you don't want $temp to be an array, but an object.
The else{ echo ""; } lines are also redundant.
You don't need the if($i != 0) check now because that case will not pass the next test anymore.
Otherwise the code seems fine to me.

I think the problem is that your $i starts at zero.
Let's look at your conditions before creating a new row :
if($i != 0){
if($i % $n == 0 && $i != $users_count-1)
If $i = 0, the first condition isn't matched. Then the second isn't for $i = 1 and $i = 2. And then your script creates another ... in your first row before matching your if conditions for the first time.
I guess you could move this part of the code right after the beginning of the for instructions :
for($i=0; $i<$users_count;$i++)
{
if($i != 0)
{
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
// Echo your <td> ... </td>
}
echo "</tr></table>";

Related

PHP Loop determine 4th iteration but if the iteration is less than 4 echo something

Heres the scenario, I have array with 7 items and I want to separate them in every fourth iteration.. just like this
$counter2 = 0;
$counter3 = 0;
$counter4 = 0;
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
foreach($sample_array as $sample_array_value)
{
if(++$counter4 % 4 == 0)
{
echo $sample_array_value;
echo "</div>";
}
elseif(++$counter3 % 3 == 0)
{
echo $sample_array_value;
}
elseif(++$counter2 % 2 == 0)
{
echo $sample_array_value;
}
else
{
echo "<div>";
echo $sample_array_value;
}
}
The out put will be div AsoPusaDagaKuting /div div TutaBubwitBoom
The problem is when it ends in iteration that doesn't count 4 it doesn't give the separator ending..
I need it to output div AsoPusaDagaKuting /div div TutaBubwitBoom /div
Thanks in advance...
You can split it with array_chunk, implode the new subarrays of 4 with array_map, then echo it with implode.
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
echo "<div>", implode("</div><div>", array_map("implode", array_chunk($sample_array, 4))), "</div>";
Result:
<div>AsoPusaDagaKuting</div><div>TutaBubwitBoom</div>
Try this:
$i = 0;
foreach($sample_array as $sample_array_value)
{
if(++$counter4 % 4 == 0)
{
echo $sample_array_value;
echo "</div>";
}
elseif(++$counter3 % 3 == 0 || ++$counter2 % 2 == 0)
{
echo $sample_array_value;
}
else
{
echo "<div>";
echo $sample_array_value;
}
$i++;
}
if ($i % 4 != 0) {
echo "</div>";
}
You are printing the values inside every condition, then why not use echo once outside any condition? Also you want to close and open a div tag for the forth element, then only 1 counter would do the trick. Only this will work -
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
$i = 0;
echo "<div>";
foreach($sample_array as $sample_array_value)
{
if($i > 0 && $i % 4 == 0)
{
echo "</div><div>";
}
echo $sample_array_value;
$i++;
}
echo "</div>";
Output
<div>AsoPusaDagaKuting</div><div>TutaBubwitBoom</div>

If else using for setting class

I want to set every first and second tr with a different class.
With my code I only get "odd" on every tr.
Does anyone knew what is wrong?
$rowCount = 0;
if ($rowCount++ % 2 == 1 ) :
echo "<tr class='even'>";
else:
echo "<tr class='odd'>";
endif;
try this one (keep the $rowCount setting outside the loop):
for($row = 0; $row < $rowTotal; $row++)
{
echo "<tr class='".($row % 2 ? "even" : "odd")."'>";
}
Your logic implementation is going in wrong direction
$rowCount = 0;//This was always initializing your count to 0
Resulting always odd class added
Change it to this:
for ($rowCount = 0;$rowCount<$total; $rowCount++) {
if ($rowCount % 2 == 1 ) :
echo "<tr class='even'>";
else:
echo "<tr class='odd'>";
endif;
}
OR you can simply use ternary operator as
for ($rowCount=0; $rowCount<$total; $rowCount++) {
echo "<tr class='".($rowCount % 2 == 0 )?'odd':'even'."'>";
}

How to create two columns PHP?

I am having the worst time trying to create this, it should be simple. I have an $info array that consists of $person=>$personInfoArray and I am trying to say, for the first six, do one column, for the next x amount, do another column.
What I have is:
$infoCounter = 0;
foreach($info as $person => $information){
$infoCounter++;
echo '<tr>';
if($infoCounter <= 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}elseif($infoCounter > 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}
echo '</tr>';
}
I am essentially trying to create a table that looks like:
Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545 Name 9785
Some One else 54212 Something else 44121
But I am getting:
Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545
Name 9785
Some One else 54212
Something else 44121
Thoughts? This should be ridiculously easy.
See that you are creating a <tr> for each person, try doing something like this:
$infoCounter = 0;
foreach($info as $person => $information){
if ($infoCounter % 2 == 0) {
echo '<tr>';
}
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
if ($infoCounter % 2 == 0) {
echo '</tr>';
}
$infoCounter++;
}
Check that % its the modulus
You will always have "2" columns with this code. I think what you are after is this.
$counter = 0;
foreach($info as $person => $information){
if($counter == 0) echo "<tr>";
echo "<td>$person</td>";
echo "<td>$information</td>";
$counter += 2;
if($counter == 4) {
$counter = 0;
echo "</tr>";
}
}
Keep in mind this is crude and off the top of my head. Not sure why you want 4 columns for 2 column information, other than formatting.

PHP Infinite While Loop, Unknown Cause

<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i==1 || ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i=$page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Trying to build a webpage that ouputs some search values neat the bottom of the page, however I keep getting an infinite loop. I've no Idea about what's causing it, and would like someone elses insight into the problem at hand.
Your if condition has a problem
Change
$i =1
with
$i==1
You have a number of places that you reset $i, inadvertently I assume
$i=1
and
$i=$page
replace them with ==
<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i == 1|| ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i == $page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Changes in 1st and second if condition.
1) $i == 1
2) $i == $page
is there a reason why this can not be accomplished using a for loop ? While loops are always risky and easy to cause problems if there is a scenario to cause to loop for ever.
eg.
for ($i = 0; $i<=$totalpages; $i++){
// do something
}
There is some issues in the conditions
first: the
|| $i=1||
allways return true.
Change for
|| $i==1||
Second: The same case of adobe
if ($i=$page) //Allways return try is assignation operation
Change to:
if ($i==$page)

Problem with displaying rows of 3 in php

So basically I have a script that is suppose to display my code into rows of 3, and create a new row. However, it is all displayed in one column. Here's the code.
while($mysql2 = mysql_fetch_array($sql2)) {
$prodlogo = $mysql2['logo'];
$prodid = $mysql2['id'];
$prodname = $mysql2['name'];
$x = 1;
if($x==1) {
echo "<tr>";
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
$x++;
if($x==3) {
echo "</tr>";
$x = 1;
}
}
move the $x out of the loop,otherwise you are reset it to one in every loop
each <td> will be a separate column. I would suggest you format it thus.
while($mysql2 = mysql_fetch_assoc($sql2)) {
foreach($mysql2 as $row) {
echo "<tr>";
echo "<td>".$row['logo']."</td><td>$".row['id']."</td><td>".$row['name']."</td>";
echo "</tr>";
}
}
also you need to use mysql_fetch_assoc() to get an associative array you can access like $row['key']
You may need to instantiate $x = 1 outside of your while loop - you're resetting it to 1 each time you're running it.
Using modulo might be good too:
if (($x % 3) == 0) {
echo "<tr>"
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
if (($x % 3) == 0) {
echo "</tr>"
}
You wouldn't need to worry about resetting $x to 1...

Categories