www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP form to display query

  1. #1
    Join Date
    May 2014
    Posts
    2

    PHP form to display query

    I have this code which displays all my products with a price range of $0-$25 great on left side of my webpage with the count number beside it like so.

    $0-$25 (12)
    Code:
         <?php 
        $sidebar_price ="";
        $price_sql = mysql_query("SELECT *,COUNT(sale_price) FROM products WHERE subcategory =      
        'Shirts' AND category = 'Women' AND (sale_price BETWEEN '$00.00' AND '$25.00') GROUP BY   
        (sale_price BETWEEN '$00.00' AND '$25.00')");
      
      
        $prCount = mysql_num_rows($price_sql);
        if ($prCount > 0) {
    	while($row = mysql_fetch_array($price_sql)){
    		
    		$sale_price = $row["sale_price"];
    		
    		$sidebar_price .= '<div id="sidebar"><label><form action="?"method="REQUEST"><input type="checkbox" name="sale_price" value="'.$sale_price.'" 
         onClick="submit(); return false" ' . (isset($_REQUEST['sale_price']) && $_REQUEST['sale_price']==$row['sale_price'] ? 'checked="checked"' : '') . ' /><font 
         size="-2"><a href="?sale_price='.$sale_price.'">$0-$25 ('.$row["COUNT(sale_price)"].')</a></font></form></label></div>';
    
    	 }
          }
         ?>
    How do I get it to work for the href and onclick?

    When I click on the href or onclick it will only display the products with prices for example just $24.99 and not all the products between $0.00-$25.00 like $24.99, $22.6, 19.35, etc..

    The problem might be is with the '.$sale_price.' I think

    If it is not clear enough I will try to explain further.

    Any ideas?
    Last edited by jedaisoul; 06-02-2014 at 02:25 AM. Reason: code tags added

  2. #2
    Join Date
    May 2014
    Posts
    75
    1) Please put your code in PHP tags [ php ] chode [ /php ].

    2)
    PHP Code:
    <form action="?"method="REQUEST"
    I'd change that to
    PHP Code:
    <form action="" method="post"
    or
    PHP Code:
    <form action="" method="get"
    Make sure you put a space between defining the action and the method.
    REQUEST is not a form submission method. $_REQUEST['whatever'] will read both POST and GET data and choose whichever exists.

    3) Your SQL may benefit from naming your counts too.
    PHP Code:
    SELECT *, COUNT(sale_price) AS product_count FROM products 
    4) It would be useful to see the output HTML too

    5) How do I get it to work for the href and onclick?
    Still a little confused about what you want and what you're getting.

  3. #3
    Join Date
    May 2014
    Posts
    1,026
    Well... for starters if you want to be doing comparison of values you should be storing them as numbers, not strings -- the only way you could pass a $ is if it's a string, making your code gibberish. If you want to do those comparisons, you've got the wrong data types... You want it to do $, you show $ and use number_format on the output, NOT the values!

    Though if we could see the structure of that table it would help so we know what your data types even are that might help a lot.

    That said, I was going to read you the riot act about the use of mysql_ like it's still 2006... since we've been told for EIGHT YEARS to stop using them, and why there have been giant red warning boxes in the manual warning of their impending doom and gaping security flaws -- but then I saw the presence of a FONT tag and just went "REALLY?!? [b]REALLY?!?!?[b]... really...

    That form is also not the greatest markup -- in fact it makes no sense. I have to ask why are you using an anchor on this in the first place, much less onclick... or slow memory wasting string addition instead of echo, or LABEL around a form (which is nowhere NEAR where it belongs, LABEL goes INSIDE a form pointing at a INPUT, SELECT or TEXTAREA with a FOR attribute)... much less method="request", which is NOT a valid property for METHOD.

    Valid METHOD properties for FORM: get, post... that's it!

    It also doesn't make any sense to select * and a count at the same time. That's not going to return anything resembling a meaningful value.

    I'm not really getting why you even have a form, or a checkbox, much less what it has to do with the anchor... That markup is incomprehensible nonsense that has nothing to do with how HTML even works; your Query is malformed and passing invalid values, and on the whole none of it makes the least bit of sense. I'm not even sure what it is you're trying to accomplish with that.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    May 2014
    Posts
    2
    Thanks for the responses. As for the GET and POST, the only thing that worked when I first made the code was REQUEST so I kept it but looks like it will work with GET so thanks. Overall beside that and besides FONT those aren't the problem.

    The code is for a clothing website which displays price ranges in the left side bar. I am trying to do a price range to read my sale_price column automatically. So say I have 24.99, 22.54, 19.73, 13.65, right now it shows up like this:

    $0-$25 (4)

    which I want but I need the href and checkbox, once clicked, to properly display all 4 products with these sale_prices. Right now it is just displaying 24.99 and not the other 3. Hope this explains it further.

  5. #5
    Join Date
    May 2014
    Posts
    1,026
    Quote Originally Posted by crowds40 View Post
    Thanks for the responses. As for the GET and POST, the only thing that worked when I first made the code was REQUEST so I kept it but looks like it will work with GET so thanks. Overall beside that and besides FONT those aren't the problem.
    There is no REQUEST in HTML... though are you thinking $_REQUEST in PHP?

    PHP $_GET == HTML method="get"

    PHP $_POST == HTML method="post"

    PHP $_REQUEST == PHP array_merge($_GET, $_POST)

    Generally $_REQUEST is frowned upon as if you are using postData you are doing so as to make it so value passed in the URL is NOT used, so as to avoid cross site scripting exploits; using the PHP array that indexes both means the value could be passed in the URL or in the header, opening a security hole.

    Really though if you are using method="get" it just doesn't matter if you use $_GET or $_REQUEST -- but if you are using method="post" -- USE $_POST

    Hope that explains it for you.

    I'll try to take a deeper look at what you are doing -- but could we see the table layout you are running queries against? Specifically what data types are defined?
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles