-1

A little help guys?

when i input list and slist the query works. but if i only input list only it doesnt show the query for finding datas with category = list, same goes for the slist if i input slist only it doesn't show the query for finding datas with subcategory = slist.

for example if i input Toys on list and boy on slist the query executes and shows the products thats category = Toys and subcategory = boys but if i only input toys in list and i didnt input anything on slist it doesnt shows the query for products that only shows toys the same thing happens if i only input slist

it only works if i input them both.. i need to make a search bar that can search for both, list only and slist only. Sorry for my bad english.

<?php
$category = $user_data['categoryquery'];

        $per_page = 15;

        // figure out the total pages in the database
        if (isset($_POST['submit']))
        {
            if($_POST['list'] != '' && $_POST['slist'] != '')
            $list = $_POST['list'];
            $slist = $_POST['slist'];
            $result = mysql_query("SELECT * FROM products WHERE category = '$list' AND subcategory = '$slist' ORDER BY date_added");

            }else{
            if($_POST['list'] == '' && $_POST['slist'] != '')
            {
                $slist = $_POST['slist'];
                $result = mysql_query("SELECT * FROM products WHERE subcategory = '$slist' ORDER BY date_added");
            }else{
            if($_POST['list'] != '' && $_POST['slist'] == '')
            {
                $list = $_POST['list'];
                $result = mysql_query("SELECT * FROM products WHERE category = '$list' ORDER BY date_added");
            }else{
        $result = mysql_query("SELECT * FROM products");
            }
            }
            }
3
  • 1
    I would indent all of the code, and add { } to all of my if/else statements, to better follow the logic of the code. Commented Mar 17, 2014 at 18:09
  • Are you sure you have all of your { paired with }? Shouldn't there be a { following if($_POST['list'] != '' && $_POST['slist'] != '')? Commented Mar 17, 2014 at 18:11
  • i tried adding the { and } there but the $result near the end of the script won't work and a lot of errors appeared i'll try to edit my indention sorry bout that Commented Mar 17, 2014 at 18:17

2 Answers 2

2

you are missing a { there if($_POST['list'] != '' && $_POST['slist'] != '') ****HERE****

correct this and fix your indents, it will help a lot

Sign up to request clarification or add additional context in comments.

1 Comment

When i Added the { and } several errors appeared and it didn't show the $result near the end of the script i posted when i reloaded the Browser
0

Can't run php right now to test this, but when you only input one of the POST variables, the other may be coming up as a null value instead of an empty string.

If that's the case, refer to the simple fix here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.