Loop not generating the HTML

If we dont pass one array.

I know every one is trying to help me looks like my basic understanding has some “missing understanding” that’s why I am facing a dead end here.

Try the three tests suggested in post #60 and var_dump(…) the array contents inside the function.

Edit:

After trying the three tests, remove the default = and run the tests again.

1 Like

Case: 1 →

Result delivered with var_dump →

array(6) {
  ["product1"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(29)
    ["features"]=>
    array(3) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 2"
      [2]=>
      string(9) "feature 3"
    }
  }
  ["product2"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(43)
    ["features"]=>
    array(4) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 2"
      [2]=>
      string(9) "feature 3"
      [3]=>
      string(9) "feature 4"
    }
  }
  ["product3"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(299)
    ["features"]=>
    array(3) {
      [0]=>
      string(9) "feature 2"
      [1]=>
      string(9) "feature 3"
      [2]=>
      string(9) "feature 4"
    }
  }
  ["product4"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(470)
    ["features"]=>
    array(3) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 3"
      [2]=>
      string(9) "feature 4"
    }
  }
  ["product5"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(689)
    ["features"]=>
    array(2) {
      [0]=>
      string(9) "feature 3"
      [1]=>
      string(9) "feature 4"
    }
  }
  ["product6"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(4999)
    ["features"]=>
    array(5) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 2"
      [2]=>
      string(9) "feature 3"
      [3]=>
      string(9) "feature 4"
      [4]=>
      string(9) "feature 5"
    }
  }
}
$4999
My Amazing Product
feature 1
feature 2
feature 3
feature 4


Case: 2 →

Result is same, but a warning is generated→

Case: 3 →

Array disappeared as it says array(0), and notices in place →

Defaulty removed condition 1:

Condition 2→


Condition 3:


1 Like

Any conclusions?

1 Like

Passing variable is essential I think. w/o them we can’t achieve end results.
Rest can you help me with further analysis?

It looks as though calling var_dump(…) confused the problem, too much information. Error reporting perhaps would have been adequate.

I only meant to remove the empty array and to leave the $array parameter.

By not setting the $array parameter value and calling the function, errors would have been produced stating missing parameters because there were no defaults for the function parameters.

I prefer setting parameters because it makes the function easier to understand and also added extra checking that fails if every condition is not satisfied.

1 Like

We have put string empty →
string $item = ''

So in this →

foreach($array["$item"]['features'] as $key => $feature) :

what is this →
$array["$item"]['features'] ?

This one →

$array[“0”][‘features’]

The passed default function card array parameters are trying to cover every eventuality.

Please copy and paste the results from the following script:

echo '<pre>';
var_dump($array);
echo '</pre>';
1 Like

Hi there @John_Betong

array(6) {
  ["product1"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(29)
    ["features"]=>
    array(3) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 2"
      [2]=>
      string(9) "feature 3"
    }
  }
  ["product2"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(43)
    ["features"]=>
    array(4) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 2"
      [2]=>
      string(9) "feature 3"
      [3]=>
      string(9) "feature 4"
    }
  }
  ["product3"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(299)
    ["features"]=>
    array(3) {
      [0]=>
      string(9) "feature 2"
      [1]=>
      string(9) "feature 3"
      [2]=>
      string(9) "feature 4"
    }
  }
  ["product4"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(470)
    ["features"]=>
    array(3) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 3"
      [2]=>
      string(9) "feature 4"
    }
  }
  ["product5"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(689)
    ["features"]=>
    array(2) {
      [0]=>
      string(9) "feature 3"
      [1]=>
      string(9) "feature 4"
    }
  }
  ["product6"]=>
  array(3) {
    ["title"]=>
    string(18) "My Amazing Product"
    ["price"]=>
    int(4999)
    ["features"]=>
    array(5) {
      [0]=>
      string(9) "feature 1"
      [1]=>
      string(9) "feature 2"
      [2]=>
      string(9) "feature 3"
      [3]=>
      string(9) "feature 4"
      [4]=>
      string(9) "feature 5"
    }
  }
}

Is this some special kind of array → array $array = [] if, yes, please guide me towards the correct learning resources. There is a huge possibility that I am missing some fundamental understanding of arrays.

The use of square brackets instead of array() is much simpler and more intuitive. I think it was introduced with PHP 7.0 quite some time ago:

================== // =====================
OK, the parameter $array is populated and when used in the following script will create a string of $features and add the array necessary iterated array $feature: NOTICE THE USE OF $features SINGULAR AND PLURAL

<?php

$features = ''; // PLURAL many features
foreach($array["$item"]['features'] as $key => $feature) :
  $features .= '<li>' .$feature .'</li>'; // NOTICE SINGLE $feature
endforeach;

================== // =====================

Try the following to show the values: (echo may not work if the values are arrays, objects, etc):

// what is this
print_r( $array["$item"]['features'] ) :

// This one
print_r( $array[“0”][‘features’] );

Compare the print_r values to the var_dumped $array.

1 Like

Hi @John_Betong, it was introduced in Php 5.4 and is right on the page you linked to.

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [ ] .

2 Likes
  $result = <<< ____EOT
   <div class="cardbody text-center">
      <h1> \${$array["$item"]['price']} </h1>
      <h3 class="text-centre">
         {$array["$item"]['title']}
      </h3>

      <ul class="list-group">
        $features
      </ul>
   </div>
____EOT;

How can we implement it w/o the Herdoc syntax?

May I ask why you do not want to use Heredoc?

Without Heredoc there will be a lot of extra script which will be more to difficult to understand and prone to syntax errors.

// When I am back on the desktop I will have a go.

Back again and surprised at how easy it was not to use HereDoc and use PHP Strings instead although it did take quite a 8it of debugging to validate and prevent errors :{

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

  $item     = 'item';
  $features = 'FEATURES';
  $array['item']['price'] = 'TESTING';
  $array['item']['title'] = 'TITLE';

  $result1 = <<< ____EOT
   <div class="cardbody text-center">
      <h1> \${$array["$item"]['price']} </h1>
      <h3 class="text-centre">
         {$array["$item"]['title']}
      </h3>
      <ul class="list-group">
        $features
      </ul>
   </div>
____EOT;
  $len1 = strlen($result1);


  $result2 = "
   <div class='cardbody text-center'>
      <h1> \${$array["$item"]['price']} </h1>
      <h3 class='text-centre'>
         {$array[$item]['title']}
      </h3>
      <ul class='list-group'>
        $features
      </ul>
   </div>
   ";
  $len2 = strlen($result2);

echo '<!DOCTYPE HTML><title> title goes here </title>';

   echo $tmp = <<< ____EOT
    <dl>
      <dt> Using PHP HereDoc - strlen($len1) </dt>
      <dd> $result1 </dd>
      <dd> &nbsp; </dd>
      
      <dt> Using PHP Strings  - strlen($len2) </dt>
      <dd> $result2 </dd>
      <dd> &nbsp; </dd>
    </dl>        
____EOT;

Output:

Using PHP HereDoc - strlen(190)
    $TESTING
    TITLE

        FEATURES 

      
Using PHP Strings - strlen(195)
    $TESTING
    TITLE

        FEATURES 

Rather than just mindlessly copying and pasting I would advise trying to write the script yourself otherwise you will have learnt how easy it is to copy and paste :slight_smile:

1 Like

But I think that you are still using herdoc syntax?

The HereDoc used was for the HTML Document Definition which uses the two variables $result1 and $result2. Former is HereDoc and latter is using strings:

Here is a version which is not using HereDoc:

<?php 
$result2 = "
   <div class='cardbody text-center'>
      <h1> \${$array["$item"]['price']} </h1>
      <h3 class='text-centre'>
         {$array[$item]['title']}
      </h3>
      <ul class='list-group'>
        $features
      </ul>
   </div>
   ";
  $len2 = strlen($result2);

?>
     <dl>
      <dt> Using PHP HereDoc - strlen(<?= $len1 ?>) </dt>
      <dd> <?= $result1 ?> </dd>
      <dd> &nbsp; </dd>
      
      <dt> Using PHP Strings  - strlen(<?= $len2 ?>) </dt>
      <dd> <?= $result2 ?> </dd>
      <dd> &nbsp; </dd>
    </dl>        

1 Like

Sir, what purpose is this line serving?

Try modifying the line and notice the difference.

For the benefit of other users, report back with your findings.

1 Like

Yes,

\$ → It is USD sign printed at the correct place.

1 Like

I was trying to do it with ob_get method. Like this →

			<?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><?php {$array["$item"]['price']} ?></h1>
      <h3 class="text-centre"><?php {$array["$item"]['title']} ?></h3>
      <ul class="list-group text-centre">
        <?php $features ?>
      </ul>
      <div class="footer text-centre">
        <!-- Stripe Button wil go here -->
      </div>
    </div><!-- cardbody -->

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

This is generating error:

If I remove it then the error pops up stating that :
Parse error : syntax error, unexpected ‘}’