Results 1 to 5 of 5

Thread: PHP form to display query

  1. #1
    Join Date
    May 2014

    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)
        $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
    1) Please put your code in PHP tags [ php ] chode [ /php ].

    PHP Code:
    <form action="?"method="REQUEST"
    I'd change that to
    PHP Code:
    <form action="" method="post"
    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
    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.

  4. #4
    Join Date
    May 2014
    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
    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?

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12157 seconds
  • Memory Usage 2,895KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (4)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates