0

I'm new to php, html, and mysql. I have a database with a username, product, and price they want to sell the product for. The code is for a checkbox form with a submit button that works like a price filter, that when submitted it's supposed to echo the username, product, and price that matches the corresponding checkbox. . If a user selects a checkbox labeled, "0-25" OR "100 & above", and clicks the submit button it supposed to echo all users that have items in that specific price range. I've looked online an tried to manipulate somewhat close to what I have. Could someone please help me out or give me insight on how to do this? Any help is appreciated

Here is my checkbox form.

<form action="pricefilter.php" method="post">
    <br><b>Filter By Price:</b><br><br> 
    <input type="checkbox" name="$0-$25[]" id="Price" value="0-25"/>&nbsp;$0-$25<br><br>
    <input type="checkbox" name="$25-$50[]" id="Price" value="25-50"/>&nbsp;$25-$50<br><br>
    <input type="checkbox" name="$50-$100[]" id="Price" value="50-100"/>&nbsp;$50-$100<br><br>
    <input type="submit" name="submit" value="Submit" />
</form>

Here is the code to my php file.

<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("xuswapuser");

$priceFilter = $_GET['priceFilter'];

 $filteredResponse = array ();
foreach($priceFilter as $range)
{
if($range == 025)
    {
        $query = "select * from Books where Price <= 25";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);



    }

    if($range == 2550)
    {
        $query = "select * from Books where Price >= 25 AND Price <=50";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
     if($range == 5075)
    {
        $query = "select * from Books where Price >= 50 AND Price <=75";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

     if($range == 75100)
    {
        $query = "select * from Books where Price >= 75 AND Price <=100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
     if($range == 100)
    {
        $query = "select * from Books where Price >= 100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

}

?>


 <html>
   <head>
   <title> 
XUSWAP 
</title>
<script type="text/javascript" src="sorttable.js"></script>
</head>
<body style="margin: 0; padding: 0;">
       <div id="header" style="background-color:#339900;height:157px;width:100%;position:relative;">                                      <!--header area-->
            <form id="headerForm" name="headerForm" method="Post" action="" align="right">
                  <input type="image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSN3XIAe50cjq5Cf91GbAywPkChmI5HOAqYxgmRN8F5OhW7I_RT5Q" alt="Placeholder" align="left" width="150" height="150">
                  <br><br><br>
                  <input type="text" name="searchInput"id="search" style="width:500px;"/>
                  <input type="submit" name="searchButton" id="searchButton" value="Search" />
                  <input type="submit" name="logoutButton" id="logoutButton" value="Logout"/>
                  <br><br><br>

            </form>
       </div> 
       <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
 <div id = "pageContent"></div>
 <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
<blockquote>
    <blockquote>
      <blockquote>
        <blockquote>
          <blockquote>
            <blockquote>
              <blockquote>
                <p>Books </p>
              </blockquote>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
        <blockquote>

                                <table class="sortable" width="940" height="52" border="0">
                                <tr>
                                  <td width="200" id="sortable">Username</td>
                                  <td width="253"> Product</td>
                                  <td width="220">Condition</td>
                                  <td width="249">Price</td>
                                </tr>
                                <?php do { ?>
                                  <tr>
                                    <td><?php echo $range['id']; ?></td>
                                    <td><?php echo $range['Product']; ?></td>
                                    <td><?php echo $range['Condition']; ?></td>
                                    <td><?php echo $range['Price']; ?></td>
                                  </tr>
                                  <?php } while ($range = mysql_fetch_assoc($sql)); ?>
                              </table>
mikez1
  • 685
  • 4
  • 9
  • 15
  • Usually a filter by price is done like a radio button option no? For example, why would a user be interested in 0-25$ and 100-200$ at the same time? – Dan Apr 18 '13 at 19:52
  • @Dan Thanks for responding. My group wanted it done like a checkbox, where you could select one or the either. I guess I should have been more specific. Let me make the edit. – mikez1 Apr 18 '13 at 19:54
  • the name of your input in html are all different... you have to make them all the same name (pricefilter) then CHANGe the VALUE or else each input is a new array the way you have it – Dan Apr 19 '13 at 16:24

2 Answers2

2

Ouch. :)

  1. What does IsChecked() function do? Is it defined, or you wanted isset() ?
  2. Don't name your fields with the $ dollar sign.
  3. Don't make them arrays, if they are not, you are using the group once, so it shouldn't have [] after it
  4. To access posted name field, you need $_POST superglobal, you cannot access it directly by its name:

-

 <input name="asd">

would be accessed if posted in PHP like:

$_POST['asd']

and the check if it's posted would be:

if(isset($_POST['asd']) { ...

By the convetion of using $row[] array, I assume you want to print a query results. In order to print results, you need to fetch them and iterate through them. In your way it would be:

 while ($row = mysql_fetch_assoc($sql)) { ...

However mysql_* functions are deprecated, and before you get used to them, better switch to mysqli_* or PDO libraries.

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
  • Big, IsChecked is supposed to check if that specific checkbox is checked. When I looked at examples online that's what they used but it didn't say anything about defining the function. I'm new to this so I'm not sure. Thanks for responding. – mikez1 Apr 18 '13 at 20:00
  • Maybe found the example you were looking for. They name the fields with [] in the end, because it's one and the same name, and creates array in PHP with the same name. http://www.html-form-guide.com/php-form/php-form-checkbox.html . Also the function IsChecked is created by the user (see almost the end of the topic) – Royal Bg Apr 18 '13 at 20:07
  • Thank you very much it's close to what I'm looking for and I'm getting some output, just not the output I'm looking for. – mikez1 Apr 18 '13 at 20:30
0

this is just simple code for you to follow, you can loop through all possibilities by putting the checked values into an array and performing the query through a foreach()

<input type="checkbox" name="priceFilter[]" value="025">
<input type="checkbox" name="priceFilter[]" value="2550">
// etc.

<?php
  $priceFilter = $_POST['pricFilter']; // this is now an array

  if(isset($priceFilter[025]))
  {
      $query25 = "select * from Books where Price <= 25";
  }
  if(isset($priceFilter[2550]))
  {
      $query2550 = "select * from Books where Price >= 25 AND Price <= 50";
  }
  //etc.

  // then run your query if it is set

  if(isset($query25))
  {
      $sql = mysql_query($query25);
  }

  // then do w/e you need to do then run subsequent possibily
?>

this is time consuming but i am just trying to get the point across to you on how to handle checkboxes in php

UPDATE if you want to do it with a foreach:

<?php
$priceFilter = $_GET['priceFilter'];
$filteredResponse = array ();
foreach($priceFilter as $range)
{
if($range == 025)
    {
        $query = "select * from Books where Price <= 25";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

    if($range == 2550)
    {
        $query = "select * from Books where Price >= 25 AND Price <=50";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

    // do this for all the conditions and you can save the results in an array of arrays
}

You now have an array of your MYSQL responses based on the selections from your checkbox form. Then just parse the information however you see fit.

i ALSO SUGGEST - before moving forward to use PDO instead of mysql_ as it is legacy code

take a gander at this for PDO use

Community
  • 1
  • 1
Dan
  • 3,755
  • 4
  • 27
  • 38
  • Thank you very much. For the foreach() would it be something like foreach($priceFilter as $query025){echo 'Username: '.$row['id']; echo '
    Price: '.$row['Price']; }
    – mikez1 Apr 18 '13 at 20:57
  • Ok thanks you very much. Would it be possible to echo the information into a table? – mikez1 Apr 18 '13 at 21:19
  • foreach($filteredResponse as $range) and within that foreach you can access the items from your DB by $range['YourRowName'] and echo it in a table of your choosing. - Select the green checkmark if this answers your original question for others to know as well – Dan Apr 18 '13 at 21:31
  • Yes it does thank you so much for helping me!!! Maybe I can use PDO in my next project but for this one we have to use mysql. – mikez1 Apr 19 '13 at 04:34
  • I tried echo the results into a dynamic table but not appears. Could you please show me where I went wrong. I made the edits in the original post. – mikez1 Apr 19 '13 at 08:36