0

I am having trouble creating a drop down that can sort my search results, any help is welcome. I have tried a couple of different methods that I have found from the internet but so far none have worked (I have no doubt in my mind that it is user error as others seem to have had success using those methods.) I apologise in advance for my being new at coding.

I have put my new code into this post if you could assess it and explain to me why it is not working I would be eternally grateful to you.

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title>Jenewan Pets</title>
        <link rel="icon" type="image/gif/png" href="img/Logo.png"/>
        <link rel="stylesheet" type="text/css" href="css/main.css" />
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
      </head>
      <body>
        <body background="img/bckgrnd.png">
        <div id="container">
            <div id="header">
              <img src="img/Header.png" style="position: absolute; top: 0px;"/>
              <h1>
              <img src="img/logo.png" alt="logo" height="100px" width="100px"  border="0px" style=position:relative; />
              <div class="box">
                <div class="container-1">
                    <form method="get" action="searchresults11.php" id="searchForm">
                    <input type="text" id="searchBox" name="q" placeholder="Let's Sniff it Out...">
                      <div class="search-icon">
                        <img class="search-icon" img src="img/PawSearch.png" width="18" height="18" alt="search icon" />
                      </div>
                </div>
              </div>
            </h1>
        </div>
    <div id="content">
          <div id="nav">
          <ul>
                  <li><a class="selected" href="/index.php">Home</a></li>
                  <li><a href="dogs.php">Dogs</a></li>
                  <li><a href="">Cats</a></li>
                  <li><a href="birds.php">Birds</a></li>
                  <li><a href="">Reptiles/Exotic</a></li>
                  <li><a href="">Aquatics</a></li>
              </ul>
            </div>
          <div id="Results">
            <h2>We sniffed and we found...</h2>
            <?php
            $con=mysqli_connect("localhost","root","","catalog");
            // Check connection
            if (mysqli_connect_errno())
            {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

            if(isset($_GET['q'])){
              $searchq = $_GET['q'];
              }
            $q = mysqli_query($con,"SELECT * FROM final_dog_catologue_full where
            keywords LIKE '%$searchq%' OR brand LIKE '%$searchq%' OR name LIKE '%$searchq%' LIMIT 0, 40") or
            die(mysqli_error("Sorry we lost the scent..."));

            echo "<table border='2'>
            <tr>
            <th>  </th>
            <th id='Name_Header'>Item Name</th>
            <th>Brand</th>
            <th id='Price_Header'>Price</th>
            </tr>";

            while($row = mysqli_fetch_array($q))
            {
            echo "<tr>";
            echo "<td><img src='img/".$row['Picture']."'></td>";
            echo "<td headers='Name_Header'>" . $row['Name'] . "</td>";
            echo "<td headers='Brand'>" . $row['Brand'] . "</td>";
            echo "<td headers='Price_Header'>£" . $row['Retail_Price_With_Delievery'] . "</td>";

            echo "</tr>";
            }
            echo "</table>";


            mysqli_close($con);
    ?>

    <script type="text/javascript">

    var table = $('table');

       $('#Name_Header, #Price_Header')
           .wrapInner('<span title="sort this column"/>')
           .each(function(){

               var th = $(this),
                   thIndex = th.index(),
                   inverse = false;

               th.click(function(){

                   table.find('td').filter(function(){

                       return $(this).index() === thIndex;

                   }).sortElements(function(a, b){

                       return $.text([a]) > $.text([b]) ?
                           inverse ? -1 : 1
                           : inverse ? 1 : -1;

                   }, function(){

                       // parentNode is the element we want to move
                       return this.parentNode;

                   });

                   inverse = !inverse;

               });

           });
           </script>

           </div>
        </div>
        <div id="footer">
            Copyright &copy; 2017 Jenewan Pets

        <!-- jQuery first, then Tether, then Bootstrap JS. -->
        <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>



      </body>
    </html>
  • It would be helpful to us if you described the methods you've tried and didn't work. Perhaps we can Identify what went wrong with your implementation. – Chris J Sep 24 '17 at 01:24
  • What does "none have worked" mean - it gave an error/it failed/ you got an unexpected result? Please review [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). Your question should include a clear outline of your *specific* coding-related issue, a summary of what you have already tried and the **relevant** code in a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), so we have enough information to be able to help – FluffyKitten Sep 24 '17 at 01:49
  • One of the methods I used to arrange the table data drew all the data from my sql database instead of just rearranging the results into alphabetical order by name or by price highest to lowest.I will find an example of the code I used if that would be of any help. – Ewan McQueen Sep 24 '17 at 02:14
  • This code you have shared in your question, is it the actual code or it's 2 different files? Because I can see you are sending an `AJAX` request to a file named: _searchresults11.php_ – EhsanT Sep 24 '17 at 03:31
  • It's a single code, but I have garnered the information from different sources (I know it's a bit of a Frankenstein way of coding but as I am still learning I thought it would possibly work.) The reason it reads from searchresults11.php is because I wanted it to post to the div#main section of my website. Like I said all I want to do is sort the data that has been returned from the query I input into my search bar. – Ewan McQueen Sep 24 '17 at 16:50
  • OK, I think I understand what you want to do. But I still do not understand that you want to filter the data or sort it?your current code is somehow weird, you have different ways of sorting the data in your `` element to filter your data using `like` in your `where clause`! If I understand what exactly you want to do, then I can post an answer for you... – EhsanT Sep 24 '17 at 22:19
  • After my results have been posted I want to be able to sort the data from A-Z, Z-A and price Ascending and Descending. – Ewan McQueen Sep 26 '17 at 20:36
  • So I think you need to post a new question, and explain your question more. Because right now I still can not understand your logic. you have an edit box which you can type something in it and then you submit your form which will search the database and filter the result, then you have a `` for sort next to the edit box and do the search and sort at the same time? 2- for sort, why don't you use some JavaScript sort functions like [this](https://stackoverflow.com/a/3160718/1908331)? – EhsanT Sep 27 '17 at 17:29
  • What I was asking was that once I had my search results filtered to the page in a table, I then wanted to have the option to sort the data by choosing an option from a drop down menu. I have not used JavaScript because this is my first attempt at making my own website and am still reading into using JavaScript more so I can understand it better. Sorry if I wasn't being clear. I will look into the link you posted, thanks for at least trying :) – Ewan McQueen Sep 27 '17 at 18:06
  • You are welcome. Using JavaScript for what you want to do(sorting current data you have on your browser) is the best practice. You can also use `AJAX` but you have to remember that for each change of sort, you are sending a request to the server and run a query again which is not necessary at all and all of it will be an extra overload to your server. In these situations, use `AJAX` only when you need new data from the server otherwise you can use JavaScript(or JavaScript based frameworks like `jQuery`) to manipulate your `HTML` – EhsanT Sep 27 '17 at 21:00
  • I have tried to use the JavaScript as linked above to try and sort my data but alas to no avail, I was receiving an error whilst attempting to use Double Quotation Marks to label an id= tab for my table columns, so I did a search and the response I found told me to use Single Quotation Marks, this corrected the error but did not allow me to sort the data in my search. – Ewan McQueen Sep 28 '17 at 02:30
  • this so far for php
    – Ewan McQueen Sep 28 '17 at 23:40

0 Answers0