gpt4 book ai didi

php - LEFT JOIN 时分页中断

转载 作者:行者123 更新时间:2023-11-28 02:39:59 26 4
gpt4 key购买 nike

我已经更改了我的分页,因为它没有 First/Previous 和 Next/Last 导航功能到有的版本。

之所以改,是因为页面底部会有30+个带编号的链接,丑陋不专业。

除网站上的两个部分外,分页适用于我的所有页面。奇怪的是,这两个页面都有LEFT JOIN,就像下面的示例一样,但不同的是,它们依赖于为每个页面选择一个categoryID来显示数据。

Working Pagination

下面是 PHP 和 MySQL 的新代码示例以及代码的 HTML。

使用 PHP 和 MySQL

//PAGINATION
try {
$totalPages = $DB->query(" SELECT COUNT(id) as rows
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=art_categories.categoryID
AND art_categories.categoryID NOT IN
(SELECT categoryID
FROM art_categories
WHERE categoryID=8)
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = 1")
->fetch(PDO::FETCH_OBJ);

$posts = $totalPages->rows;
$pages = ceil($posts / $perpage);

# default
$get_pages = isset($_GET['page']) ? $_GET['page'] : 1;

$data = array(

'options' => array(
'default' => 1,
'min_range' => 1,
'max_range' => $pages
)
);

$number = trim($get_pages);
$number = filter_var($number, FILTER_VALIDATE_INT, $data);
$range = $perpage * ($number - 1);

$prev = $number - 1;
$next = $number + 1;

$stmt = $DB->prepare("SELECT * FROM articles WHERE publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH ) LIMIT :limit, :perpage");
$stmt->bindParam(':perpage', $perpage, PDO::PARAM_INT);
$stmt->bindParam(':limit', $range, PDO::PARAM_INT);
$stmt->execute();

$result = $stmt->fetchAll();

} catch(PDOException $e) {
$error = $e->getMessage();
}

工作 HTML

<ul class="pagination">
<?php

if($result && count($result) > 0)
{ ?>
<li <?php if($_GET['page'] >= 2) { echo('class=""'); } else { echo('class="disabled"'); } ?> >
<a href="<?php if($_GET['page']==1 || $_GET['page']==0) { echo('#');} else { echo ('index.php?page=1'); } ?>">FIRST</a>
</li>
<li <?php if(!empty($_GET['page']) && $_GET['page']==1 || $_GET['page']==0) { echo('class="disabled"');} ?>>
<a <?php if($_GET['page']==1 || $_GET['page']==0) { echo('class="disabled"');} ?> href="<?php if($_GET['page']==1 || $_GET['page']==0) { echo('#');} else { echo ('index.php?page='. $prev); } ?>">&laquo; PREVIOUS</a>
</li>
<li class="active">
<a href="index.php?page=<?php echo $get_pages; ?>"><?php echo $get_pages; ?></a>
</li>
<li <?php if($_GET['page']==$pages) { echo('class="disabled"');} ?>>
<a href="<?php if($_GET['page']==$pages) { echo('#');} else { echo ('index.php?page='. $next); } ?>">NEXT &raquo;</a>
</li>
<li <?php if($_GET['page']==$pages) { echo('class="disabled"');} ?>>
<a href="<?php if($_GET['page']==$pages) { echo('#');} else { echo ('index.php?page='. $pages); } ?>">LAST</a>
</li>

<?php } ?>
</ul>

到目前为止一切都很好。一旦我尝试在其他页面上为类别和广告商执行左 LEFT JOIN,我的分页就不起作用。

我收到以下错误代码:

注意: undefined variable :结果在第 15 行的 C:\wamp64\www\Bushvelder\categories.php

这是代码中的第 15 行:

if($result && count($result) > 0)

我将为类别页面添加下面的代码,因为我相信一旦我对它进行排序,我也将能够修复广告商页面的分页。

损坏的 PHP 和 MySQL

$catID = $_GET['categoryID'];

//PAGINATION
try {
$totalPages = $DB->query(" SELECT COUNT(id) as rows
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID={$catID}
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish")
->fetch(PDO::FETCH_OBJ);

$posts = $totalPages->rows;
$pages = ceil($posts / $perpage);

# default
$get_pages = isset($_GET['page']) ? $_GET['page'] : 1;

$data = array(

'options' => array(
'default' => 1,
'min_range' => 1,
'max_range' => $pages
)
);

$number = trim($get_pages);
$number = filter_var($number, FILTER_VALIDATE_INT, $data);
$range = $perpage * ($number - 1);

$prev = $number - 1;
$next = $number + 1;

$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID={$catID}
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
$stmt->bindParam(':perpage', $perpage, PDO::PARAM_INT);
$stmt->bindParam(':limit', $range, PDO::PARAM_INT);
$stmt->execute();

$result = $stmt->fetchAll();

} catch(PDOException $e) {
$error = $e->getMessage();
}

HTML 代码

<ul class="pagination">
<?php

if($result && count($result) > 0)
{ ?>
<li <?php if($_GET['page'] >= 2) { echo('class=""'); } else { echo('class="disabled"'); } ?> >
<a href="<?php if($_GET['page']==1 || $_GET['page']==0) { echo('#');} else { echo ('categories.php?categoryID='. $article['categoryID'] . '&amp;page=1'); } ?>">FIRST</a>
</li>
<li <?php if(!empty($_GET['page']) && $_GET['page']==1 || $_GET['page']==0) { echo('class="disabled"');} ?>>
<a <?php if($_GET['page']==1 || $_GET['page']==0) { echo('class="disabled"');} ?> href="<?php if($_GET['page']==1 || $_GET['page']==0) { echo('#');} else { echo ('categories.php?'. $article['categoryID'] . '&amp;page='. $prev); } ?>">&laquo; PREVIOUS</a>
</li>
<li class="active">
<a href="categories.php?categoryID=<?php echo $article['categoryID']; ?>&amp;page=<?php echo $get_pages; ?>"><?php echo $get_pages; ?></a>
</li>
<li <?php if($_GET['page']==$pages) { echo('class="disabled"');} ?>>
<a href="<?php if($_GET['page']==$pages) { echo('#');} else { echo ('categories.php?'. $article['categoryID'] . '&amp;page='. $next); } ?>">NEXT &raquo;</a>
</li>
<li <?php if($_GET['page']==$pages) { echo('class="disabled"');} ?>>
<a href="<?php if($_GET['page']==$pages) { echo('#');} else { echo ('categories.php?'. $article['categoryID'] . '&amp;page='. $pages); } ?>">LAST</a>
</li>

<?php } ?>
</ul>

我强烈怀疑我搞砸了我的 MySQL,但我不知道从这里去哪里。如有任何帮助,我们将不胜感激。

如果我在回答之前弄清楚,我会发布我的发现。


编辑

好吧,我用 LIMIT :limit, :perpage 修复了错误,但如上所述,我仍然在第 15 行遇到错误。

最佳答案

这就是我为解决问题所做的工作。

首先我使用 var_dump($variable) 对与我的分页问题相关的所有变量进行了一些变量转储。

<?php echo var_dump($result); ?>
<?php echo var_dump($totalPages); ?>
<?php echo var_dump($pages); ?>
<?php echo var_dump($get_pages); ?>
<?php echo var_dump($posts); ?>
<?php echo var_dump($stmt); ?>
<?php echo var_dump($catID); ?>
<?php echo var_dump($article['categoryID']); ?>
<?php echo var_dump($_GET['page']); ?>

这让我深入了解哪些变量有值,哪些变量为空。

然后我转到我的原始代码:

$stmt = $DB->prepare("  SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID={$catID}
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
$stmt->bindParam(':perpage', $perpage, PDO::PARAM_INT);
$stmt->bindParam(':limit', $range, PDO::PARAM_INT);
$stmt->execute();

然后我通过 PHP 设置了各种 SWITCH CASES 以查看当我点击我的菜单时 categoryID 是否确实发生了变化。这确实在起作用。

switch ($catID) {
case 1:
$stmt = $DB->prepare(" SELECT art_categories.*, articles.*
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=1
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
case 2:
$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=2
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
case 3:
$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=3
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
case 4:
$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=4
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
case 5:
$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=5
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
case 6:
$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=6
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
case 7:
$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=7
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
case 8:
$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID=8
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
AND state = Publish
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
break;
}

然后我查看了 VAR_DUMP 数据,$result 变量仍然显示为空。所以我决定检查我的查询。我经历了查询代码的多次迭代,尝试了不同的 JOIN 方法,以了解它如何影响我的 VAR_DUMP 数据。

然后这向我展示了我最初在 MySQL 查询中使用的 LEFT JOIN 没有问题,因为它在使用 LEFT JOIN 的其他页面上运行.

我继续测试我的查询,直到最后成功!

$catID = $_GET['categoryID'];

//PAGINATION
try {

$totalPages = $DB->query(" SELECT COUNT(id) as rows
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID = {$catID}
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )")
->fetch(PDO::FETCH_OBJ);

$posts = $totalPages->rows;
$pages = ceil($posts / $perpage);

# default
$get_pages = isset($_GET['page']) ? $_GET['page'] : 1;

$data = array(

'options' => array(
'default' => 1,
'min_range' => 1,
'max_range' => $pages
)
);

$number = trim($get_pages);
$number = filter_var($number, FILTER_VALIDATE_INT, $data);
$range = $perpage * ($number - 1);

$prev = $number - 1;
$next = $number + 1;

$stmt = $DB->prepare(" SELECT *
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE articles.categoryID = {$catID}
AND articles.publishDate >= DATE_ADD( NOW( ) , INTERVAL -2 MONTH )
ORDER BY articles.publishDate DESC LIMIT :limit, :perpage");
$stmt->bindParam(':perpage', $perpage, PDO::PARAM_INT);
$stmt->bindParam(':limit', $range, PDO::PARAM_INT);
$stmt->execute();

$result = $stmt->fetchAll();

} catch(PDOException $e) {
$error = $e->getMessage();
}

所以在所有测试之后,AND state = Publish 似乎是破坏我的分页并导致数小时的挫折、浪费时间和抓挠的罪魁祸首。我创建的用于检查内容是保存为 DRAFT 还是 PUBLISH 状态的 ENUM 似乎有问题。

我从中学到了什么?

  • 保持冷静,不要 panic
  • 使用var_dump() 测试您的变量
  • 设置不同的CASE SWITCHES对我很有帮助
  • 正确解决您的问题
  • 不要放弃
  • 学会了一些新技巧

关于php - LEFT JOIN 时分页中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45055505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com