/    Sign up×
Community /Pin to ProfileBookmark

Why Pagination Numered Do Not Display ?

Folks,

90% of my pagination is complete with mysqli.
The bottom part where the page numbers supposed to show up don’t show up
Why is that ?

Problem is here:

[code]
if($page_number>$total_pages)
{
echo “?><a href=”pagination_test.php?limit=$limit&page=$previous_page”>”;?><?php echo “<b> Previous </b>”;?></a><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo “<b> $i </b>”;?></a><?php
}

$i++;
}
}
[/code]

How to fix the above ?

Full Code:

[code]
<?php
require ‘conn.php’;
require ‘error_reporting.php’;
?>

<!DOCTYPE HTML”>
<html>

<head>
<meta name=”viewport” content=”width-device=width, initial-scale=1″>
</head>
<body>

<?php

if($_SERVER[‘REQUEST_METHOD’] === ‘GET’)
{
if(ISSET($_GET[‘search’]))//THERE WILL BE MORE THAN ONE BUTTON ON THIS PAGE. HENCE THIS LINE.
{
if(ISSET($_GET[‘keywords’]))
{
$keywords = $_GET[‘keywords’];
}
echo __LINE__; echo “<br>”;//THIS IS LINE 24
$query_1 = “SELECT COUNT(id) FROM links WHERE keywords = ? OR keyphrases = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,”ss”,$keywords,$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);

if($bind_result_1 === FALSE)
{
echo __LINE__;echo “<br>”;
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}

mysqli_stmt_fetch($stmt_1);
echo “Row Count: $row_count<br>”;
}
else
{
echo __LINE__;echo “<br>”;
die(“prepare failed!”);
}
mysqli_stmt_close($stmt_1);
//mysqli_close($conn);//THIS LINE CAUSED THE WHOLE PAGINATION TO MALFUNCTION GIVING FALSE ERROR PREPARE FAILED!

//PAGINATION
if(!ISSET($_GET[‘page’]))
{
$page = 1;
}
else
{
$page = $_GET[‘page’];
}

if(!ISSET($_GET[‘limit’]))
{
$limit = 2;
}
else
{
$limit = $_GET[‘limit’];
}
echo “Page: $page”; echo “<br>”;
echo “Limit: $limit”; echo “<br>”;

$offset = (($page * $limit) – $limit);
$previous_page = $page-1;
$next_page = $page+1;

echo “Row Start: $offset”; echo “<br>”;
echo __LINE__; echo “<br>”; //THIS IS LINE 78
$query_2 = “SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? OR keyphrases = ? ORDER by id LIMIT $offset,$limit”;
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))//WHY PREPARE FAILING ON THIS LINE ?
{
mysqli_stmt_bind_param($stmt_2,”ss”,$keywords,$keywords);
//mysqli_stmt_execute($stmt_2);

if(mysqli_stmt_execute($stmt_2) === FALSE)
{
echo __LINE__;echo “<br>”;
printf(“Error: %s.n”, mysqli_stmt_error($stmt_2));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt_2));
die;
}

$result_2 = mysqli_stmt_get_result($stmt_2);
//Grab total number of pages to paginate.
//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?

echo “TOTAL PAGES: $total_pages<br><br>”;
if(!$result_2)
{
die(“fetching Error”);
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
//Retrieve Values.
echo __LINE__;echo “<br>”;
$id = $row[“id”];
$page_url = $row[“page_url”];
$link_anchor_text = $row[“link_anchor_text”];
$page_description = $row[“page_description”];
$keyphrases= $row[“keyphrases”];
$keywords = $row[“keywords”];

echo “Id: $id<br>”;
echo “Page Url: $page_url<br>”;
echo “Link Anchor Text: $link_anchor_text<br>”;
echo “Page Description: $page_description<br>”;
echo “Keyphrases: $keyphrases<br>”;
echo “Keywords: $keywords<br>”;
echo “<br>”;
echo “<br>”;
}
}
else
{
echo __LINE__;echo “<br>”; //THIS IS LINE 127
printf(“Error: %s.n”, mysqli_stmt_error($stmt_2));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt_2));
die(“Prepare failed!”);
}
//Close Statement.
mysqli_stmt_close($stmt_2);

if($page_number>$total_pages)
{
echo “?><a href=”pagination_test.php?limit=$limit&page=$previous_page”>”;?><?php echo “<b> Previous </b>”;?></a><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo “<b> $i </b>”;?></a><?php
}

$i++;
}
}
}
//Close Connection.
mysqli_close($conn);
}

?>

<form method=’GET’ action=”<?php echo $_SERVER[‘PHP_SELF’];?>?limit=$limit&page=$page”>
<label for=”keywords”>Keywords:*</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Input Keywords” required>
<br>
<label for=”limit”>Results per Page</label>
<select name=”limit” id=”limit”>
<option value=”10″>10</option>
<option value=”10″>25</option>
<option value=”10″>50</option>
<option value=”10″>100</option>
</select>
<br>
<button name=search value=” “>Search</button><br>
<button type=”submit” name=”search” value=”search”>Search</button>
<br>
<input type=”reset”>
<br>
</form>

[/code]

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinAug 03.2020 — @developer_web#1621728

this line is

if($_SERVER['REQUEST_METHOD'] === 'GET')

is silly, in your code $_SERVER['REQUEST_METHOD'] === 'GET'

will always return true
Copy linkTweet thisAlerts:
@developer_webauthorAug 03.2020 — Attempted. No luck:
<i>
</i>if($page_number&gt;$total_pages)
{
echo "?&gt;&lt;a href="pagination_test.php?limit=$limit&amp;page=$total_pages"&gt;";?&gt;&lt;?php echo "&lt;b&gt; Final Page &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
}
else
{
$i = 1;
while($i!=$total_pages)
{
if($i&lt;$total_pages)
{
echo "&lt;a href="pagination_test.php?limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
}
elseif($i==$page_number)
{
echo "&lt;a href="pagination_test.php?limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
}

<i> </i> $i++;
<i> </i> }
}
Copy linkTweet thisAlerts:
@developer_webauthorAug 03.2020 — @DaveyErwin#1621729

Why will it always return true unless button clicked ?

How to fix it ?
Copy linkTweet thisAlerts:
@daveyerwinAug 03.2020 — @developer_web#1621731 said ...

Why will if($_SERVER['REQUEST_METHOD'] === 'GET') always return true

- - - - -
because in your code the REQUEST_METHOD is always get

make a page with only this

<?

echo $_SERVER['REQUEST_METHOD'];

?>

see what you get 😄
Copy linkTweet thisAlerts:
@developer_webauthorAug 03.2020 — @DaveyErwin#1621736

I get a total blank page.
Copy linkTweet thisAlerts:
@daveyerwinAug 03.2020 — on my version of PHP I don't

need this...

<?php

maybe you have older version ?

try like this ...

<?php

echo $_SERVER['REQUEST_METHOD'];

?>
Copy linkTweet thisAlerts:
@developer_webauthorAug 03.2020 — @daveyErwin,

Testing my code, I noticed this.

When I click the SEARCH button, I am fetched row count and then the 1st page gets displayed. No pagination section is shown on bottom.

And so on the url field, I type the url to go to PAGE 2 and go that way.

I get no results. Because the variables get deleted. And without the variables, the query cannot be made to query mysql for page 2 results. So, what did I amend now ? I added the variables into sessions. That way, when I go to PAGE 2, then the queries variables are called via the $_SESSIONs.

Also note that, my whole code was based on the SEARCH button getting clicked. Even though I cried the pagination section is not displaying, even if it did and I clicked the PAGE 2 link, then my code never would have triggered due to it being dependant on the SEARCH button getting clicked. And so, I copied the code to the bottom of the page. Now the code gets triggered regardless of the SEARCH button.

Check the code beneath this section:

//PAGINATION COPY STARTS FROM HERE. THE COPY THAT WILL RUN WHEN PAGE 2 AND ONWARDS ARE LOADED INDEPENDANT OF THE 'SEARCH' BUTTON CLICK.


<?php
require 'conn.php';
require 'error_reporting.php';
?>

<!DOCTYPE HTML">
<html>

<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</head>
<body>

<?php

if($_SERVER['REQUEST_METHOD'] === 'GET')
{
if(ISSET($_GET['search']))//THERE WILL BE MORE THAN ONE BUTTON ON THIS PAGE. HENCE THIS LINE.
{
if(ISSET($_GET['keywords']))
{
$keywords = $_GET['keywords'];
//$SESSION['keywords'] = $keywords;
}
echo __LINE__; echo "<br>";//THIS IS LINE 24
$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ? OR keyphrases = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,"ss",$keywords,$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
$SESSION['row_count'] = $row_count;

if($bind_result_1 === FALSE)
{
echo __LINE__;echo "<br>";
printf("Error: %s.n", mysqli_stmt_error($stmt));
printf("Error: %d.n", mysqli_stmt_errno($stmt));
die;
}

mysqli_stmt_fetch($stmt_1);
echo "Row Count:"; echo $_SESSION['row_count']; echo "<br>";
}
else
{
echo __LINE__;echo "<br>";
die("prepare failed!");
}
mysqli_stmt_close($stmt_1);
//mysqli_close($conn);//THIS LINE CAUSED THE WHOLE PAGINATION TO MALFUNCTION GIVING FALSE ERROR PREPARE FAILED!

//PAGINATION STARTS FROM HERE
if(!ISSET($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}

if(!ISSET($_GET['limit']))
{
$limit = 2;
$SESSION['limit'] = $limit;
}
else
{
$limit = $_GET['limit'];
$SESSION['limit'] = $limit;
}
echo "Page: $page"; echo "<br>";
echo "Limit:"; echo $SESSION['limit']; echo "<br>";

$offset = (($page * $limit) - $limit);
$SESSION['offset'] = $offset;

$previous_page = $page-1;
$SESSION['previous_page'] = $previous_page;

$next_page = $page+1;
$SESSION['next_page'] = $next_page;

echo "Row Start: $offset"; echo "<br>";
echo __LINE__; echo "<br>"; //THIS IS LINE 78
$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? OR keyphrases = ? ORDER by id LIMIT $offset,$limit";
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))//WHY PREPARE FAILING ON THIS LINE ?
{
mysqli_stmt_bind_param($stmt_2,"ss",$keywords,$keywords);
//mysqli_stmt_execute($stmt_2);

if(mysqli_stmt_execute($stmt_2) === FALSE)
{
echo __LINE__;echo "<br>";
printf("Error: %s.n", mysqli_stmt_error($stmt_2));
printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
die;
}

$result_2 = mysqli_stmt_get_result($stmt_2);
//Grab total number of pages to paginate.
//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?

echo "TOTAL PAGES: $total_pages<br><br>";
if(!$result_2)
{
die("fetching Error");
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
//Retrieve Values.
echo __LINE__;echo "<br>";
$id = $row["id"];
$page_url = $row["page_url"];
$link_anchor_text = $row["link_anchor_text"];
$page_description = $row["page_description"];
$keyphrases= $row["keyphrases"];
$keywords = $row["keywords"];

echo "Id: $id<br>";
echo "Page Url: $page_url<br>";
echo "Link Anchor Text: $link_anchor_text<br>";
echo "Page Description: $page_description<br>";
echo "Keyphrases: $keyphrases<br>";
echo "Keywords: $keywords<br>";
echo "<br>";
echo "<br>";
}
}
else
{
echo __LINE__;echo "<br>"; //THIS IS LINE 127
printf("Error: %s.n", mysqli_stmt_error($stmt_2));
printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
die("Prepare failed!");
}
//Close Statement.
mysqli_stmt_close($stmt_2);

if($page_number>$total_pages)
{
echo "?><a href="pagination_test.php?limit=$limit&page=$total_pages">";?><?php echo "<b> Final Page </b>";?></a><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo "<a href="pagination_test.php?limit=$limit&page=$i">";?><?php echo " $i ";?></a><?php
}
elseif($i==$page_number)
{
echo "<a href="pagination_test.php?limit=$limit&page=$i">";?><?php echo "<b> $i </b>";?></a><?php
}

$i++;
}
}
}
//Close Connection.
//mysqli_close($conn);
}

//PAGINATION COPY STARTS FROM HERE. THE COPY THAT WILL RUN WHEN PAGE 2 AND ONWARDS ARE LOADED INDEPENDANT OF THE 'SEARCH' BUTTON CLICK.

if(ISSET($_GET['page']))
{
$page = $_GET['page'];
}

if(!ISSET($_GET['limit']))
{
$SESSION['limit'];
}
else
{
$limit = $_GET['limit'];
}
echo "Page: $page"; echo "<br>";
echo "Limit:"; echo $SESSION['limit']; echo "<br>";

$offset = (($page * $limit) - $limit);
//$SESSION['offset'] = $offset;

$SESSION['previous_page'] = $previous_page;

$SESSION['next_page'] = $next_page;

echo "Row Start: $offset"; echo "<br>";
echo __LINE__; echo "<br>"; //THIS IS LINE 78
$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? OR keyphrases = ? ORDER by id LIMIT $offset,$limit";
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))//WHY PREPARE FAILING ON THIS LINE ?
{
mysqli_stmt_bind_param($stmt_2,"ss",$_GET['keywords'],$_GET['keywords']);
//mysqli_stmt_execute($stmt_2);

if(mysqli_stmt_execute($stmt_2) === FALSE)
{
echo __LINE__;echo "<br>";
printf("Error: %s.n", mysqli_stmt_error($stmt_2));
printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
die;
}

$result_2 = mysqli_stmt_get_result($stmt_2);
//Grab total number of pages to paginate.
//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($_SESSION['row_count']/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?

echo "TOTAL PAGES: $total_pages<br><br>";
if(!$result_2)
{
die("fetching Error");
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
//Retrieve Values.
echo __LINE__;echo "<br>";
$id = $row["id"];
$page_url = $row["page_url"];
$link_anchor_text = $row["link_anchor_text"];
$page_description = $row["page_description"];
$keyphrases= $row["keyphrases"];
$keywords = $row["keywords"];

echo "Id: $id<br>";
echo "Page Url: $page_url<br>";
echo "Link Anchor Text: $link_anchor_text<br>";
echo "Page Description: $page_description<br>";
echo "Keyphrases: $keyphrases<br>";
echo "Keywords: $keywords<br>";
echo "<br>";
echo "<br>";
}
}
else
{
echo __LINE__;echo "<br>"; //THIS IS LINE 127
printf("Error: %s.n", mysqli_stmt_error($stmt_2));
printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
die("Prepare failed!");
}
//Close Statement.
mysqli_stmt_close($stmt_2);

if($page_number>$total_pages)
{
echo "?><a href="pagination_test.php?limit=$limit&page=$total_pages">";?><?php echo "<b> Final Page </b>";?></a><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo "<a href="pagination_test.php?limit=$limit&page=$i">";?><?php echo " $i ";?></a><?php
}
elseif($i==$page_number)
{
echo "<a href="pagination_test.php?limit=$limit&page=$i">";?><?php echo "<b> $i </b>";?></a><?php
}

$i++;
}
}
//Close Connection.
//mysqli_close($conn);
//PAGINATION COPY ENDS HERE

?>

<form method='GET' action="<?php echo $_SERVER['PHP_SELF'];?>?limit=$limit&page=$page">
<label for="keywords">Keywords:*</label>
<input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required>
<br>
<label for="limit">Results per Page</label>
<select name="limit" id="limit">
<option value="10">10</option>
<option value="10">25</option>
<option value="10">50</option>
<option value="10">100</option>
</select>
<br>
<button name=search value=" ">Search</button><br>
<button type="submit" name="search" value="search">Search</button>
<br>
<input type="reset">
<br>
</form>


Now, all pages (PAGE 1 & PAGE 2 etc. show relevant results. Only problem is, the code still fails to display the pagination section:

After checking the latest code, do you still suggest I rid the ...

if($_SERVER['REQUEST_METHOD'] === 'GET')

Maybe, I just keep the ...

if(ISSET($_GET['search']))//THERE WILL BE MORE THAN ONE BUTTON ON THIS PAGE. HENCE THIS LINE.
Copy linkTweet thisAlerts:
@developer_webauthorAug 04.2020 — @DaveyErwin#1621739

Lol!

I get echoed:

**GET**

So you suggest I now remove the 1st IF even if there will be more buttons in the same page such as one for "search" and another for "submit".
<i>
</i>if($_SERVER['REQUEST_METHOD'] === 'GET')
{
if(ISSET($_GET['search']))//THERE WILL BE MORE THAN ONE BUTTON ON THIS PAGE. HENCE THIS LINE.
{
Copy linkTweet thisAlerts:
@daveyerwinAug 04.2020 — @developer_web#1621778 said ...

So you suggest I now remove the 1st IF even if there will be more buttons in the same page such as one for "search" and another for "submit".

- - - - -
No, I suggested that ...

this line

if($_SERVER['REQUEST_METHOD'] === 'GET')

is silly,

IN YOUR CODE

$_SERVER['REQUEST_METHOD'] === 'GET'

will always evaluate to true
Copy linkTweet thisAlerts:
@developer_webauthorAug 05.2020 — @DaveyErwin#1621790

So, if it is unnecessary then what is the use of keeping it ? Hence, I just deleted it but can change it to something you suggest if you got something to suggest.
Copy linkTweet thisAlerts:
@PereveAug 07.2020 — This is good way to manage and category. Can I manage discrete pages and boot Seo of website?
Copy linkTweet thisAlerts:
@developer_webauthorSep 09.2020 — @Pereve#1621869

What are you talking about ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 07.2021 — @NogDog

What does DaveyErwin mean ?

https://www.webdeveloper.com/d/390870-why-pagination-numered-do-not-display/2

Why would it always result TRUE ?

How to fix it then ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 18.2021 — @DaveyErwin#1621790

Show me how you would code it then if my code is silly.

Hate to ask you this a year later again.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...