链接到 php 分页页面上的项目

发布于 2024-11-07 20:48:32 字数 5060 浏览 0 评论 0原文

我是 PHP / MySQL 方面的新手。我目前正在开发一个网站,并找到了我在该网站上遇到的一些问题的许多答案 - 很棒的资源! - 但现在我需要问我自己的一个我正在努力解决的问题。请耐心等待...

所以在我的网站上,我有一个侧面板,它显示来自数据库的推荐,并随机提取 1 个结果,以便用户每次浏览网站时显示的推荐都会发生变化。侧面板是一个 php 包含文件,并在整个站点上显示。我只显示几行感言,然后添加一个阅读更多链接。

在推荐页面上,我有 13 个推荐,每页 1 个(13 页),因为每个推荐都很长 - 这就是客户想要的。

我想做的是从“阅读更多”链接链接到相关推荐 - 我认为使用 html 锚点是最好的?我发现的问题是我似乎无法链接到分页页面上的相关项目。如果我使用推荐 ID,我只能链接到 testimonials.php 的第 1 页,而不能链接到推荐出现的特定页面。我希望这是有道理的?

我在侧面板和推荐页面上包含了我的代码副本。

任何见解或帮助将不胜感激。

谢谢。

侧边栏.php

$gettest = mysql_query ("SELECT * FROM testimonial ORDER BY RAND() LIMIT 1");

while ($row=mysql_fetch_array($gettest)) 
{
$testimonial = $row['testimonial'];
$shorttest = myTruncate($testimonial, 200, " ");
?>

<blockquote><span class="bqstart">&#8220;</span><p><?PHP echo $shorttest;?></p><span class="bqend">&#8221;</span></blockquote>      
<p><a href="testimonials.php#<?PHP echo '' . $row["id"] . '';?>"><?PHP echo '' . $row["name"] . '';?></a></p>
<br clear='all' />

<?PHP
}                           
mysql_close($DB);
?>

推荐.php

<?php

include('connectDB.php'); 

$tableName="testimonial";
$targetpage = "testimonials.php";
$limit = 1; 

$query = "SELECT COUNT(*) as num FROM testimonial";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}   

// Get page data
$query1 = "SELECT * FROM testimonial LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;                    

$paginate = '';
if($lastpage > 1)
{   

$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>";   }

// Pages
if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
 $paginate.= "<span class='current'>$counter</span>";
}else{
 $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
{

// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
 if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
 }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}

// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
 if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
 }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}

// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
 if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
 }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}           

}

// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}

$paginate.= "</div>";       

}
// pagination
echo $paginate;

?>



<?php 

while($row = mysql_fetch_array($result))
{



?>
<a name="<?PHP echo '' . $row["ID"] . '';?>"></a>

<blockquote><span class="bqstart">&#8220;</span><p><?PHP echo '' .nl2br($row["testimonial"]) . '';?></p><span class="bqend">&#8221;</span></blockquote> 
<p class="highlight"><?PHP echo '' . $row["name"] . '';?></p>
<br clear='all' />

<?PHP
}

echo $paginate;

mysql_close($DB);

?>

I'm quite a novice coder in PHP / MySQL. I am developing a site at the moment and have found many answers to some of the questions I have had on this site - Great Resource! - but now I need to ask one of my own which I'm struggling with. Do bear with me...

So on my site I have a side panel which shows testimonials from a database and randomly pulls in 1 result so that the testimonial showing changes every time the user navigates around the site. The side panel is an php include and is shown across the site. I am only showing a few lines of the testimonial and then including a read more link.

On the testimonials page, I have 13 testimonials paginated on 1 per page (13 pages) as each testimonial is quite long - that's how the client wants it.

What I am looking to do is to link from the 'Read More' link to the relevant testimonial - I assume using an html anchor is best? The issue I'm finding is that I can't seem to link to the relevant item on it's paginated page. If I use the testimonial ID I can link through to only page 1 of the testimonials.php and not to the specific page that the testimonial appears on. I hope this makes sense?

I have included a copy of my code from the Side Panel and also on the Testimonial page.

Any insight or help would be greatly appreciated.

Thanks.

Sidebar.php

$gettest = mysql_query ("SELECT * FROM testimonial ORDER BY RAND() LIMIT 1");

while ($row=mysql_fetch_array($gettest)) 
{
$testimonial = $row['testimonial'];
$shorttest = myTruncate($testimonial, 200, " ");
?>

<blockquote><span class="bqstart">“</span><p><?PHP echo $shorttest;?></p><span class="bqend">”</span></blockquote>      
<p><a href="testimonials.php#<?PHP echo '' . $row["id"] . '';?>"><?PHP echo '' . $row["name"] . '';?></a></p>
<br clear='all' />

<?PHP
}                           
mysql_close($DB);
?>

Testimonials.php

<?php

include('connectDB.php'); 

$tableName="testimonial";
$targetpage = "testimonials.php";
$limit = 1; 

$query = "SELECT COUNT(*) as num FROM testimonial";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}   

// Get page data
$query1 = "SELECT * FROM testimonial LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;                    

$paginate = '';
if($lastpage > 1)
{   

$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>";   }

// Pages
if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
 $paginate.= "<span class='current'>$counter</span>";
}else{
 $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
{

// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
 if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
 }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}

// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
 if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
 }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}

// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
 if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
 }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}           

}

// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}

$paginate.= "</div>";       

}
// pagination
echo $paginate;

?>



<?php 

while($row = mysql_fetch_array($result))
{



?>
<a name="<?PHP echo '' . $row["ID"] . '';?>"></a>

<blockquote><span class="bqstart">“</span><p><?PHP echo '' .nl2br($row["testimonial"]) . '';?></p><span class="bqend">”</span></blockquote> 
<p class="highlight"><?PHP echo '' . $row["name"] . '';?></p>
<br clear='all' />

<?PHP
}

echo $paginate;

mysql_close($DB);

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-11-14 20:48:32

我不会详细介绍您可以对代码进行的所有更改,因为现在它很混乱。

您可能迷失在自己的代码中。

只需将此行(在 sidebar.php 上)更改为:(

<p><a href="testimonials.php?page=<?PHP echo '' . $row["id"] . '';?>"><?PHP echo '' . $row["name"] . '';?></a></p>

类似于您在“Testimonials.php”页面上所做的操作。)

I'm not going to to go into all the changes you can do to your code, because right now it's messy.

You probably went lost in your own code.

Just change this line (on the sidebar.php), to:

<p><a href="testimonials.php?page=<?PHP echo '' . $row["id"] . '';?>"><?PHP echo '' . $row["name"] . '';?></a></p>

(similar to what you've already done on your "Testimonials.php" page.)

安穩 2024-11-14 20:48:32

像这样:

<?php
$gettest = mysql_query ("SELECT * FROM testimonial ORDER BY RAND() LIMIT 1");

while ($row=mysql_fetch_array($gettest)) 
{
$testimonial = $row['testimonial'];
$shorttest = myTruncate($testimonial, 200, " ");
?>

<blockquote><span class="bqstart">“</span><p><?PHP echo $shorttest;?></p><span class="bqend">”</span></blockquote>      
<p><a href="testimonials.php?testimonialid=<?PHP echo '' . $row["id"] . '';?>"><?PHP echo '' . $row["name"] . '';?></a></p>
<br clear='all' />

<?PHP
}                           
mysql_close($DB);
?>

<?php
include('connectDB.php'); 

$query = "SELECT COUNT(*) as num FROM testimonial";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$testimonialid = mysql_escape_string($_GET['testimonialid']);  

// Get page data
$query1 = "SELECT * FROM testimonial WHERE id = ".$testimonialid;
$result = mysql_query($query1);

$paginate = '';
if($total_pages > $testimonialid)
{
    $paginate .= '<a href="testimonials.php?testimonialid='.($testimonialid+1).'">next</a>';
}
if($testimonialid > 1)
{
    $paginate .= '<a href="testimonials.php?testimonialid='.($testimonialid-1).'">previous</a>';
}
?>



<?php
echo $paginate;

while($row = mysql_fetch_array($result))
{
    ?>
    <a name="<?PHP echo '' . $row["ID"] . '';?>"></a>

    <blockquote><span class="bqstart">“</span><p><?PHP echo '' .nl2br($row["testimonial"]) . '';?></p><span class="bqend">”</span></blockquote> 
    <p class="highlight"><?PHP echo '' . $row["name"] . '';?></p>
    <br clear='all' />

    <?php
}

echo $paginate;

mysql_close($DB);
?>

已经删除了大部分分页代码,但这只是为了向您展示如何更简单地实现它。

Somethis like this:

<?php
$gettest = mysql_query ("SELECT * FROM testimonial ORDER BY RAND() LIMIT 1");

while ($row=mysql_fetch_array($gettest)) 
{
$testimonial = $row['testimonial'];
$shorttest = myTruncate($testimonial, 200, " ");
?>

<blockquote><span class="bqstart">“</span><p><?PHP echo $shorttest;?></p><span class="bqend">”</span></blockquote>      
<p><a href="testimonials.php?testimonialid=<?PHP echo '' . $row["id"] . '';?>"><?PHP echo '' . $row["name"] . '';?></a></p>
<br clear='all' />

<?PHP
}                           
mysql_close($DB);
?>

and

<?php
include('connectDB.php'); 

$query = "SELECT COUNT(*) as num FROM testimonial";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$testimonialid = mysql_escape_string($_GET['testimonialid']);  

// Get page data
$query1 = "SELECT * FROM testimonial WHERE id = ".$testimonialid;
$result = mysql_query($query1);

$paginate = '';
if($total_pages > $testimonialid)
{
    $paginate .= '<a href="testimonials.php?testimonialid='.($testimonialid+1).'">next</a>';
}
if($testimonialid > 1)
{
    $paginate .= '<a href="testimonials.php?testimonialid='.($testimonialid-1).'">previous</a>';
}
?>



<?php
echo $paginate;

while($row = mysql_fetch_array($result))
{
    ?>
    <a name="<?PHP echo '' . $row["ID"] . '';?>"></a>

    <blockquote><span class="bqstart">“</span><p><?PHP echo '' .nl2br($row["testimonial"]) . '';?></p><span class="bqend">”</span></blockquote> 
    <p class="highlight"><?PHP echo '' . $row["name"] . '';?></p>
    <br clear='all' />

    <?php
}

echo $paginate;

mysql_close($DB);
?>

I've removed most of the pagination code, but that's just to show you how it can be achieved a little simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文