开发者

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><?开发者_JS百科= $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!!!


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>


Your problem, I believe, is starting on this line:

echo "<table border='1'

You are opening a string using double quotes, but it's not finding a matching double quote until it gets to:

<a href="<?

It's taking " as ending the string, then the < as an operator. The next character is the unexpected ?

If you fix up your code so the quotes are correct, the problem should go away.

There are many other problems in the code though. For example, you're opening a <tr> outside of the loop, but closing it inside the loop. And as for the mysql_query() calls within the loop... I don't know where you've copied the code from, but it looks decidedly dodgy to me. That won't return a string to echo out, for a start, but a resource; and the $result variables are in double quotes, meaning they'd be interpolated. As someone else has said, it's a mess, and you'd be better off finding another example to base your solution on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜