如何在 PHP 中不循环地增加 $var 值?

发布于 2024-10-02 18:49:40 字数 2326 浏览 0 评论 0原文

我遇到这个问题,我想将值增加 1 并将其应用于我的 HTML,但我无法使用 for()while() 循环(至少我认为我不能)。我正在定制一个电子商品程序(opencart),但我的 php 知识不足以解决这个问题。

有这个功能可以显示商店的类别。它使用通过 $var .= "value" 不断更新的变量。

到目前为止,我知道有多少个子类别,但我不知道如何将此范围应用于我的 HTML。

我正在努力解决如下情况,

<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>

我不知道如何增加第二无序列表的数量。在生成第二个无序列表的代码下方。


[..]
$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" 应用到第二个无序列表一个(如我上面的例子))。

问题是我不能在 else 部分之后使用 for() 循环,因为在我的 HTML 中我将得到两个

    标签而不是一个。

在 PHP 代码下面,这一切都发生了


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

if ($results) { 如果($parent_id == 0){ $output .= '

    '; } 别的 { $output .= '
      '; } }

foreach ($results as $result) { $计数++; 如果(!$当前路径){ $new_path = $result['category_id']; $output .= '

  • '; } 别的 { $新路径=$当前路径。 '_' 。 $结果['category_id']; $output .= '
  • '; }
  • $孩子= '';

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

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

    $输出.= $孩子;

    如果(!$当前路径){ $output .= ''; } 别的 { $output .= ''; }

    }

    if ($结果) { 如果($parent_id == 0){ $output .= ''; } 别的 { $output .= ''; } }

    有人知道如何解决这个问题吗?

    编辑: 哦,我尝试在 foreach() 循环中添加以下构造,但是当某些类别没有任何子类别时,这会出现问题。

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

    I have this issue where i want to increase a value with 1 and apply this to my HTML, but i can't use a for() or while() loop (at least i think i can't). I'm customizing an e-merchandise program (opencart) and my php knowledge isn't enough to tackle the problem.

    There is this function which displays the categories from the store. It uses a variable that is constantly updated via $var .= "value".

    I'm so far that i know how many sub-categories there are, but i don't know how to apply this range to my HTML.

    I'm working towards 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 don't have a clue how to increase the count of the second unordered lists. Below the code where the second unordered lists are generated.

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

    The variable $cPiD holds the total amount of sub categories (in this case 2). I want this variable to automatically apply the correct number to the unordered list (so apply id="sub1" to the first unordered list and id="sub2" tot he second one (as in my example above)).

    The problem is that i can't use a for() loop after the else part, because in my HTML i wil get two <ul> tags instead of one.

    Below the PHP code where it all happens

    
    $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>'; } }

    Does anybody maybe have an idea how to solve this?

    EDIT:
    Oh, i tries adding the following construction in the foreach() loop, but that gave problems when a certain categories don't have any sub categories.

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

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    苏别ゝ 2024-10-09 18:49:40

    你可以使用这个:

    // 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。(已经使用过它之后)

    You can use this:

    // 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++.'">';
    

    After each time the item is used, its value will be incremented by 1. (after already using it)

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文