开发者

PHP from external anchor to internal

I have modified my project into a one page website type.

Part of the code is this:

for ($i = 1; $i <= $total_pages; $i++)
{                                    
    echo '<a href="view.php?page='.$i.'" data-role="button">'.$i.'</a>';

}

I need that code above to work with an internal link for example:

From here:

echo '<a href="view.php?page='.$i.'" data-role="button">'.$i.'</a>';

TO

echo '<a href="#my-anchor?page='.$i.'" data-role="button">'.$i.'</a>';

Here is the full source code:

$per_page = 6;
$result = mysql_query("SELECT * FROM mytable ORDER BY date DESC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);

if (isset($_GET['page']) && is_numeric($_GET['page']))
{
   $show_page = $_GET['page'];    
   if ($show_page > 0 && $show_page <= $total_pages)
   {
        $start = ($show_page -1) * $per_page;
        $end = $start + $per_page;
   }
   else
   {
        // error - show first set of results
        $start = 0;
        $end = $per_page;
   }                  
}
else
{
   // if page isn't set, show first set of results
      $start = 0;
      $end = $per_page;
}

// display pagination    
echo '<strong>View Page:</strong> ';
echo '<div data-role="controlgroup" data-type="horizontal">';
for ($i = 1; $i <= $total_pages; $i++)
{          
     echo '<a href="view.php?page='.$i.'" data-role="button">'.$i.'</a>';
     //echo '<a href="#view-pag-events" data-role="button">'.$i.'</a>';                
}    
echo '</div>';

I need to change this:

echo '<a href="view.php?page='.$i.'" data-role="button">'.$开发者_运维百科i.'</a>';

so it's internal.


Short answer: This can't be done in PHP alone. You will need send AJAX requests to a PHP script that grabs the data.

Basically, this will involve creating some javascript that listens for any changes to the hash (that's the part after the #);

(function(window) {
    var hash = window.location.hash;
    setTimeout(function() {
        if(window.location.hash !== hash) {
            hash = window.location.hash;
            //Grab and update page with ajax...
        }
        setTimeout(arguments.callee, 50);
    }, 50);
})(window);

For AJAX basics, check W3Schools. And please, please respect the back button.


Without reloading the page:

<a href="#my-anchor">foo</a>

If you modify the query string in the link, the page will reload and jump to the anchor:

<a href="?foo=bar#my-anchor">foo</a>

Or with JQuery:

<script type="text/javascript">
$(document).ready(function(){
   window.location.hash = 'my-anchor';
});
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜