Solved

PHP create a wildcard string if GET empty

Posted on 2011-09-15
4
367 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
perfect despite my bad question!!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now