问题
I have a popup dialog on first visit. you can set the postcode with a radius. onsubmit the cookies are set and shown on the main webpage:
You can try this here: http://kees.een-site-bouwen.nl
Now you see a searchform for searching factories in my database. my factories table looks like this:
Factories
- - - - -
idfactories
factoryname
postcode
place
telephonenumber
email
website
profile
adress
note i have a postcode field in my table. so how do i search in that table using the postcode i set using cookies?
my code for setting the cookie:
<form method="post" action="">
<input type="text" name="postcode" placeholder="postcode">
<select name="radius">
<option disabled selected>Afstand</option>
<option>5km</option>
<option>10km</option>
<option>15km</option>
<option>25km</option>
</select>
<br /><br />
<input type="submit" value="Opslaan">
<input type="hidden" name="submitted" value="true">
<input type="hidden" name="afstand" value="true" />
</form>
<?php
if(isset($_POST['postcode']))
{
setcookie('postcode', $_POST['postcode'], time() + (20 * 365 * 24 * 60 * 60));
setcookie('radius', $_POST['radius'], time() + (20 * 365 * 24 * 60 * 60));
header("location: {$_SERVER['PHP_SELF']}");
};
?>
<?php
if (isset($_POST["set_radius"])) {
}
?>
My searchquery in my model:
function get_search($match)
{
$this->db->like('Bedrijfsnaam', $match);
$this->db->or_like('Postcode', $match);
$this->db->or_like('Plaats', $match);
$this->db->or_like('Telefoonnummer', $match);
$this->db->or_like('Email', $match);
$this->db->or_like('Website', $match);
$this->db->or_like('Profiel', $match);
$this->db->or_like('Adres', $match);
$this->db->or_like('Categorie', $match);
$this->db->join('bedrijven', 'bedrijfcategorieen.idbedrijven = bedrijven.idbedrijven');
$this->db->join('categorieen', 'bedrijfcategorieen.idcategorieen = categorieen.idcategorieen');
$this->db->group_by('bedrijfcategorieen.idbedrijven', 'bedrijfcategorieen.idcategorieen');
$query = $this->db->get('bedrijfcategorieen');
return $query->result();
}
my controller function:
function searchresults()
{
$this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken' );
$this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten' );
$data['breadcrumbs'] = $this->breadcrumbs->get();
$match = $this->input->post('search');
$data['query'] = $this->bedrijven_model->get_search($match);
$this->load->view('views/header');
$this->load->view('views/searchresults', $data);
$this->load->view('views/footer');
$data['query'] = $this->bedrijven_model->bedrijven_tags();
}
i tried using $this->input->cookie('search');
but it did not work
回答1:
First I don't see where you save your cookie with key "swarch". I see postcode and radius only. Then you have to retrieve the cookie like
$this->input->cookie('postcode')
Anyway if you are setting the correct cookie. Are you loading the cookie helper?
$this->load->helper('cookie');
Cheers!
回答2:
I fixed it by simply adding the following code to my controller function
$match = $this->input->cookie('postcode');
Codeigniter is awesome!
来源:https://stackoverflow.com/questions/16010012/how-to-search-with-the-value-of-a-cookie-in-php-codeigniter