Setting the default pagination page (Something to do with the loop?) - php

I am currently using the plugin WP-CommentNavi for comments pagination but whenever a page is clicked it goes to the highest comments page (oldest comments). My comments are purposely displayed newest first and therefore I need the default pagination page to be 1 whenever a link is clicked.
I notice there is the same default for other pagination plugins too whereby the highest pagination page (ie if there are 5 pages) page 5 is displayed.
At the moment when I load page tellhi####.com/newcastle the page that will be loaded is tellhi####.com/newcastle/comment-page-2/ whereas I want tellhi####.com/newcastle/comment-page-1/ to be loaded
I can't be sure this is the relevant code to what I am trying to achieve but I feel the answer might lie in here (below). If not, please tell me and disregard this code.
### Function: Comment Navigation: Boxed Style Paging
function wp_commentnavi($before = '', $after = '') {
global $wp_query;
$comments_per_page = intval(get_query_var('comments_per_page'));
$paged = intval(get_query_var('cpage'));
$commentnavi_options = get_option('commentnavi_options');
$numcomments = intval($wp_query->comment_count);
$max_page = intval($wp_query->max_num_comment_pages);
if(empty($paged) || $paged == 0) {
$paged = 1;
}
$pages_to_show = intval($commentnavi_options['num_pages']);
$pages_to_show_minus_1 = $pages_to_show-1;
$half_page_start = floor($pages_to_show_minus_1/2);
$half_page_end = ceil($pages_to_show_minus_1/2);
$start_page = $paged - $half_page_start;
if($start_page <= 0) {
$start_page = 1;
}
$end_page = $paged + $half_page_end;
if(($end_page - $start_page) != $pages_to_show_minus_1) {
$end_page = $start_page + $pages_to_show_minus_1;
}
if($end_page > $max_page) {
$start_page = $max_page - $pages_to_show_minus_1;
$end_page = $max_page;
}
if($start_page <= 0) {
$start_page = 1;
}
if($max_page > 1 || intval($commentnavi_options['always_show']) == 1) {
$pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $commentnavi_options['pages_text']);
$pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
echo $before.'<div class="wp-commentnavi">'."\n";
switch(intval($commentnavi_options['style'])) {
case 1:
if(!empty($pages_text)) {
echo '<span class="pages">'.$pages_text.'</span>';
}
if ($start_page >= 2 && $pages_to_show < $max_page) {
$first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $commentnavi_options['first_text']);
echo ''.$first_page_text.'';
if(!empty($commentnavi_options['dotleft_text'])) {
echo '<span class="extend">'.$commentnavi_options['dotleft_text'].'</span>';
}
}
previous_comments_link($commentnavi_options['prev_text']);
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $paged) {
$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $commentnavi_options['current_text']);
echo '<span class="current">'.$current_page_text.'</span>';
} else {
$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $commentnavi_options['page_text']);
echo ''.$page_text.'';
}
}
next_comments_link($commentnavi_options['next_text'], $max_page);
if ($end_page < $max_page) {
if(!empty($commentnavi_options['dotright_text'])) {
echo '<span class="extend">'.$commentnavi_options['dotright_text'].'</span>';
}
$last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $commentnavi_options['last_text']);
echo ''.$last_page_text.'';
}
break;
case 2;
echo '<form action="'.admin_url('admin.php?page='.plugin_basename(__FILE__)).'" method="get">'."\n";
echo '<select size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">'."\n";
for($i = 1; $i <= $max_page; $i++) {
$page_num = $i;
if($page_num == 1) {
$page_num = 0;
}
if($i == $paged) {
$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $commentnavi_options['current_text']);
echo '<option value="'.clean_url(get_comments_pagenum_link($page_num)).'" selected="selected" class="current">'.$current_page_text."</option>\n";
} else {
$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $commentnavi_options['page_text']);
echo '<option value="'.clean_url(get_comments_pagenum_link($page_num)).'">'.$page_text."</option>\n";
}
}
echo "</select>\n";
echo "</form>\n";
break;
}
echo '</div>'.$after."\n";
}
}
In short I need, anytime a page is clicked, for the comments pagination to be on page 1, not the highest page available. Just so you know I have tried the discussion settings on the wordpress dashboard. Nothing on google either!
Failing a full answer, does anyone know the actual php code that determines what pagination page is loaded by default?
I received this response via an e-mail but due to my PHP knowledge am unable to implement it, any ideas? ...
Just reverse the loop. That'll fix it. Here's the relevant part:
http://pastie.org/8166399 You need to go back, i.e. use $i--
Would this change the default start page or just reverse the comments? Hope I have been clear in my question! Will appreciate any response at all.
Thanks in advance, Paul

The option was in Wordpress all along. See picture: http://oi40.tinypic.com/35aj3pt.jpg
If anyone has the answer to the specific code you manipulate, still answer the question here because someone doing this without Wordpress could very well encounter this problem also.

Related

Using GET, POST and GLOBALS

I'm trying to sort table of data by it's columns(as hyperlinks), but also to navigate using < or > buttons.
<?php
...
if (isset($_POST["next"]) && isset($_POST["start"]))
{
//force start to be an integer and add 5 to it
$start = (int)$_POST["start"] + 5;
}
elseif (isset($_POST["prev"]) && isset($_POST["start"]))
{
//force start to be an integer and remove 5 from it
$start = (int)$_POST["start"] - 5;
}
else
{
$start = 0;
}
// if one of hyperlinks in table heading was clicked
if (isset($_GET["sort"]))
{
$orderByFields = ["TerritoryDescription", "RegionDescription"];
$GLOBALS['sort'] = $_GET["sort"];
...
SQL here.
}
else
{
...
}
//problem here
// if next button was pressed
if (isset($_POST['next']))
{
echo 'next';
if (isset($_GET['sort']))
{
echo 'sort';
//undefined
//i'm trying to retrive value here.
echo $GLOBALS['sort'];
}
}
// if prev button was pressed
elseif (isset($_POST['prev']))
{
echo 'prev';
}
...
?>
I'm trying to retrieve value $GLOBALS['sort'] when < or > button was pressed so I can sort the SQL results by $GLOBALS['sort']. But the variable is undefined when I try to access from outside.

PHP Pagination Script Not Showing Correct Content For Page Number

I've built a php script to navigate my blog pages back and forwards, but the first page shows up correctly, but when I navigate to the next page it shows the correct number, but still shows the same content as before. Then, the next button keeps showing the current page.
Here is my script:
$publication = "magazine";
$ppp = "6";
if(isset($_GET["id"])){$id = $_GET["id"];}else{
header("Location: ".$url."");
exit();
}
if(isset($_GET["currentpage"])){$currentpage = $_GET["currentpage"];}else{
header("Location: ".$url."");
exit();
}
$count_sql = "SELECT id FROM publication_posts WHERE publication = '".$publication."' AND issue = '".$id."'";
$count_res = mysqli_query($con, $count_sql);
$count_total = mysqli_num_rows($count_res);
//If None, Then Exit
if($count_total == 0){
header("Location: ".$url."");
exit();
}
$display_limit = $ppp;
$totalpages = ceil($count_total / $display_limit);//$rowsperpage
$currentpage = $totalpages;
$offset = ($totalpages - 1) * $display_limit;//$rowsperpage
if($currentpage == "0"){
header("Location: ".$url."/issue/".$id."/".$totalpages."");
exit();
}
$posts_sql = "SELECT * FROM publication_posts WHERE publication = '".$publication."' AND issue = '".$id."' ORDER BY id DESC LIMIT ".$offset.", ".$display_limit."";
Another thing that I notice here is that if I set the current page to 0, it still shows issue/id/0, whereas this should redirect to the $totalpages
Anyone know where the problem is?
Here is my pagination:
<?php
// range of num links to show
$range = 0;
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
$link_next = "<div class=\"pagiCell pagiNext fadeOut\"></div>";
}
else{
$link_next = "<div class=\"pagiCell pagiNextX\"></div>";
}
?>
<?php
// range of num links to show
$range = 0;
if ($currentpage > 1) {
// get previous page
$prevpage = $currentpage - 1;
// echo forward link for next page
$link_prev = "<div class=\"pagiCell pagiPrev fadeOut\"></div>";
}
else{
$link_prev = "<div class=\"pagiCell pagiPrevX\"></div>";
}
?>
<?php
$link_refresh = "<div class=\"pagiCell pagiRefresh fadeOut\"></div>";
?>
<?php
$randpage = rand(1,$totalpages);
$link_random = "<div class=\"pagiCell pagiRandom fadeOut\"></div>";
?>
Short URL : localhost/publication/magazine/issue/1/1
Long URL : localhost/publication/magazine/issue?id=1&currentpage=1
I think your problem is that your $currentpage var is a string and you are doing math with it without converting it. You should type cast it: PHP: Type Juggling
$nextpage = $currentpage + 1;
becomes $nextpage = (int)$currentpage + 1;
Like Magnus Eriksson told before, you are not checking your user inputs for security issues. Testing your variables for expected values imply that, e.g. PHP: is_int

How to make specific button disappear

I am newbie at website programming. This is for my school 'end of the year' project. I need to know how to make upvote/downvote system. I managed to make +1 and -1 buttons for each of my posts, but when I want them to disappear when clicked all of them disappear (instead of only the specific one). The +1 and -1 works for individual posts but my solution does not work. example: When I click button +1 on post id=2 all of +1 buttons disappear and so on. I would like to know the solution for my problem. Thanks in advance
while ($forum = $vysledek->fetch_assoc())
{
if (isset($_SESSION['upvote'])) {
$type = 'hidden';
}
else
{
$type = 'button';
}
if (isset($_SESSION['downvote'])) {
$type1 = 'hidden';
}
else
{
$type1 = 'button';
}
?>
<a href='votes.php?pris=<?php echo $forum['id_prispevek'] ?> & ad_id=1'>
<input type="<?php echo $type ?>" value="+1"></a>
<a href='votes.php?pris=<?php echo $forum['id_prispevek'] ?> & ad_id=0'>
<input type="<?php echo $type1 ?>" value="-1"></a>
}
and on votes.php
<?php
include "pripojeni1.php";
$var_value = $_GET['ad_id'];
$prispevek = $_GET['pris'];
if ($var_value == 1) {
$query = mysqli_query($link,"
UPDATE forum
SET votes = votes + 1
WHERE id_prispevek = '".$prispevek."'
");
header("location:vypisForum.php?var=$prispevek");
$_SESSION['upvote'] = 1;
unset($_SESSION['downvote']);
}
if ($var_value == 0) {
$query = mysqli_query($link,"
UPDATE forum
SET votes = votes - 1
WHERE id_prispevek = '".$prispevek."'
");
header("location:vypisForum.php?var=$prispevek");
$_SESSION['downvote'] = 1;
unset($_SESSION['upvote']);
}
?>

How to make current page with different style (PHP ?page href)?

My piece of code.
And problem now are about styling, i just want style page number, wich is open, like a active id, if page open, then that number is in different color or smth else.
I need make a new variable? Or what, i just try to add but its wont work, it only works if at the end i write echo $page; , then it show style on this, but i need on links, numbers.
<?php
if($total_pages > 1){
if($page != 1){
echo ' < ';
}
for($number=1;$number<=$total_pages;$number++)
{
echo ''.$number.'';
}
if($page != $total_pages){
echo ' > ';
}
}
?>
You can do something like this.
$currentPageNumber = $_GET['page'];
for($number=1;$number<=$total_pages;$number++){
$currentPageStyle = '';
if($number == $currentPageNumber){
$currentPageStyle = 'style="color:red"';
}
echo '<a href="?page='.$number.'" '.$currentPageStyle.'>'.$number.'</a>';
}

Making a virtual shelf and populating it via a database

I'm trying to make a virtual shelf type of thing which is populated via a MySQL database. The column shelfPos holds the position of the item on the shelf. Each row/'shelf' starts with <div class="shelfRow"> and obviously ends with </div> so it's styled and positioned correctly. Items on the shelves can be moved around using the jQuery UI droppable interaction.
The overall layout is this: http://jsfiddle.net/aRA5D/
Each shelf can hold 5 items (left to right).
I'm having trouble populating the shelves. At the moment I've got this: (This is in the place of the HTML)
<?php
$sql="SELECT * FROM shelf WHERE userID='$userID'";
$result=mysql_query($sql);
if (mysql_num_rows($result) == 0) {
// Show a message of some sort? (No items)
}
else {
$tries = 1;
$times = 10; // How many shelves. (10 = 2 shelves)
while(($row = mysql_fetch_array($result)) && ($tries <= $times)) {
while ($tries <= $times) {
if ($tries == $row['shelfPos']) {
echo '<div class="drop" id="drop'.$tries.'"><div class="boxArt" id="'.$row['gameID'].'">'.$row['gameID'].'</div></div>';
}
else {
echo '<div class="drop" id="drop'.$tries.'"></div>';
}
$tries = $tries + 1;
}
$times = $times + 5;
}
}
?>
There's several things wrong with it. It doesn't include the <div class="shelfRow"> html (didn't know how/where to put it, as it needs to be echoed after every 5 'blank' and real items - for loop maybe?) and it requires me to input the number of shelves (2 in this case). Would it be possible to determine how many shelves are required based on the item's position? It's awkward to do because it also needs to echo 'blank' .drop divs before and after them so that the items can be moved around.
Hope this all makes sense. Thanks for the help!
First u need to get data in order of ShelfPos
"SELECT * FROM shelf WHERE userID='$userID' order by shelfPos asc"
And try this code:
...
$i = 0;
while($row = mysql_fetch_array($result)) {
//Each 5
if($i % 5 == 0) echo '<div class="shelfRow">';
if ($i == $row['shelfPos']) {
echo '<div class="drop" id="drop'.$i.'"><div class="boxArt" id="'.$row['gameID'].'">'.$row['gameID'].'</div></div>';
}
else {
echo '<div class="drop" id="drop'.$i.'"></div>';
}
//close shelfrow div
if($i % 5 == 4) echo '</div>';
$i++;
}
//to complete the loop
$shelv_left = 5 - ($i % 5);
if($shelv_left < 5) {
for($j=0; $j < $shelv_left; $j++) {
echo '<div class="drop" id="drop'.($i+$j).'"></div>';
}
echo '</div>'; // end shelfrow div
}
...

Categories