2009-10-31 81 views
0

如何将下面的代码中的php函数转换为非函数。如何将PHP函数转换为非函数?

<?php 
require_once ('./mysqli_connect.php'); // Connect to the db. 

function make_list ($parent) 
{ 
    global $tasks; 
    echo '<ol>'; 
    foreach ($parent as $task_id => $todo) 
    { 
     echo "<li>$todo"; 
     if (isset($tasks[$task_id])) 
     { 
      make_list($tasks[$task_id]); 
     } 
     echo '</li>'; 
    } 

    // Close the ordered list: 
    echo '</ol>'; 
} 

$mysqli = new mysqli("localhost", "root", "", "sitename"); 
$dbc = mysqli_query($mysqli,"SELECT task_id, parent_id, task FROM tasks WHERE date_completed='0000-00-00 00:00:00' ORDER BY parent_id, date_added ASC"); 

if (!$dbc) 
{ 
    // There was an error...do something about it here... 
    print mysqli_error(); 
} 
$tasks = array(); 

while (list($task_id, $parent_id, $task) = mysqli_fetch_array($dbc, MYSQLI_NUM)) 
{ 
    // Add to the array: 
    $tasks[$parent_id][$task_id] = $task; 
} 

make_list($tasks[0]); 

mysqli_close(); // close the connection 

// Include the html footer 
include('./includes/footer.html'); 
?> 

即使我没有发布的代码的其余部分是非函数形式,最好还是留下这样的代码。

+9

我认为更好的问题是:为什么你会想要* *把你的函数成非功能? – hbw 2009-10-31 18:46:07

回答

8

第一个:您不能以简单的方式将递归函数转换为意大利面条代码。
第二个:没有必要这样做。把你的逻辑,行为分解为功能,以及数据的逻辑和表达。不要用html标签将您的代码垃圾邮件。使用某种模板机制。

1

除了erenon的回答(这我完全支持,他已经作出,为什么你的问题是导致了错误的方向上分):

使用(或创建)一个隐藏连接一些数据库抽象类,查询执行等等,使得以后更改数据库或适应接口更改变得更加容易,并且您有一个集中处理错误的地方。

和不说话的数据库作为超级用户..