我改变了我的分页,因为它没有第一个/上一个和下一个/最后一个导航功能到一个版本。
我改变它的原因是因为页面底部会有30多个编号的链接,而且它看起来很丑陋而且看起来不专业。
除了网站上的两个部分外,分页适用于我的所有页面。奇怪的是,这两个页面的 LEFT JOIN 就像下面的示例一样,但不同之处在于它们依赖于为每个页面选择 categoryID 来显示数据
以下是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); } ?>">« 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 »</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,我的分页功能就不起作用。
我收到以下错误代码:
注意:未定义的变量:导致第15行的C:\ wamp64 \ www \ Bushvelder \ categories.php
这是代码中的第15行:
if($result && count($result) > 0)
我将在“类别”页面中包含以下代码,因为我相信,一旦我对其进行了排序,我也可以修复广告商页面分页。
破解PHP&amp; 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 CODE
<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'] . '&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'] . '&page='. $prev); } ?>">« PREVIOUS</a>
</li>
<li class="active">
<a href="categories.php?categoryID=<?php echo $article['categoryID']; ?>&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'] . '&page='. $next); } ?>">NEXT »</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'] . '&page='. $pages); } ?>">LAST</a>
</li>
<?php } ?>
</ul>
我强烈怀疑我搞砸了我的MySQL,但我不知道从哪里开始。非常感谢任何帮助。
如果我在回答之前弄明白,我会发布我的发现。
修改
好吧所以我用 LIMIT:limit,:perpage 修正了错误,但我仍然在第15行得到错误,如上所述。
答案 0 :(得分:0)
所以这就是我为解决问题所做的工作。
<强>第一强> 对于与我的分页问题相关的所有变量,我使用 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变量仍然发布为null。所以我决定查看我的疑问。我经历了多次查询代码迭代,尝试了 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 似乎是打破我的分页的罪魁祸首,导致数小时的挫折,浪费时间和刮伤。我创建的 ENUM 似乎存在问题,以检查内容是否以DRAFT或PUBLISH状态保存。
我从中学到了什么?