English 中文(简体)
PHP回收功能问题?
原标题:PHP recursive function problem?
  • 时间:2010-08-19 00:04:49
  •  标签:
  • php

我要在我的第一期和第四期<代码><ol>tag中添加两个不同的类别特性,但我确实不知道如何在我的休养职能中添加这种特性? 谁能帮助我?

这里是我的购买力平价。

function make_list ($parent = 0, $parent_url =   ) {
    global $link;
    echo  <ol> ;

    foreach ($parent as $id => $cat) {
        if($cat[ parent_id ] ==  0 ){
            $url = $parent_url . $cat[ url ];
            echo  <li><a href="  . $url .  " title="  . $cat[ category ] .   Category Link" style="color: orange; font-weight: bold;">  . $cat[ category ] .  </a> ;            
        } else {
            $url = $parent_url . $cat[ url ];
            // Display the item:
            echo  <li><a href="  . $url .  " title="  . $cat[ category ] .   Category Link">  . $cat[ category ] .  </a> ;
        }

        if (isset($link[$id])) {
            make_list($link[$id], $url);
        }               
        echo  </li> ;
    }       
    echo  </ol> ;
}

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
    print mysqli_error();
} 

$link = array();

while (list($id, $parent_id, $category, $url, $depth) = mysqli_fetch_array($dbc)) {
    $link[$parent_id][$id] =  array( parent_id  => $parent_id,  category  => $category,  url  => $url,  depth  => $depth);
}

make_list($link[0]);

产出

<ol>
   <li>First Nested List</li>
   <li>First Nested List</li>
   <li>First Nested List
      <ol>
        <li>Second Nested List</li>
        <li>Second Nested List</li>
        <li>Second Nested List
          <ol>
            <li>Third Nested List</li>
            <li>Third Nested List</li>
            <li>Third Nested List
              <ol>
                <li>Fourth Nested List</li>
                <li>Fourth Nested List</li>
                <li>Fourth Nested List</li>
              </ol>
            </li>
            <li>Third Nested List</li>
            <li>Third Nested List</li>
          </ol>
        </li>
        <li>Second Nested List</li>
        <li>Second Nested List</li>
      </ol>
   </li>
   <li>First Nested List</li>
   <li>First Nested List</li>
</ol>
最佳回答

仅作为参数添加深度。 然后检查其0或4或需要什么。

function make_list ($parent = 0, $parent_url =   , $depth=0) {
...
make_list($link[$id], $url, $depth+1);
...
问题回答

添加全球<代码>$num_links变量。 每当你发出<代码><ol>时,就会增加。 在达到你想要的价值时添加属性。

然而,如果你重新这样做,你所选择的职能与你重新解决的任务之间可能存在着不匹配的情况。 “第一或第四”很可能不是你想要检查的真正条件。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签