Jump to content

In a PHP based url, can a search term use a wildcard?

Featured Replies

I've got a report function in our local training system, where we enter the manager ID of a group, then the course code. This will pull who in that managers group has completed that course code and who has not.

 

I'm trying to find a way to replace putting a (or multiple) specific course code (or codes) with a "begins with" logic that will pull all course codes beginning with a certain term.

 

However, I've messed around a bit with wildcards (like * and %), but either the wildcards don't work, or my syntax is wrong.

 

 

 

An example of an actual report url for a specific manager and a specific course is as follows:

 

http: //OurTrainingSite/reports/mgr_drill_multi.php?mgrid=IDgoesHere&QS_crse_codes=ExactCourseCode

 

 

What I want is a way to do something similar, but with "QS_crse_codes" begins with "CodePrefix."

 

Any ideas of syntax to try?

 

 

 

If I can't get it to work this way, I'll just ask our DEV team to create a new report type with this logic, but my hope is that I can leverage existing functionality without the need for a production change request. My skills are limited, and this may not even be possible, but I figured it could not hurt to ask.

 

Any and all ideas are welcome. Cheers.

  • Author

Cool. I sort of figured that, and it makes sense. I can pull the data myself from the database on the backend, but I will need to get a new end-user report created to do what I described above.

 

 

Thanks guys.

iNow, the way to do it is set up a condition in the code itself. For instance:

 

URL:

http://www.whatever.com?search=term&type=1

 

Code:

<?php
switch ($_GET["type"]) 
{
    case 1: //wildcard both sides:
         $sTerm = '%' . $_GET['term'] . '%';
         break;
    case 2: //wildcard before only:
         $sTerm = '%' . $_GET['term'];
         break;
    case 3: //wildcard after only:
         $sTerm = $_GET['term'] . '%';
         break;
    default:
    case 4: //no wildcard:
         $sTerm = $_GET['term'];
         break;
}

//do whatever in the SQL search:
$sql = "SELECT * FROM tbl WHERE field=".$sTerm;

?>

 

You can play with the search terms like that. If your intention is to ALWAYS add a wildcard, then just tell the code to automatically add it (instead of switching) and use the new variable for the search.

 

 

Hope that helps :)

 

~moo

  • Author

Nice. I'll give that a whirl after the weekend. Thanks, moo! :)

Nice. I'll give that a whirl after the weekend. Thanks, moo! :)

 

No problems :)

 

I just noticed that I didn't FULLY answer it though.. you should probably avoid the switch, since you want to use only "before" thing, but I guess you can figure it out by only using the third case.

 

also, don't forget to use some security anti-SQL injection in there, because this is still a GET command. So, to summarize, your code should look something like this:

 

$baseTerm = "%" . [color=red]mysql_real_escape_string([color=Black]$_GET["term"][/color])[/color];
$sql="SELECT * FROM table WHERE field LIKE '".$baseTerm."'";

// etc

 

 

Don't forget the mysql_real_escape_string thing, it's VERY important if you're dealing with direct-field injections to the SQL database :)

 

g'luck :)

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.