分页,mysql_fetch_assoc(),问题与url

时间:2012-08-10 20:19:07

标签: php mysql content-management-system pagination

我看到很多关于某些mysql错误的错误以及一些分页脚本。 我会尽力解释。

mysql_fetch_assoc() expects parameter 1 to be resource,

我包含了我的网页,所以我最终得到了这样的网址链接:

  • 的index.php?页=消息
  • 的index.php?页=下载
  • ...

当我加载索引(它开始显示news.php)时,我的分页脚本有效。 网址将如下所示:

http://mt-hq.com/http://mt-hq.com/index.php?page=1

这些陈述有效,但是当我转到另一个页面时,例如下载 然后返回新闻,网址将为index.php?page=2

应为index.php?page=news&page=2

在线它只是没有显示,但在本地我得到这个

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, <br>
boolean given in C:\usbwebserver\root\mt-hq.com\news.php on line 19

的index.php

<?php 
$docRoot = $_SERVER["DOCUMENT_ROOT"];
require_once $docRoot.'/mt-hq.com/include/functions.php';
dbconnect();

$page = isset($_GET['page']) ? trim(strtolower($_GET['page'])) : "news";

$allowedPages = array(

'news'          => './news.php',
'projects'      => './projects.php',
'gallery'       => './gallery.php',
'downloads'     => './downloads.php',
'tutorials'     => './tutorials.php',
'about'         => './about.php',
'contact'       => './contact.php',

);
?>


// Listing of the menu

<nav>
<?php
    echo '<ul>';

    foreach ($allowedPages as $pname => $v) 
    {

echo '<li><a class="noreload" href=index.php?page='. $pname .'>'. ucfirst($pname) .'</a></li>';

    }
echo '</ul>';
?>

 </nav>



// include of the page, starting with news
<div class="primary">
<?php include( isset($allowedPages[$page]) ? $allowedPages[$page] : $allowedPages["news"] );?>
</div>

news.php

<?php

    $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
        $limit = 3;
        $startpoint = ($page * $limit) - $limit;

        //to make pagination
        $statement = "`news` where `active` = 1";
        ?>

<section id="about-us">

<?php


//show records
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");

     while ($row = mysql_fetch_assoc($query)) {

echo '<h2>'. $row['title'] .'</h2>';
echo '<a href="edit.php" class="edit">edit</a>';
echo '<p>by <b>'. $row['name'] .'</b></p>';
echo '<p>'. $row['text'] .'</p>';


            }
    echo '<center>';
    echo pagination($statement,$limit,$page);
    echo '</center>';

?>

</section>

分页

function pagination($query, $per_page = 10,$page = 1, $url = '?'){        
        $query = "SELECT COUNT(*) as `num` FROM {$query}";
        $row = mysql_fetch_array(mysql_query($query));
        $total = $row['num'];
        $adjacents = "2"; 
        $page = ($page == 0 ? 1 : $page);  
        $start = ($page - 1) * $per_page;               
    $firstPage = 1;
    $prev = ($page == 1)?1:$page - 1;
        $prev = $page - 1;                          
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;
        $pagination = "";
        if($lastpage > 1)
        {   
        $pagination .= "<ul class='pagination'>";
        if ($page == 1)
        {
        $pagination.= "<li><a class='current'>&laquo;</a></li>";
        $pagination.= "<li><a class='current'>&lsaquo;</a></li>";

        }else{

$pagination.= "<li><a href='{$url}page=$firstPage'>&laquo;</a></li>";
$pagination.= "<li><a href='{$url}page=$prev'>&lsaquo;</a></li>";
}

$pagination .= "<li class='details'>Page $page of $lastpage</li>";

if ($lastpage < 7 + ($adjacents * 2))
{   
     for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
        $pagination.= "<li><a class='current'>$counter</a></li>";

else

$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";

}
}
elseif($lastpage > 5 + ($adjacents * 2))
{

if($page < 1 + ($adjacents * 2))        
{

for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)

{

if ($counter == $page)

$pagination.= "<li><a class='current'>$counter</a></li>";

else

$pagination.= "<li><a href='{$url}page=$counter'>$counter</a</li>";

}

$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      

}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                {
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>...</li>";

for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)

{

if ($counter == $page)

$pagination.= "<li><a class='current'>$counter</a></li>";

else

$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";

}

$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      

}else{

$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)

{

if ($counter == $page)

$pagination.= "<li><a class='current'>$counter</a></li>";

else

$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";

}
}
}


if ($page < $counter - 1){ 

$pagination.= "<li><a href='{$url}page=$next'>&rsaquo;</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>&raquo;</a></li>";

}else{

$pagination.= "<li><a class='current'>&rsaquo;</a></li>";
$pagination.= "<li><a class='current'>&raquo;</a></li>";

}

$pagination.= "</ul>\n";

}


return $pagination;
} 

尝试清理代码,使其适合此屏幕。

此致

0 个答案:

没有答案