2011-08-09 80 views
1

我如何可以显示或根据结果隐藏DIV除法来自服务器,用于处理请求的文件中,我得到一个客户对象从数据库中,根据用户的要求,如果我没有找到我想隐藏div的相应客户。秀取决于结果从服务器

这里是我的PHP代码

$customer=getCustomerViaCellPhone($link,$cellPhoneNo); 
if(!$customer){ 
    $error = 'No result found'; 
} 

这里的景色

<div id="customerInfo" class="<?php (is_null($customer))?'hide':''?>"> 
.... 
</div> 

但它表明在两种情况下的div

+0

检查你的功能getCustomerViaCellPhone在手机的情况下返回不存在/未找到 – Sleeperson

+0

干了什么'的var_dump($客户)'返回? – ajreal

+0

NULL [ “customerCellPhone:私人”] => NULL [ “customerTelephone:私人”] => NULL [ “customerDob:私人”] => NULL [ “customerCountry:私人”] => NULL [ “customerCity:私人”] = > NULL [ “customerNeighborhood:私人”] => NULL [ “customerStreet:私人”] => NULL [ “customerAddressDetails:私人”] => NULL [ “customerEmail:私人”] => NULL – palAlaa

回答

3

你在你的例子里缺了命令echo所以它应该是:

<div id="customerInfo" class="<?php echo (is_null($customer))?'hide':''?>"> 
.... 
</div> 

显然你的CSS还需要定义一个名为.hide类,里面有display:hidden;

0
<div id="customerInfo" style="display:<?php echo (is_null($customer))?'none':'block'?>"> 

看看CSS' displayvisibility选项

1

根据您的意见,$customerNULL,它与它NULL值的对象。这将返回false您is_null($customer)检查,因为它不是NULLDocs - 它是一个对象。

您可以从您的数据去耦输出逻辑有点解决这个问题:

$customer=getCustomerViaCellPhone($link,$cellPhoneNo); 
$hideCustomerInfo = false; 
if(!$customer) { 
    $error = 'No result found'; 
    $hideCustomerInfo = true; 
} 

,并在视图,则:

<div id="customerInfo" class="<?php echo $hideCustomerInfo ? 'hide' : ''; ?>"> 
.... 
</div> 

如果这不起作用,你可以验证$hideCustomerInfo标志已经存在于您的控制器中,以查看它是否包含您期望的值。此外,这简化了你的观点。

下一步,确保您的CSS是正确的,以及隐藏有这样一类的HTML元素。