Abdul Shakoor Kakar
Abdul Shakoor Kakar

Reputation: 611

if mysql field empty

i have menu from database.

$sqlCommand = "SELECT id, menu FROM myTable WHERE showing='1' ORDER BY id ASC"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$menu='';
while ($row = mysqli_fetch_array($query)) { 
    $id = $row["id"];
    $hormenu = $row["menu"];
    $menu .='<li><a href="mypage.php?id='.$id.'">'. $hormenu .'</a></li>';
     }

when i keep any menu field empty. it shows me

<li><a href="mypage.php?id='.$id.'"></a></li>

how could i hide or disable it

Any help Plzz.

Upvotes: 0

Views: 942

Answers (3)

ain
ain

Reputation: 22759

If "when i keep any menu field empty" means that the row in database has NULL or '' value then just do not select such rows, ie to exclude rows where menu field is empty add (menu <> '')AND(menu IS NOT NULL) to your query's WHERE clause. So your SQL would be

SELECT id, menu FROM myTable WHERE(showing='1')AND(menu <> '')AND(menu IS NOT NULL) ORDER BY id ASC

Althought it would probably be better not to save such (invalid?) rows into database at all.

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39724

check if it has any data

$hormenu = $row["menu"];
if($hormenu){
    $menu .='<li><a href="mypage.php?id='.$id.'">'. $hormenu .'</a></li>';
}

Upvotes: 1

Sascha Galley
Sascha Galley

Reputation: 16091

while ($row = mysqli_fetch_array($query)) { 
    if (!empty($row["menu"]))
        $menu .='<li><a href="mypage.php?id='.$row["id"].'">'. $row["menu"] .'</a></li>';
}

Upvotes: 1

Related Questions