Php 如何在MySQL表中创建传递空数据的循环

Php 如何在MySQL表中创建传递空数据的循环,php,mysql,loops,oop,for-loop,Php,Mysql,Loops,Oop,For Loop,我在MySQL中有一个表,如下所示: 为了从这个表中检索数据,我做了以下工作: $menuSet = new Menus(); $menuSet->SelectMenuById($id); for($i=1;$i<=$menuSet->GetMenuItems();$i++){ $func = "GetMenuLink".$i; echo " <tr> <td>$i</td> <td>".$me

我在MySQL中有一个表,如下所示:

为了从这个表中检索数据,我做了以下工作:

$menuSet = new Menus();
$menuSet->SelectMenuById($id);
for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
$func = "GetMenuLink".$i;
    echo "
    <tr>
    <td>$i</td>
    <td>".$menuSet->$func()."</td>
    <td>
        <a title='Edit' href=''><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
        <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a>
    </td>
    </tr>
";
}

在这种情况下,您可以根据菜单项的值跳过循环迭代

<?php

$menuSet = new Menus();
$menuSet->SelectMenuById($id);
for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
    $func = "GetMenuLink".$i;
    $menuItemValue = $menuSet->{$func}();
    if (!$menuItemValue) {
        continue;
    }
    echo "
    <tr>
        <td>$i</td>
        <td>" . $menuItemValue . "</td>
        <td>
            <a title='Edit' href=''><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
            <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a>
        </td>
    </tr>
    ";
}

在for中放置if语句,仅当其不为null时才回显

还将
$menuSet->GetMenuItems()
强制转换为一个变量,并在其上执行foreach循环

“$items=$menuSet->GetMenuItems()”

<?php

$menuSet = new Menus();
$menuSet->SelectMenuById($id);
for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
    $func = "GetMenuLink".$i;
    $menuItemValue = $menuSet->{$func}();
    if (!$menuItemValue) {
        continue;
    }
    echo "
    <tr>
        <td>$i</td>
        <td>" . $menuItemValue . "</td>
        <td>
            <a title='Edit' href=''><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
            <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a>
        </td>
    </tr>
    ";
}
foreach($items as $count=>$item){
 if($menuSet->$func()){
  //echo statement 
 }
}