Click here to Skip to main content
15,887,347 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I am attempting to sort my search results in two columns (Name and Price) by ascending and descending options. I have used a code I found but the code requires me to add an 'id' tag to the 'th' headings I require, I have attempted to do this but have not been able to for some reason. I am very new to coding and am struggling with this issue as I am trying learning on the fly.

HTML
<!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="query" 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['query'])){
          $searchq = $_GET['query'];
          }
        $query = 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">Item Name</th>
       <th>Brand</th>
       <th id="Price">Price</th>
       </tr>";

        while($row = mysqli_fetch_array($query))
        {
        echo "<tr>";
        echo "<td><img src='img/".$row['Picture']."'></td>";
        echo "<td>" . $row['Name'] . "</td>";
        echo "<td>" . $row['Brand'] . "</td>";
        echo "<td>£" . $row['Retail_Price_With_Delievery'] . "</td>";
        echo "</tr>";
        }
        echo "</table>";


        mysqli_close($con);
?>
<script>

var table = $('table');

   $('Names, 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 © 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>


What I have tried:

I have tried using single quotation marks on the table header 'id' tags as well as double quotation marks. I have tried changing the way it reads from the SQL database. But unfortunately my skill is limited for now as I am still learning the different aspects of the codes. I have a Javascript option that I cannot seem to get working and am unsure why.
Posted
Updated 30-Sep-17 8:36am

1 solution

PHP
$query = 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..."));

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]

Quote:
I am very new to coding and am struggling with this issue as I am trying learning on the fly.

It is generally a wrong good idea. Learn properly find some tutorial to get you started
This site is a good start:
W3Schools Online Web Tutorials[^]
PHP 5 Tutorial[^]
JavaScript Tutorial[^]

[Update]
Quote:
i checked on stackoverflow and they said their is not injection bit confused?

Simple test
PHP
$myquery = "SELECT * FROM final_dog_catologue_full where keywords LIKE '%$searchq%' OR brand LIKE '%$searchq%' OR name LIKE '%$searchq%' LIMIT 0, 40";
echo $myquery;
$query = mysqli_query($con, $myquery) or die(mysqli_error("Sorry we lost the scent..."));

If the 3 LIKE '%$searchq%' are changed with the text you search, it is an SQL injection.
 
Share this answer
 
v2
Comments
Ewan McQueen 30-Sep-17 14:34pm    
ah right concatenating strings is their a defenition to this not to sure what this means?
Patrice T 30-Sep-17 14:37pm    
"is their a defenition to this"
Definition to what?
Ewan McQueen 30-Sep-17 14:36pm    
and could i eliminate mysql use another program instead?
Patrice T 30-Sep-17 14:41pm    
Yes you can replace mysql by another program, but it require changes in the code you found, and you are back to the problem of properly learning.
Ewan McQueen 30-Sep-17 14:50pm    
i checked on stackoverflow and they said their is not injection bit confused?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900