How to increase the value of $ var without a loop in PHP?

I have this problem when I want to increase the value with 1 and apply it to my HTML, but I cannot use a loop for()or while()(at least I think I can not). I am setting up an e-merchandise (opencart) program, and my php knowledge is not enough to solve this problem.

This function displays categories from the store. It uses a variable that is constantly updated through $var .= "value".

So far, I know how many subcategories there are, but I don’t know how to apply this range to my HTML.

I am working on a situation like below

<ul id="cats">
 <li id="cat1">Cat
  <ul id="sub1">
   <li>item</li>
   <li>item</li>
  </ul>
 </li>
 <li id="cat2">Cat
  <ul id="sub2">
   <li>item</li>
   <li>item</li>
  </ul>
 </li>
</ul>

I do not know how to increase the number of second unordered lists. Below is the code in which two unordered lists are generated.


[..]
$cPiD = strlen($parent_id);

if ($results) {
 if ($parent_id == 0) {
  $output .= '<ul id="cats">';
 } else {
  $output .= '<ul id="sub'.$cPiD.'">';
 }
}

[..]

$cPiD ( 2). , ( id="sub1" id="sub2" tot ( )).

, for() else, HTML <ul> .

PHP-,


$category_id = array_shift($this->path);
$output = '';
$results = $this->model_catalog_category->getCategories($parent_id);
$count = 0;
$cPiD = strlen($parent_id);

if ($results) { if ($parent_id == 0) { $output .= '<ul id="cats">'; } else { $output .= '<ul id="sub'.$cPiD.'">'; } }

foreach ($results as $result) { $count++; if (!$current_path) { $new_path = $result['category_id']; $output .= '<li id="cat'.$count.'">'; } else { $new_path = $current_path . '_' . $result['category_id']; $output .= '<li>'; }

$children = '';

$children = $this->getCategories($result['category_id'], $new_path);

$output .= $result['name'];

$output .= $children;

if (!$current_path) { $output .= '</li>'; } else { $output .= '</li>'; }

}

if ($results) { if ($parent_id == 0) { $output .= '</ul>'; } else { $output .= '</ul>'; } }

- , ?

EDIT: , foreach(), , .

if (!$current_path) {
 $output .= '$result['name'] . ' <ul id="sub'.$count.'">';
}else{
 $output .= $result['name'];
}
+3
1

:

// at the top of your code (ouside of the loop)
$cPiD = 1;
// inside the loop you need to increment the parameter 
$output .= '<ul id="sub'.$cPiD++.'">';

1. ( )

0

Source: https://habr.com/ru/post/1775649/


All Articles