How To Hide Items Which Has Null Value In The Databas

i have created a table which stores features of all the mobiles. i update the table and view the same. It works perfectly fine. Now, in the view page i do not want to see all the features. i want to display only those fields that has some value it i.e not null.

can anyone suggest me how to do this? how can i filter the null value fields.

your help is highly appreciated.

using example from http://www.yiiframework.com/doc/api/1.1/CDetailView, ‘visible’ property


$this->widget('zii.widgets.CDetailView', array(

    'data'=>$model,

    'attributes'=>array(

        'title',             // title attribute (in plain text)

        'owner.name',        // an attribute of the related object "owner"

        'description:html',  // description attribute in HTML

        array(               // related city displayed as a link

            'label'=>'City',

            'type'=>'raw',

            'value'=>CHtml::link(CHtml::encode($model->city->name),

                                 array('city/view','id'=>$model->city->id)),

        ),

        array(

            'label'=>'Phone',

            'name'=>'phone',

            'visible'=>'$data->phone', //<==here!

        ),

    ),

));

You need to add a condition ‘column IS NOT NULL’. If you post the code you currently use, to get the model record, I’ll show you how to change it.

I personally didn’t care for the look of the CDetailView and wanted something that saved space a little better (so that I could also use it for my list views) by using a 2 column (or better) layout. So I rolled my own ‘non standard’ solution which you may or may not prefer. For my purposes, removing null items was essential as well. I had already made a class in the components folder which had a bunch of display related static functions so I just needed to add a few more functions to that (which has later become 2 functions to handle the case of Boolean items which I do want to show (as false) even when not set; if you don’t care about that then you’ll just need the first function).




class MyHtml extends GxHtml { // If you aren't using Giix then extend from CHtml instead of GxHtml.

   public static function makeOptionalSpan($label, $result, $encode=true, $itm_class='itmlbl', $rslt_class='rslt', $rslt_sep=', ') {

      $lbl = $encode?MyHtml::encode($label):$label;

      if(!$result) return null; else if(is_array($result)) {

         $res='';

         if(!$isset($rslt_sep)) $rslt_sep=', ';

         if(is_array($rslt_sep)) {

            if(!count($rslt_sep)) $rslt_sep=array(', ');

            $sep_dx=0;

            foreach($result as $key=> $val) {

               if($val) $res.=($res?$rslt_sep[$sep_dx]:null).$val;

               if(isset($rslt_sep[$sep_dx+1]))

                  $sep_dx++;

            }

         } else {

            foreach($result as $key=> $val) {

               if($val) $res.=($res?$rslt_sep:null).$val;

            }

         }

         return " <span class=$itm_class>$lbl: <span class=$rslt_class>".

            ($encode?MyHtml::encode($res):$res)."</span></span>\n";

      } else

         return " <span class=$itm_class>$lbl: <span class=$rslt_class>".

            ($encode?MyHtml::encode($result):$result)."</span></span>\n";

   }

   public static function makeCheckboxSpan($label, $result, $encode=true, $itm_class='itmlbl', $rslt_class='rslt', $rslt_sep=', ') {

      $lbl = $encode?MyHtml::encode($label):$label;

      return " <span class=$itm_class>$lbl: <span class=$rslt_class>". ($result?'Yes':'No')."</span></span>\n";

   }

... MORE MyHtml code here ...

   public static function formatDateTime($datetime, $input_tz='UTC') { // This part is not really related but I use it below to show how I format dates

      if($datetime<1000) return null;

      return date('M d, Y g:i a', strtotime($datetime.' '.$input_tz));

   }

... REST OF MyHtml  follows ...

}



Then I added this CSS to my site style:




/* Styles for View\Index Pages */

div.view h5 {margin:0px; padding:2px 0px 3px 0px;}

span.itmlbl, span.itmlbl_wid {font-weight:bold; display: inline-block; min-width:200px; width:250px;}

span.itmlbl_wid {min-width:400px; width:100%;}

span.rslt {font-weight:normal;}



Finally, to create a view I use code like this (change the field names to what you are actually using):




echo "<div class=view>\n".

     "<h1>".MyHtml::link(MyHtml::makeOptionalSpan($data->getAttributeLabel('id'), $data->id, false),

	array('update', 'id'=>$data->id)). $data->employee->extendedFullNameLink."</h1>\n";

echo "<p>".

      MyHtml::makeOptionalSpan($data->getAttributeLabel('timein'), MyHtml::formatDateTime($data->timein)).

      MyHtml::makeOptionalSpan($data->getAttributeLabel('timeout'), MyHtml::formatDateTime($data->timeout)).

      MyHtml::makeOptionalSpan($data->getAttributeLabel('billableJob'), $data->billableJob->title).

      MyHtml::makeCheckboxSpan($data->getAttributeLabel('is_billable'), $data->is_billable).

      MyHtml::makeOptionalSpan($data->getAttributeLabel('client'), $data->client).

      MyHtml::makeOptionalSpan($data->getAttributeLabel('hours_type_id'), $data->hoursType).

      MyHtml::makeOptionalSpan($data->getAttributeLabel('exception_id'), $data->exception).

   "</p>\n</div>\n";



As you can see there are a few more extras in MyHtml::makeOptionalSpan that I didn’t cover but hopefully you can sort it out by looking at the code. Importantly, you are able to disable the automatic encoding using the third parameter (which is mostly needed when you want to make the label for the item a link) and using the forth parameter you are able to set a different class for the span (which can do a lot but for my purposes all that I use it for is to set the wide layout for that item which is good when I want to display an address for example which doesn’t fit well in the 2 column layout which I use by default). There are many things you can do to tweak this to your exact needs but I like this approach a lot better than using a widget for something as simple as this because I still have complete control over the design and don’t have to worry too much if I want to modify how it works (if I had modified the widget code then I’d have to make sure the others in my team knew not to update that package without updating that code as well). Since everything is in a function my IDE will give me hints as I type which is VERY helpful for this sort of thing (this is also why I’m not just sending the function an array like is done with widgets; less typing is not always easier). Don’t worry about the extra typing too much because this system lends itself well to copy\paste since each line describes the output you want for an entire field. If you put that view code in your _index.php you’ll be able to reuse the same code for both your listings and your views (putting only the extra code in your view.php file) but I’ll leave that as an exercise rather than show how I did it because that’d probably take a few dozen pages to type out due to all of the other goodies in that MyHtml class.

If you would rather use the CDetailView then I’d listen to Root Bear.

thanks.

below model,view,controller record…