需要 PHP 分析 SQL 命令并将行放置在正确的位置
我正在使用 PHP 和 JQUERY Slider Menu 的混合体,现在我正在尝试实现一个 CMS 后端系统,以便在我因陪产假离开公司时使用。
到目前为止,我的代码有 5 个部分:
- 标头,其中的 SubID 为“0”,URL 为 NULL
- 子标头,列出了其父类别的 ID,且 URL 不为 NULL
- 链接,其中的 SubID 为“0” SubID 和 URL 不为 NULL,但 CSS 看起来像标题
- 可见,它是 1 或 0,并指示是否显示记录
- 。 顺序,按记录升序计数。
所以我需要 SQL 读取数据库的每一行(TinyINT 因此只有大约 10 条记录)并确定该行是标题、子标题还是链接。
我可以让数据库找到所有标题,然后将子标题排列到正确的标题中,但我必须单独对链接进行编码,而不是按数字顺序显示它。
我的代码目前如下所示:
<?php
require("includes/connection.php");
$conHeaderSQL = "SELECT * FROM content WHERE conSubID = '0' AND conURL IS NULL AND conVis = 1 ORDER BY conOrder";
$conHeaderResult = $glob->query($conHeaderSQL) or die(mysqli_error());
if($conHeaderResult){
while($conHeaderRow = $conHeaderResult->fetch_object()){
echo "<p class='menuHeader' style='cursor:pointer'>";
echo $conHeaderRow->conHeader;
echo "</p>";
$conSubHeaderSQL = "SELECT * FROM content WHERE conSubID = $conHeaderRow->conID AND conVis = 1 ORDER BY conOrder";
$conSubHeaderResult = $glob->query($conSubHeaderSQL) or die(mysqli_error());
if($conSubHeaderResult){
while($conSubHeaderRow = $conSubHeaderResult->fetch_object()){
echo "<div class='msg_body'><ul>";
echo "<li><a href='$conSubHeaderRow->conURL' class='menuText'>{$conSubHeaderRow->conHeader}</a></li>";
echo "</ul></div>";
}
}
}
}
$conLinkSQL = "SELECT * FROM content WHERE conURL IS NOT NULL AND conBody IS NULL AND conSubID = '0' AND conVis = 1 ORDER BY conOrder";
$conLinkResult = $glob->query($conLinkSQL) or die(mysqli_error());
if($conLinkResult){
while($conLinkRow = $conLinkResult->fetch_object()){
echo "<p class='menuHeader'>";
echo "<a href='$conLinkRow->conURL' class='noDecorations'>$conLinkRow->conHeader</a>";
echo "</p>";
}
}
?>
任何人都可以帮助我将标题和链接按相同的编号顺序放置吗?
I am using a hybrid of PHP and JQUERY Slider Menu and now I am trying to implement a CMS back-end system for when I leave my company on Paternity leave.
So far I have 5 parts to the code:
- Header, this has "0" as the SubID and URL is NULL
- Sub-Header, this lists the ID from its parent category and URL is not NULL
- Link, this has "0" as the SubID and URL is not NULL but with CSS looks like a header
- Visible, this is either 1 or 0 and dictates if the record is shown or not
- Order, this counts in ascending order the records.
So I need the SQL to read every row of the database (TinyINT so only about 10 records) and decide whether the row is a header, sub-header or a link.
I can get the database to find all headers, then arrange the sub-headers into the correct header, but I have to code the links separately instead of showing it in number order.
My code currently looks like this:
<?php
require("includes/connection.php");
$conHeaderSQL = "SELECT * FROM content WHERE conSubID = '0' AND conURL IS NULL AND conVis = 1 ORDER BY conOrder";
$conHeaderResult = $glob->query($conHeaderSQL) or die(mysqli_error());
if($conHeaderResult){
while($conHeaderRow = $conHeaderResult->fetch_object()){
echo "<p class='menuHeader' style='cursor:pointer'>";
echo $conHeaderRow->conHeader;
echo "</p>";
$conSubHeaderSQL = "SELECT * FROM content WHERE conSubID = $conHeaderRow->conID AND conVis = 1 ORDER BY conOrder";
$conSubHeaderResult = $glob->query($conSubHeaderSQL) or die(mysqli_error());
if($conSubHeaderResult){
while($conSubHeaderRow = $conSubHeaderResult->fetch_object()){
echo "<div class='msg_body'><ul>";
echo "<li><a href='$conSubHeaderRow->conURL' class='menuText'>{$conSubHeaderRow->conHeader}</a></li>";
echo "</ul></div>";
}
}
}
}
$conLinkSQL = "SELECT * FROM content WHERE conURL IS NOT NULL AND conBody IS NULL AND conSubID = '0' AND conVis = 1 ORDER BY conOrder";
$conLinkResult = $glob->query($conLinkSQL) or die(mysqli_error());
if($conLinkResult){
while($conLinkRow = $conLinkResult->fetch_object()){
echo "<p class='menuHeader'>";
echo "<a href='$conLinkRow->conURL' class='noDecorations'>$conLinkRow->conHeader</a>";
echo "</p>";
}
}
?>
Can anyone help me place the headers and links in the same numbered order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为将链接合并到标题中的最简单方法是在 SQL 中使用
UNION
。对于获取的每一行,您必须区分标题和链接,但您的代码可以与现在非常相似。
当您休完陪产假回来时,您可以尝试深入更改代码。现在,您正在为每个标头发送一个新的 SQL 查询。因此,如果您有 10 个标头,您将向服务器发送 11 个查询(1 个用于标头,10 个用于子标头)。我建议您尝试用一个查询替换所有查询。您需要自动加入,可能使用
内容为 c1 LEFT JOIN content AS c2 ON c2.conSubId=c1.conId
。也许您需要花一些时间才能实现这一目标,但更好地了解 SQL 确实很有用。I think the easiest way to incorporate the links into the headers would be to use an
UNION
in your SQL.For each line fetched, you'll have to distinguish headers and links, but your code can stay quite similar to what it is now.
When you return from paternity leave, you could try to change your code in depth. Right now, you're sending a new SQL query for each header. So if you have 10 headers you'll send 11 queries to the server (1 for the headers, 10 for subheaders). I suggest you try to replace all your queries by a single one. You'll need to auto-join, probably with a
content as c1 LEFT JOIN content AS c2 ON c2.conSubId=c1.conId
. Maybe you'll take time to achive it, but getting to kown SQL better is truly useful.我不喜欢这 3 组数据不能轻易区分的方式,如果你添加一个新列
linktypeid
到content
和一个 linktype 表,例如:
这会简化选择标准,为内容类型附加含义,并允许您按列排序,为什么不呢。
I do not like the way the 3 sets of data can not be easily distinguished, if you add a new column
linktypeid
tocontent
And a linktype table e.g:
It would simplify the selection criteria, attach meaning to the content types, and allow you to order by the column, why not.