codginator search query

I want to make search query(codginator)from select box mean when i push submit button filter do his work .

but this giving me error.

Undefined variable: dt
Undefined variable: val

Invalid argument supplied for foreach()

IF ANY ONE CAN PLEASE GUIDE WHERE I M WRONG.
model.php
controler.php
VIEW.php
LVL 1
Question Mark?Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You cannot rely on undefined variables in PHP.  You must test to see if they exist and contain useful values before you use them in a script.
http://php.net/manual/en/function.isset.php

In case you're new to PHP and want to get a foundation (necessary for working on PHP internals of CodeIgniter or any other framework) this article might help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
Marco GasiFreelancerCommented:
I think you get that error when you load the page before to submit your query: this means that the chek

if($this->input->post('submit'))

fails and then the variable $dt['val'] is never filled and when the view is loaded it raises the error. Put a check in the View:

<?php 
if (isset($val))
{
foreach ($val as $va){?>
<?php echo  $va['namehost'];?><br>
<?php  }
}?>

Open in new window



And the error will go away :-)
Question Mark?Author Commented:
THANKS SIR ,
  FOR YOUR SUPPORT BUT I THINK THAT I M MAKING PROBLEM MOST ON
MODEL (I THINK THATS NOT EXACT QUERY AS CI WANT)
THEN SMALL ON CONTROLLER.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Marco GasiFreelancerCommented:
orry, have you tried to do what I said?
Marco GasiFreelancerCommented:
About your model, here you'll find the info you need:https://ellislab.com/codeigniter/user-guide/database/active_record.html


 public function mgetclinds($a,$b,$c,$d,$e,$f,$g,$h,$i,$j)
    {
         $array = array(
              'state' => $f,
              'city' => $g,
              'area' => $h, 
              'cate' => $i,
              'for' => $j
         );
        $this->db->like($array);
        $query=$this->db->get($a,$b,$c,$d,$e);
        return $query->result_array();
    }?>

Open in new window

Question Mark?Author Commented:
Oh sorry
yes sir i apply checck method from this 2 error are gone

but after pushing submit button

these error are come

* all errors on same page

really thanks sir that you are helping me.
error1.png
error2.png
Marco GasiFreelancerCommented:
Ok, I'd have to look at your code with more attention. It is inconsistent. Look at this: your model method accept 10 parameters
public function mgetclinds($a,$b,$c,$d,$e,$f,$g,$h,$i,$j)

Open in new window

but you pas it just 6
$dt['val']=$this->demomod->mgetclinds('reg',$selected1,$selected2,$selected3,$selected4,$selected5);

Open in new window

And 'reg' param what is? It is never used in model method so it is uneeded.
Let's summarize: you want look for records which have area, city, state etc LIKE the ones submitted by the user, right? Okay rewrite code this way and see if it makes sense:

Controller:
<?php  //ON CONTROLLER//
public function extra1()
        {
          if($this->input->post('submit'))
                {
                    $selected1=$this->input->post('state');
                    $selected2=$this->input->post('city');
                    $selected3=$this->input->post('area');
                    $selected4=$this->input->post('cate');
                    $selected5=$this->input->post('for');
              $dt['val']=$this->demomod->mgetclinds($selected1,$selected2,$selected3,$selected4,$selected5);
          }  
            
          $this->load->view('extra1',$dt); 

       }
    ?> 

Open in new window


Model:
 public function mgetclinds($state,$city,$area,$cate,$for)
    {
         $array = array(
              'state' => $state,
              'city' => $city,
              'area' => $area, 
              'cate' => $cate,
              'for' => $for
         );
        $this->db->like($array);
        $query=$this->db->get('city', 'state', 'area', 'cate', 'for');
        return $query->result_array();
    }?>

Open in new window

I don't grant this one work, but it's more consistent. Notice I've used significant names for variables: it's a superflous diffculty to use meaningless letters: if you type a bit more, you can live better whean you have to debug your code :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question Mark?Author Commented:
THANKS SIR
FOR YOUR GREAT CONTRIBUTION. THIS WILL SOLVED MY PROBLEM.

BUT THERE IS AN (ERROR OF UNDEFINED VARIABLE dt.)
SO I HIDE THIS FROM Error Reporting (0).

THEN THIS IS MUCH FOR ME THAT NOW RESULTS ARE COMING.

AGAIN thanks SIR.
UNDEFINED-VARIABLE.png
Marco GasiFreelancerCommented:
I strongly discourage you from hiding errors, warnings and even notices: if there is an issue, then solve it now. Hiding it just grant you to accumulate an incredible amount of small issues which, all together, can make your life a nightmare.
That error is probably because the creation of $dt variable depends on the submission of values. If the user doesn't submit anything, that is if you access the page directly typing in the adddress bar of your browser www.yourdomain.com/controller_name/extra1, then the $dt array doesn't exsist, but you pass it to the view anyway: from here the warning. Do this instead:
<?php  //ON CONTROLLER//
public function extra1()
        {
          $dt['val'] = '';
          if($this->input->post('submit'))
                {
                    $selected1=$this->input->post('state');
                    $selected2=$this->input->post('city');
                    $selected3=$this->input->post('area');
                    $selected4=$this->input->post('cate');
                    $selected5=$this->input->post('for');
         
        
        $dt['val']=$this->demomod->mgetclinds('reg',$selected1,$selected2,$selected3,$selected4,$selected5);
          // echo "$selected1,$selected2,$selected3,$selected4,$selected5";
   
          }  
            
          $this->load->view('extra1',$dt); 

       }
    ?> 

Open in new window

Question Mark?Author Commented:
Yes sir I do this as you guide me.
Question Mark?Author Commented:
Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.