Jump to content

# help with \$_GET

## Recommended Posts

Hello to everyone.I have a sort of search form on my. it consists of three form items.on the search.php i have a select statement with variables (i get them from \$_GET variables coming from my form) included.something like this:\$ctg = \$_GET['frm_category'];\$type = \$_GET['frm_type'];\$city = \$_GET['frm_city'];

`" SELECT * FROM \$table1, tbl_users WHERE category = '\$ctg' AND type = '\$type'AND city = '\$city' ORDER BY..  "`

all in all, everything works just fine, UNLESS one (or several fields) not selected (their values being passed with NO VALUE).how can i avoid using ONE or MORE variables in my SELECT statement if no value has been passed from my form?i managed to do so using the IF statement(if VARIABLE empty --STATEMENT WITHOUT THAT VARIABLE-- else --STATEMENT WITH THAT VARIABLE INCLUDED)but, this seems to be complicated, so i wonder if there's any other way?I'm pretty new to PHP so.. :)Thanks in advanceDenis K., Bosnia H.

##### Share on other sites
i managed to do so using the IF statement(if VARIABLE empty --STATEMENT WITHOUT THAT VARIABLE-- else --STATEMENT WITH THAT VARIABLE INCLUDED)
That's the best way to do it, if you can find something that is less complicated then a basic if statement, please let the programming community know.
##### Share on other sites

Maybe there should be a 'why not' statement in PHP?! That would be fun! Uh. I literally just did something like your doing. I put the 'LIKE' statements into an array so :

`\$likes= array();foreach (\$values as \$k=>\$v) {  if (isset(\$_GET['this'])) {	\$this= \$_GET['this']; 	likes[]= "word LIKE '\$this'";  }  elseif ...}`

then in the query you use implode(" AND ", \$likes)or you can change to OR (can't mix with this method) if the likes are optionalso something like

` \$sql= "SELECT col FROM table WHERE ".implode(" AND ", \$likes);`

This method I suppose isn't simpler as you're using more loops and arrays. I suppose you can't get simpler than if...else like justsomeguy says! But I like my method Maybe it will be useful for you!

##### Share on other sites

Right. This is about as easy as it gets:

`\$sql = "SELECT * FROM \$table1, tbl_users ";\$where = "";if (\$ctg != "")  \$where .= " WHERE category = '\$ctg'";if (\$type != ""){  if (\$where == "")	\$where .= " WHERE ";  else	\$where .= " AND ";  \$where .=  "type = '\$type'";}if (\$city != ""){  if (\$where == "")	\$where .= " WHERE ";  else	\$where .= " AND ";  \$where .=  "city = '\$city'";}\$sql .= \$where . " ORDER BY ...";`

It might be a lot of code (I format my code for readability over compactness), but it is easy to read and it will execute quickly. If you want code that is more compact, you can replace this:

`  if (\$where == "")	\$where .= " WHERE ";  else	\$where .= " AND ";`

With this:

`\$where .= (\$where == "" ? " WHERE " : " AND ");`

to get this:

`\$sql = "SELECT * FROM \$table1, tbl_users ";\$where = "";if (\$ctg != "")  \$where .= " WHERE category = '\$ctg'";if (\$type != "")  \$where .= (\$where == "" ? " WHERE " : " AND ") . "type = '\$type'";if (\$city != "")  \$where .= (\$where == "" ? " WHERE " : " AND ") . "city = '\$city'";\$sql .= \$where . " ORDER BY ...";`

##### Share on other sites

well guys, it seems LULZIM gave me the most suitable solution.yes, i had to do some modification in order for the code to work (i had my categories' id's numbered from 1 to 34) so now i changed them to end in 0 (zero) so that Lulzim's condition LIKE %\$category would work if there's no value passed from the form (since there's no category id = 0)--thanks to all of you who wanted to help.see you next timep.s. i have a new topic in PHP category so if ur interested in it, take a look!Denis

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
×
• Create New...
{"ft_lang_label":"__label__en","ft_lang_prob":0.82587147,"math_prob":0.792372,"size":1175,"snap":"2021-04-2021-17","text_gpt3_token_len":287,"char_repetition_ratio":0.08198121,"word_repetition_ratio":0.0,"special_character_ratio":0.25957447,"punctuation_ratio":0.12765957,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97923225,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-23T13:17:51Z\",\"WARC-Record-ID\":\"<urn:uuid:19ff0e6d-cb3f-4efb-919e-85b341874cfa>\",\"Content-Length\":\"128411\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e877d5e9-efff-4013-8159-a9fdeba9ca7c>\",\"WARC-Concurrent-To\":\"<urn:uuid:9346a08a-af18-4005-8aa9-5b4b05fe0021>\",\"WARC-IP-Address\":\"52.85.132.122\",\"WARC-Target-URI\":\"https://w3schools.invisionzone.com/topic/12973-help-with-_get/?tab=comments\",\"WARC-Payload-Digest\":\"sha1:YDXTSQVTYRT76PV6FND7A5VIBMO5NL6W\",\"WARC-Block-Digest\":\"sha1:WN3KUFTFQPQSBMRERZWJFJIQDZRPF6JT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618039594808.94_warc_CC-MAIN-20210423131042-20210423161042-00223.warc.gz\"}"}