1

Using Codeigniter 3 and PHP I have developed a search form using the get action. The form has four possible search criteria and once submitted, these appear as query strings in the URL as follows;

results/?q=law&coll_id=2&start_year=1200&end_year=1500

On the search results page I would like to display these 'search terms' to the users, showing exactly what they have searched for.

For example;

Hello, you searched for the following;

Search Term: `law` 
Collection ID: `2`
Start Year: `1200` 
End Year: `1500`

When I var_dump($this->input->get()) this is what I see;

array(4) { ["q"]=> string(3) "law" ["coll_id"]=> string(1) "2" ["start_year"]=> string(4) "1200" ["end_year"]=> string(4) "1500" }

Note, it's possible to search using only one criteria, not all four are required therefore may be null.

How can I display the search terms to the user like this?

Presumably I need to loop through the array to check if they're not null, then do something else?

Any advice is appreciated. I'm a beginner at PHP and teaching myself

1
  • 1
    You've already answered you question. Commented May 9, 2018 at 15:05

2 Answers 2

1

Gather all the GET items at once and check for each expected index, echo if index is found.

$search_items = $this->input->get();

if(isset($search_items['q']))
{
    echo "Search Term: " . $search_items['q']."<br>";
}

if(isset($search_items['coll_id']))
{
    echo "Collection ID: " . $search_items['coll_id']."<br>";
}

if(isset($search_items['start_year']))
{
    echo "Start Year:: " . $search_items['start_year']."<br>";
}

if(isset($search_items['end_year']))
{
    echo "End Year: " . $search_items['end_year']."<br>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Since $this->input->get()/post()/etc returns false if not set, you can easily check and/or access get variables.

if($this->input->get('q'))
{
   echo $this->input->get('q');
}

if($this->input->get('coll_id'))
{
   echo $this->input->get('coll_id')
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.