Search code examples
phphtmlclickable

How do I make a clickable column title that will cause the data to re-order


Part One is below. Here is Part Two:

I have used the code provided below. I moved things around and got rid of all the errors. However, now my data is missing. I get the table with the titles and they are clickable (however, without the table it is impossible to see what clicking makes happen). Can anyone see what I've done wrong that keeps the data from showing up?

 <html>

 function getRecords($query) {
     $con = mysql_connect("localhost", "movie", "moviepw");
     if (!$con)
     {
        die('Could not connect: ' . mysql_error());
     }

     mysql_select_db("movies", $con);

     $result = mysql_query($query);
     return $result;

     function buildQuery($sortOrder) {
             $so = mysql_real_escape_string($sortOrder);
             $so = $so ? $so : "movie_title";
             return "SELECT * FROM table1 ORDER BY " + $so;
 }

     ?>

     </thead>
      <tbody>

      <?
                $sortOrder = $_GET['sortOrder'];

           $query = buildQuery($sortOrder);

           $records = getRecords($query);
 ?>

      <?

     while($row = mysql_fetch_array($result)){ ?>
               <tr>
               <td><?= $row['movie_title']; ?></td>
               <td><?= $row['movie_rating']; ?></td>
               <td> <img src="<?= $row['movie_image'];?>"> </td>
               <td><?= $row['movie_description']; ?></td>
      </tr>

 <table border='1'>
      <thead>
             <tr>
                    <th><a href="?sortOrder=movie_title">Title</a></th>
                    <th><a href="?sortOrder=movie_rating">Rating</a></th>
                    <th>Image</th>
                    <th><a href="?sortOrder=movie_description">Description</a></th>

             </tr>

</table>
</body>

Thank you!

Part One:

I'm brand new to PHP and very rusty in HTML, so this is a real beginner question. Here is the code in progress. But note that this is not my complete code. I'm trying a lot of different things and I'm getting this syntax error:

Parse error: syntax error, unexpected '?' in C:\xampp\htdocs\moviedata3.php on line 30

As far as I can tell, I NEED that "?."

So, here is the whole thing:

<html>
<body>
<table>

<?php
$con = mysql_connect("localhost", "movie", "moviepw");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("movies", $con);

$result = mysql_query("SELECT * FROM table1");

echo "<table border='1'
<tr>

?>

<?php

while($row = mysql_fetch_array($result))
  {

  ?>

   <? { ?>

   <a href="<?= mysql_query("SELECT * FROM table1 ORDER BY movie_title") ?>"><th>Title</th></a>

   <a href="<?php echo("$PHP_SELF?execute=$result = mysql_query("SELECT * FROM table1 ORDER BY movie_rating")") ?>"><th>Rating</th></a>

   <a href="<?php echo("$PHP_SELF?execute=$result = mysql_query("SELECT * FROM table1 ORDER BY movie_image")") ?>"><th>Image</th></a>

   <a href="<?php echo("$PHP_SELF?execute=$result = mysql_query("SELECT * FROM table1 ORDER BY movie_description")") ?>"><th>Description</th></a>

  </tr>";

<? } ?>

  <tr>
 <td><?= $row['movie_title']; ?></td>
 <td><?= $row['movie_rating']; ?></td>
 <td> <img src="<?= $row['movie_image'];?>"> </td>
 <td><?= $row['movie_description']; ?></td>
 </tr>

 <?
  }
echo "</table>";

mysql_close($con);
?>

    </td></tr>
</table>
</body>
</html>

Row 30 is the one that reads:

<a href="<?= mysql_query("SELECT * FROM table1 ORDER BY movie_title") ?>"><th>Title</th></a>

Thanks so much!!!


Solution

  • it seems that you do not pay attention to what should be executed where. querying DB is server side code and clicking button (column header in your case) is client side.

    so try to rewrite your snipped in some way like this:

     <?php
    
         function buildQuery($sortOrder) {
            $so = mysql_real_escape_string($sortOrder);
            $so = $so ? $so : "movie_title";
            return "SELECT * FROM table1 ORDER BY " + $so;
         }
    
         function getRecords($query) {
             $con = mysql_connect("localhost", "movie", "moviepw");
             if (!$con)
             {
                die('Could not connect: ' . mysql_error());
             }
    
             mysql_select_db("movies", $con);
    
             $result = mysql_query($query);
             return $result;
         }
    
         $sortOrder = $_GET['sortOrder'];
    
         $query = buildQuery($sortOrder);
    
         $records = getRecords($query);
         ?>
         <table border='1'>
              <thead>
                     <tr>
                            <th><a href="?sortOrder=movie_title">Title</a></th>
                            <th><a href="?sortOrder=movie_rating">Rating</a></th>
                            <th>Image</th>
                            <th><a href="?sortOrder=movie_description">Description</a></th>
    
                     </tr>
              </thead>
              <tbody>
        <?php
    
         while($row = mysql_fetch_array($result)){ ?>
              <tr>
              <td><?= $row['movie_title']; ?></td>
              <td><?= $row['movie_rating']; ?></td>
              <td> <img src="<?= $row['movie_image'];?>"> </td>
              <td><?= $row['movie_description']; ?></td>
              </tr>
    
        <? } ?>
              </tbody>
        </table>