0
while(fetching){
    if($x != $y){
        echo "<div> Something </div>
                  <div>";
    }
    $x = $y;
    echo "<div> Something Else </div>";

    if($x != $y) {echo "</div>";} //The Problem Here
    }

In my code here, I can't close my element at all, All the elements above the ending are combined inside each other and the element is closed at the end of all of it, How can i fix that? i tried going

    if($x != $y){
        echo "<div> Something </div>
                  <div>}
    $x = $y;
    echo "<div> Something Else </div>";
    }
    if($x != $y) {</div>";}

but didn't work either.

What i'm trying to accomplish is

<div> Something </div>
   <div>
       <div> Something Else </div>
       <div> Something Else </div>
   </div>
<div> Something </div>
   <div>
       <div> Something Else </div>
       <div> Something Else </div>
   </div>

What always happens is

<div> Something </div>
   <div>
       <div> Something Else </div>
       <div> Something Else </div>
   <div> Something </div>
   <div>
       <div> Something Else </div>
       <div> Something Else </div>
   </div>
   </div>
Calibur Victorious
  • 638
  • 1
  • 6
  • 20

3 Answers3

0

While $x == $y the code right below: if($x != $y) {</div>";} will never run.

You have

$x = $y;

Meaning, always to ignore closing </div>, that has to be run in the conditional statement in if($x != $y) {..}.

This is logical error.

Whatever it is, I don't see the point in that assignment. Remove it and you will get your closing div tag where you want.

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • I'm trying to build that on my previous question's answer here http://stackoverflow.com/a/43507106/7490349 I want a way to close the div i started or even refactoring the entire code to close its div. – Calibur Victorious Apr 22 '17 at 14:18
0
$x = $y;
    echo "<div> Something Else </div>";

    if($x != $y) {echo "</div>;} //The Problem Here

First of all, that if statement is never going to run because you have assigned $x to $y.

Also, inside your first if statement, you have a stray <div> tag.

I'm not exactly sure what you're trying to accomplish but remove the second <div> tag in the while loop. If you want everything to be in one div then open a div before the while loop and close it after the loop

echo "<div>";
while(fetching)
{
  if($x != $y)
  {
    echo "<div> Something </div>";
  }
}
echo "</div>";
0

I think you want something like this

$idx = 0;
while(fetching){
    if($idx % 3 == 0 && $idx != 0){
        echo "</div>";
    }

    if($idx % 3 == 0){
        echo "<div> Something </div>";
        echo "<div>";
    } else {
        echo "<div> Something Else </div>";
    }

    $idx++;
}

This code prints the 'Something' tag including the open tag for 'Something Else' element every 1st, 4th, 7th ... nth iteration.

On the 4th, 7th ... nth iteration the closing tag is printed.

On all the other iterations 'Something else' is printed.

Thakkie
  • 604
  • 4
  • 17