Welcome Guest, Not a member yet? Register   Sign In
problem with 0 int from post request
#1

[eluser]Ngulo[/eluser]
hi guys i have:

view
Code:
<select name="public">
   <option value="">-</option>
    <option value="1">1</option>
   <option value="0">0</option>
   </select>
  &lt;?php echo $this->session->userdata('admin_events_filter_public');?&gt;
   &lt;?php $this->output->enable_profiler(TRUE);?&gt;

controller

Code:
$sessionFilter=array(
             'admin_events_filtering'=>'1',
             'admin_events_filter_day_from' => $this->input->post('day_from',true),
             'admin_events_filter_month_from' => $this->input->post('month_from',true),
             'admin_events_filter_year_from'=>$this->input->post('year_from',true),
             'admin_events_filter_day_to'=>$this->input->post('day_to',true),
             'admin_events_filter_month_to'=>$this->input->post('month_to',true),
             'admin_events_filter_year_to'=>$this->input->post('year_to',true),
             'admin_events_filter_public'=>"".$this->input->post('public'),
             'admin_events_filter_date_from'=>$date_from,
             'admin_events_filter_date_to'=>$date_to,
             'admin_events_filter_text' => $text,
             'admin_events_filter_title'=>$title,
             'admin_events_filter_order'=>$order
             );

              $this->session->set_userdata($sessionFilter);

              $this->load->model('admin_events');

            $populateevents = $this->admin_events->populate(
                    $this->session->userdata('admin_events_filter_date_from'),
                    $this->session->userdata('admin_events_filter_date_to'),
                    $this->session->userdata('admin_events_filter_text'),
                    $this->session->userdata('admin_events_filter_title'),
                    $this->session->userdata('admin_events_filter_order'),
                    $this->session->userdata('admin_events_filter_public')
                    );


model

Code:
function populate($date_from,$date_to,$text,$title,$order,$public){
    $this->db->select('
        ci_events.id,
        ci_events.public,
        ci_events.event_date,
        ci_events_details.title,
        ci_events.insert_datetime,
        ci_events.update_datetime'
        );
    $this->db->from('ci_events');
    $this->db->join('ci_events_details','ci_events.id = ci_events_details.id_event');
    $this->db->where('ci_events_details.lang','it');
    if($title){

        $this->db->like('ci_events_details.title',$title);

    }
    if($text){

        $this->db->like('ci_events_details.text',$text);

    }
     if($date_from){

         $this->db->where('ci_events.event_date >=',$date_from);
    }
    if($date_to){

        $this->db->where('ci_events.event_date <=',$date_to);
    }

    if($public){

        $this->db->where('ci_events.public',$public);
    }

    if($order)
    {

        $this->db->order_by('ci_events.event_date '.$order);
    }
    else{

        $this->db->order_by('ci_events.event_date DESC');
    }
    $query = $this->db->get();
    return $query->result();

ok enabled profiler and i see query in plain text

when i pass $this->input->post('public'); to session then to model for query results:

if public = 1 query it's ok
if public = '' query it's ok
if public = 0 query NOT ok cause seems in session and post array it exists as 0 but in query it doesn't comes up in WHERE condition Sad

does anyone have ideas?

thanks
#2

[eluser]InsiteFX[/eluser]
Because 0 is FALSE which will not print out anything!

The way around it is to use an enum field or BOOL!

InsiteFX
#3

[eluser]Ngulo[/eluser]
but my public db field is ENUM('1','0')

Sad

if i print out $this->input->post('public'); it shows me 0
if i print out session data then it shows me anyway 0

really can't understand Sad
#4

[eluser]InsiteFX[/eluser]
As I said above when it comes back it thinks it is FALSE!

Code:
echo '0';     // works displays 0
echo FALSE;   // displays nothing!

Do a search on checkbox and you will see what I am talking about!

InsiteFX
#5

[eluser]TWP Marketing[/eluser]
Ngulo,
I think the input function post() is returning an integer value, not a boolean or enum value.
test it with some code in your view:
Code:
$test = $this->input->post(‘public’);
if($test === '0'){ echo "string zero";}
if($test === 0){ echo "integer zero";}
if($test === ''){ echo "empty string";}
please note the use of "===" as the comparison operator, it must match value AND field type.




Theme © iAndrew 2016 - Forum software by © MyBB