Welcome Guest, Not a member yet? Register   Sign In
How do I use multiple form input so it can be used in a search feature
#1

[eluser]msank[/eluser]
Hi,

I am new here, and was needing to figure out how I can pass two values at the same time from two form input fields to a search feature... The values are hidden, and predetermined based on which page the user is on.

This is the form:
Code:
<?php
  echo form_open('/products/search') . "\n";
  echo form_hidden('search', '258') . "\n";//Value 1 to be used for searching
  echo form_hidden('search', '900') . "\n";//Value 2 to be used for searching
  echo form_submit('submit', 'Go') . "\n";
  echo form_close() . "\n";
?>

This is the controller:
Code:
function search() {

  $search = $this->input->post('search');
  $this->load->model('web_inventory_model');
  $inventory = $this->web_inventory_model->searchInventory($search);
  if($inventory !== FALSE) {
   $results = $inventory->result_array();
  } else {
   $results = 'There are no results';
  }
  
  $data = array(
   'search' => $search,
   'inventory' => $results,
   'title' => 'Search Results for "' . $search . '"',
   'locations' => $this->locations,
   'readMore' => $this->readMore
  );

  $this->load->view('search_view', $data);
}

This is the model:
Code:
function searchInventory($search) {
  $this->db->like('item_desc', $search);
  $this->db->or_like('item_no',$search);
  $this->db->or_like('manu_no',$search);
  $query = $this->db->get('Web_Inventory');
   if($query->num_rows > 0) {
    return $query;
   } else {
    return FALSE;
   }
}


At this point in time it is only using the last input value (900) to search. I would like it to use both input values at the same time, and display all of the results on the same page.

Any help is appreciated!
#2

[eluser]ELRafael[/eluser]
Code:
foreach ( $your_hidden_array as $hi ) :
  <input type="hidden" name="hiddens[]" value="<?php echo $hi; ?>" />
endforeach;

Your model
Code:
foreach ( $this->input->post('hiddens') as $var ) :
  $this->or_like('your_field', $var);
endforeach;
#3

[eluser]msank[/eluser]
[quote author="ELRafael" date="1338318389"]
Code:
foreach ( $your_hidden_array as $hi ) :
  <input type="hidden" name="hiddens[]" value="<?php echo $hi; ?>" />
endforeach;

Your model
Code:
foreach ( $this->input->post('hiddens') as $var ) :
  $this->or_like('your_field', $var);
endforeach;
[/quote]

Is this going to allow me to use the two different input values at the exact same time? And post results from both values on the same page?
#4

[eluser]ELRafael[/eluser]
the secret is hiddens[] <- see the [].

You'll have an array with all values.
I think that it was what you asked Wink
#5

[eluser]msank[/eluser]
It doesn't like that... I keep on getting an undefined variable error message. I am trying to pass both values at the same time to get one result.
#6

[eluser]ELRafael[/eluser]
I still believe I answered correct.

If you want to pass more than 1 value to the search model, you can do using array in your input tag.

Example:
Your html
Code:
&lt;input type="hidden" name="hiddens[]" value="1" /&gt;
&lt;input type="hidden" name="hiddens[]" value="2" /&gt;
&lt;input type="hidden" name="hiddens[]" value="3" /&gt;

Your Controller
Code:
$hiddens = $this->input->post('hiddens');
foreach ( $hiddens as $hi ) :
  echo $hi . '<br />';
endforeach;
This code will show you:
Code:
1<br />
2<br />
3<br />
#7

[eluser]msank[/eluser]
It's returning "Array" thus no results. Sad
#8

[eluser]ELRafael[/eluser]
Now I tested this function, here it's working

Your controller
Code:
class test_array extends CI_Controller {

private $data;

public function __construct()
{
  parent::__construct();
  $this->load->helper( array('form', 'url') );
}

public function index()
{
  if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
  {
   $hidden_fields = $this->input->post('test');
   if ( is_array($hidden_fields) )
   {
    foreach ( $hidden_fields as $field )
     printf('<p>Your field value is: %s</p>', $field);
   }
   printf('<p>Your text value: <strong>%s</strong></p>', $this->input->post('test_text'));
  }
  
  $this->load->view('test_array', $this->data);
}
}

Your view
Code:
<!DOCTYPE html>
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Test Array&lt;/title&gt;
&lt;meta http-equiv="content-language" content="pt-pt" /&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php echo form_open( current_url() ); ?&gt;
  &lt;input type="hidden" name="test[]" value="1" /&gt;
  &lt;input type="hidden" name="test[]" value="2" /&gt;
  &lt;input type="hidden" name="test[]" value="3" /&gt;
  
  &lt;input type="text" name="test_text" value="" /&gt;
  <button type="submit">Go Spider, go!</button>
&lt;?php echo form_close(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;
#9

[eluser]msank[/eluser]
K. Well your code works, but not for what I am trying to do... OR I'm am just blind and can't see what I am missing? Either way I am still getting "Array" as the thing being searched for, and it is not doing anything.

Thanks for your help, though!
#10

[eluser]ELRafael[/eluser]
If you getting "Array" as result, try to dump the var.

Assuming your variable is $xpto, try to do:
Code:
var_dump($xpto);

You should see your array's content. You'll probably need to put this array inside a foreach.

Try to put your code in pastebin or something like that for a precise help.

Smile




Theme © iAndrew 2016 - Forum software by © MyBB