如何制作可单击的列标题以导致数据重新排序
第一部分如下。这是第二部分:
我使用了下面提供的代码。我移动了一些东西并消除了所有错误。但是,现在我的数据丢失了。我得到带有标题的表格,并且它们是可点击的(但是,如果没有表格,就不可能看到点击会发生什么)。谁能看到我做错了什么导致数据无法显示?
<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>
谢谢你!
第一部分:
我对 PHP 很陌生,对 HTML 很生疏,所以这是一个真正的初学者问题。这是正在进行的代码。但请注意,这不是我的完整代码。我尝试了很多不同的事情,但遇到了这个语法错误:
解析错误:语法错误,意外的“?”在 C:\xampp\htdocs\moviedata3.php 第 30 行
据我所知,我需要那个“?”
所以,整个事情是这样的:
<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>
第 30 行写着:
<a href="<?= mysql_query("SELECT * FROM table1 ORDER BY movie_title") ?>"><th>Title</th></a>
非常感谢!!!
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!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来你没有注意什么应该在哪里执行。查询数据库是服务器端代码,单击按钮(在您的情况下是列标题)是客户端代码。
所以尝试以这样的方式重写你的片段:
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:
我相信你的问题是从这一行开始的:
你正在使用双引号打开一个字符串,但直到它到达以下位置才找到匹配的双引号:
它以 " 作为字符串的结尾,然后将 < 作为运算符。下一个字符是意外的 ?
如果您修复代码以使引号正确,则问题应该消失。
但是,代码中还有许多其他问题。 在循环之外,但在循环内关闭它。至于循环内的 mysql_query() 调用...我不知道您从哪里复制了代码,但对我来说它看起来绝对是狡猾的。首先,它不会返回一个字符串来回显,而是一个资源;并且
$result
变量用双引号引起来,这意味着它们会被插值。一片混乱,你最好再找一个您的解决方案所基于的示例。Your problem, I believe, is starting on this line:
You are opening a string using double quotes, but it's not finding a matching double quote until it gets to:
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 themysql_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.