Solved

PHP create a wildcard string if GET empty

Posted on 2011-09-15
4
372 Views
Last Modified: 2012-06-22
Hi,

I have this chunk of PHP that I needed to create a wildcard % to be used in a query if no $_GET was found:


 
<?php
if (!isset($_GET["Sub_Category"]) || empty($_GET["Sub_Category"])) {
     $scat = '%';
	} else {
	 $scat = ($_GET['Sub_Category']);
}
?>

Open in new window


Any thoughts
Thanks
Tony
0
Comment
Question by:tonypearce
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
Finec earned 500 total points
ID: 36542112
If you're looking for a shorter solution:

$scat = empty($_GET['Sub_Category']) ? '%' : $_GET['Sub_Category'];

Open in new window


Or what's the question? :)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36542119
What is the question?  The code posted here will set $scat to be a string of length=1 and content=% if the URL does not have the Sub_Category argument.  Is that what you want?
0
 

Author Closing Comment

by:tonypearce
ID: 36542144
perfect despite my bad question!!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36542171
Just a thought.. You might want to leave the questions open a little longer and maybe give a little more expository content with the question.  For example, I sense that you might be wanting to use the $scat variable in a data base query (this is a common design pattern).  Before you do that you might want to post a question here about something like, "What are the well-known best security practices that can help me keep my data base from getting destroyed or stolen by hackers?"
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question