Loop not generating the HTML

Either an echo is missing or PHP wants replacing with =

1 Like

This was the case +

{$array["$item"]['price']}
{$array["$item"]['title']}

I also taxed {}

Like this →

$array["$item"]['price']
$array["$item"]['title']

Still, I am facing issues. Features are not getting published

.

even if I echo then they are publishing just $features.

You still have a syntax error there, the one that @John_Betong mentioned earlier.

Show us your code. If you mean that your code is

echo $features;

and all it does is echo the contents of that variable, then please explain why that’s incorrect, and what it should be doing instead.

PS - much easier to copy/paste the code and put a comment on the end to show where the error is than to create all these images with arrows on, surely?

2 Likes

whole code →

			<?php

  foreach($products as $key => $product) :
      echo card($products, $key);
  endforeach;

//=======================
function card( array $array = [], string $item = ''):string {
  $features = '';

    foreach($array["$item"]['features'] as $key => $feature) :
    $features .= '<li class=""> $feature </li>';
    endforeach;
    ob_start();
    ?>
    <div class="cardbody">
      <h1 class="text-centre"><span>$</span><?= $array["$item"]['price'] ?></h1>
      <h3 class="text-centre"><?= $array["$item"]['title'] ?></h3>
      <ul class="list-group text-centre">
        <?= $features ?>
      </ul>
      <div class="footer text-centre">
        <!-- Stripe Button wil go here -->
      </div>
    </div><!-- cardbody -->

    <?php
    $result = ob_get_clean();
    return $result;
      }
    ?>

Output →

The echo or = is missing and where is declare strict, error reporting, etc?

This problem has no problem in output, but $features part.

Take a look at post #75 and notice how the for next loop is not the same.

1 Like

Think about the differences between single and double quotes.

1 Like

Got it:

$features .= '<li class="">' . $feature . '</li>';

1 Like

That’s one way. The only reason for the complexity is because you want those quotes around the blank class-name, so couldn’t you leave that out altogether? I’m very rusty on HTML / CSS, but I can’t see what it’s doing. Then you have

$features .= "<li>$feature</li>";
1 Like

Yes, true. Thanks for pointing.

[off-topic]
I have adopted the following coding style:

  1. PHP variables are never inside double quoted strings
  2. PHP variables are always concatenated using periods although I should use commas
  3. strings are always enclosed in single quotes
  4. href, class, title, alt, etc always use double quotes
  5. use PHP HereDoc for complicated strings

Just my style but I recommend adopting your own style and sticking to it.
[/off-topic]

2 Likes

I agree, in particular about not just sticking variable names inside double-quoted strings. I’m not actually sure, on that basis, why I posted my last post, really it was just to point out a further simplification.

2 Likes

For sure your intention was to help. Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.