0

so I have a logic problem here.

what i want:

cicle through an array (actually a foreachable object) if it's the first element echo a <div>, then every three elements echo the closing </div>.

If there are fewer than 3, of course end the div too. EDIT: since I was not clear, what I want to achieve is based on n° of elements. Paradoxically I need it to work with 0 elements too.

0 elements:

<div><div>

1 element:

 <div>
   <a></a>
 </div>

3 elements:

 <div>
      <a></a>
      <a></a>
      <a></a>
 </div>

5 elements:

    <div>
      <a></a>
      <a></a>
      <a></a>
    </div>
    <div>
      <a></a>
      <a></a>
    </div>

etc..... you got the point

my wrong code:

        $counter = 0;
        foreach ($prods as $prod) {
            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '<div data-count="' . $counter . '">';  //just keeping track
            }
            $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';

            $counter++;

            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '</div>';
            }               
        }
        $counter = 0;
        if($counter == 0 || ($counter % 3) == 0) {
            $htm_l .= '</div>';
        } 

What can I do? (prod is just a collection of objects, could be an array of strings)

6
  • Please include an MVCE that we can test. We can't see that your code isn't working right now because we can't test it because we don't know what $prods is. Commented Oct 18, 2017 at 15:58
  • you want to archive which is in left side ?? Commented Oct 18, 2017 at 15:58
  • No the problem isn't $prod or $variable, see it as an array of strings if you like. I want to be able to achieve both right or left side, depending how many items in prods ""array"" Commented Oct 18, 2017 at 16:01
  • What is the output of your code? Commented Oct 18, 2017 at 16:03
  • clear us what you need as output Commented Oct 18, 2017 at 16:03

3 Answers 3

1

This is untested code but should work for you unless you have the special case of an empty prods object and you don't want an empty div produced. That will require a slightly more complex solution

$counter = 0;
// start the first div
$htm_l = '<div data-count="' . $counter . '">';
foreach ($prods as $prod) {
    $counter++;
    // close the current div and open a new one after 3 objects
    if ($counter % 3 == 0) {
        $htm_l .= '</div> <div data-count="' . $counter . '">';
    }
    // add the object specific data
    $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';
}
// close the last div
$htm_l .= '</div>';

To handle that special case you could do something like this.

$counter = 0;
// find out how many total objects we will be stepping through
$total_count = count($prods);
$htm_l = '';
foreach ($prods as $prod) {
    $counter++;
    // always start with a div for the first of 3 objects
    if ($counter % 3 == 1) {
        $htm_l .= '<div data-count="' . $counter . '">';
    }
    // add the object specific data
    $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';
    // close the div after 3 objects or after the last object
    if ($counter % 3 == 0 || $counter == $total_count) {
        $htm_l .= '</div>';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're wanting to unconditionally start with a <div> and end with a </div>. The only condition is when to close/open during the loop. So something like this should work:

$counter = 0;
$htm_l   = '<div data-count="0">';

foreach ( $prods as $prod ) {
    $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';

    $counter++;

    if ( $counter % 3 == 0 ) {
        $htm_l .= '</div>';

        if ( $counter != count( $prods )) {
            $htm_l .= "<div data-count=\"$counter\">";
        }
    }
}

if ( $counter % 3 ) {
    $htm_l .= '</div>';
}

Comments

0

Try the following:

$counter = 1;
$total = count($prods);
$htm_l = '';
$htm_l .= "<div data-count=\"1\">\r\n";
foreach ($prods as $prod)
{
    $prod->id = $counter;

    $htm_l .= "<a data-id=\"{$prod->id}\"> link </a>\r\n";


    if (($counter % 3) == 0)
    {
        $htm_l .= "</div>\r\n";
    }
    if($counter == $total){$counter++;continue;}
    if (($counter % 3) == 0)
    {
        $htm_l .= "<div data-count=\"$counter\">\r\n";
    }

    $counter++;
}
if (($counter % 3) == 0)
{
    $htm_l .= '</div>';
}

echo($htm_l);

This will iterate over the $prods and will add <div> after every 3 <a> tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.