CodeIgniter 3中如何展示用户提交的非空搜索查询参数
Question
I'm a beginner PHP developer learning CodeIgniter 3, and I built a search form that uses the GET method with four optional criteria. When submitted, the parameters show up in the URL query string like this:
results/?q=law&coll_id=2&start_year=1200&end_year=1500I want to display the user's search criteria clearly on the results page, like this example:
Hello, you searched for the following; Search Term: `law` Collection ID: `2` Start Year: `1200` End Year: `1500`When I run
var_dump($this->input->get()), I get this output:array(4) { ["q"]=> string(3) "law" ["coll_id"]=> string(1) "2" ["start_year"]=> string(4) "1200" ["end_year"]=> string(4) "1500" }The catch is users can search with just one (or any combination) of the criteria—they don't all have to be filled in. I'm wondering how to implement this display feature. Do I need to loop through the array and only show non-empty values?
Answer
Absolutely, looping through the array and checking for non-empty values is exactly the right approach here—perfect for handling optional criteria like yours. Let's break this down into simple, beginner-friendly steps:
1. Grab and sanitize your GET parameters
First, use CodeIgniter's built-in input class to safely retrieve all GET parameters (it automatically sanitizes the data to avoid security risks):
$search_params = $this->input->get();
2. Create a friendly label mapping
Your URL parameter keys (q, coll_id) are technical, so we'll map them to user-friendly labels using an associative array. This makes it easy to update labels later without rewriting the entire display logic:
$param_labels = [ 'q' => 'Search Term', 'coll_id' => 'Collection ID', 'start_year' => 'Start Year', 'end_year' => 'End Year' ];
3. Loop through parameters and display only non-empty ones
Now we'll loop through our label mapping, check if the corresponding GET parameter exists and isn't empty (including trimming whitespace to avoid showing blank inputs), then display it:
// First, check if any search parameters were submitted if (!empty($search_params)) { echo "<p>Hello, you searched for the following;</p>"; foreach ($param_labels as $key => $label) { // Check if the parameter exists and has a non-empty value if (isset($search_params[$key]) && !empty(trim($search_params[$key]))) { // Use CodeIgniter's escaped get method to prevent XSS attacks echo "<p>{$label}: `{$this->input->get($key, TRUE)}`</p>"; } } } else { // Optional: Show a message if no search criteria were entered echo "<p>No search criteria were entered.</p>"; }
Key Tips for Beginners
- Security First: Using
$this->input->get($key, TRUE)enables HTML escaping, which protects your site from XSS attacks—always do this with user input! - Trim Whitespace: The
trim()function ensures we don't display values that are just empty spaces (e.g., if a user accidentally hit spacebar instead of typing a term). - Flexible Display: This setup automatically adapts to whatever combination of criteria the user uses—no need to write separate code for each possible scenario.
Example Partial Search Output
If a user only submits coll_id=5 and start_year=1300, the output will look like this:
Hello, you searched for the following; Collection ID: `5` Start Year: `1300`
内容的提问来源于stack exchange,提问作者TheOrdinaryGeek

