如何在MySQL表

时间:2017-07-19 12:33:25

标签: php mysql loops oop for-loop

我在MySQL中有表格并且这样:

print screen of table

为了从这个表中检索数据,我做了这个:

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

因此,您可以看到我所做的就是获取menu_items并根据该数字检索数据。

但是该循环的问题在于它还计算了这个例子中menu_link_1的空字段。你可以在这张照片中看到结果:

print screen of menu

我不希望如此。我希望循环通过NULL列,只显示填充数据的列。

那我怎么能这样做呢?

这是我在这个项目中使用的php类,如果你想看看它......

<?php 
class Menus
{
    public $id,$mname,$menui,$menul1,$menul2,$menul3,$menul4,$menul5,$menul6,$menul7,$menul8,$menul9,$menul10,$menul11,$menul12,$menul13;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function GetMenus()
    {
        if(empty($name))
        {
            $menu = $this->db->prepare("select * from menu_nav");
            $menu->execute();
            $menu_array = array();
            while($row = $menu->fetch())
            {
                $menu_array[] = $row;
            }
            return $menu_array;
        }
        else
        {
            header("Location: php/includes/errors/008.php");
            exit();
        }
    }
    public function SelectMenuById($id)
    {
        if(!empty($id))
        {
            $mnu = $this->db->prepare("select * from menu_nav where id = ?");
            $mnu->bindParam(1,$id);
            $mnu->execute();
            while($row = $mnu->fetch())
            {
                $this->id           = $row['id'];
                $this->mname        = $row['menu_name'];
                $this->menui        = $row['menu_items'];
                $this->menul1       = $row['menu_link_1'];
                $this->menul2       = $row['menu_link_2'];
                $this->menul3       = $row['menu_link_3'];
                $this->menul4       = $row['menu_link_4'];
                $this->menul5       = $row['menu_link_5'];
                $this->menul6       = $row['menu_link_6'];
                $this->menul7       = $row['menu_link_7'];
                $this->menul8       = $row['menu_link_8'];
                $this->menul9       = $row['menu_link_9'];
                $this->menul10      = $row['menu_link_10'];
                $this->menul11      = $row['menu_link_11'];
                $this->menul12      = $row['menu_link_12'];
                $this->menul13      = $row['menu_link_13'];
            }
        }
        else
        {
            header("Location: php/includes/errors/009.php");
            exit();
        }
    }
    public function DeleteMenu($id)
    {
        if(!empty($id))
        {
            $adm = $this->db->prepare("delete from menu_nav where id = ?");
            $adm->bindParam(1,$id);
            $adm->execute();
        }
        else
        {
            header("Location: php/includes/errors/010.php");
            exit();
        }
    }
    public function DeleteMenuItem($i)
    {
        if(!empty($i))
        {
            $adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = NULL");
            $adm->bindParam(1,$i);
            $adm->execute();
        }
        else
        {
            header("Location: php/includes/errors/011.php");
            exit();
        }
    }
    public function GetId()
    {
        return $this->id;
    }
    public function GetMenuName()
    {
        return $this->mname;
    }
    public function GetMenuItems()
    {
        return $this->menui;
    }
    public function GetMenuLink1()
    {
        return $this->menul1;
    }
    public function GetMenuLink2()
    {
        return $this->menul2;
    }
    public function GetMenuLink3()
    {
        return $this->menul3;
    }
    public function GetMenuLink4()
    {
        return $this->menul4;
    }
    public function GetMenuLink5()
    {
        return $this->menul5;
    }
    public function GetMenuLink6()
    {
        return $this->menul6;
    }
    public function GetMenuLink7()
    {
        return $this->menul7;
    }
    public function GetMenuLink8()
    {
        return $this->menul8;
    }
    public function GetMenuLink9()
    {
        return $this->menul9;
    }
    public function GetMenuLink10()
    {
        return $this->menul10;
    }
    public function GetMenuLink11()
    {
        return $this->menul11;
    }
    public function GetMenuLink12()
    {
        return $this->menul12;
    }
    public function GetMenuLink13()
    {
        return $this->menul13;
    }
}
?>

2 个答案:

答案 0 :(得分:0)

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

<?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>
    ";
}

答案 1 :(得分:0)

在for中放置一个if语句,如果它不为空

,则只回显

还将foreach($items as $count=>$item){ if($menuSet->$func()){ //echo statement } } 转换为变量并对其进行foreach循环。

'$ items = $ menuSet-&gt; GetMenuItems()'

@GET("user")
Call<User> getUserByEmail(@Query("email") String email, @Header("Authorization") String authorization);