Jump to content

Recommended Posts

It doesn't get any errors. Whichever page I click, it only shows the homepage. What is the reason for this error?

 

$results_per_page = 10;

$sql='SELECT * FROM icerik';
$result = mysqli_query($conn, $sql);
$number_of_results = mysqli_num_rows($result);

$number_of_pages = ceil($number_of_results/$results_per_page);

if (!isset($_GET['sayfa'])) {
  $page = 1;
} else {
  $page = $_GET['sayfa'];
}

$this_page_first_result = ($page-1)*$results_per_page;

$sql='SELECT * FROM icerik order by icerik_id desc limit 10';
$result = mysqli_query($conn, $sql);

for ($page=1;$page<=$number_of_pages;$page++) {
  echo '<a href="index.php?sayfa=' . $page . '">' . $page . '</a> ';
}

Link to comment
https://forums.phpfreaks.com/topic/313819-paging-is-not-happening/
Share on other sites

You are calculating the offset

$this_page_first_result = ($page-1)*$results_per_page; 

but you don't use it in your LIMIT clause, so you always start at offset 0. Try...

$sql="SELECT * FROM icerik order by icerik_id desc limit $this_page_first_result, $results_per_page";

 

That's all the pagination related code. no other code

 

$results_per_page = 10;

$sql='SELECT * FROM icerik';
$result = mysqli_query($conn, $sql);
$number_of_results = mysqli_num_rows($result);

$number_of_pages = ceil($number_of_results/$results_per_page);

if (!isset($_GET['sayfa'])) {
  $page = 1;
} else {
  $page = $_GET['sayfa'];
}

$this_page_first_result = ($page-1)*$results_per_page;

$sql='SELECT * FROM icerik order by icerik_id desc limit $this_page_first_result, $results_per_page';
$result = mysqli_query($conn, $sql);

for ($page=1;$page<=$number_of_pages;$page++) {
  echo '<a href="index.php?sayfa=' . $page . '">' . $page . '</a> ';
}

I think you would need something more like:

<?php
if (isset($_GET['sayfa'])) {
	$page_no = $_GET['sayfa'];
} else {
	$page_no = 1;
}

$no_per_page = 10;
$offset = ($page_no-1) * $no_per_page;

$total_pages = "SELECT COUNT(*) FROM icerik";
$result = mysqli_query($conn,$total_pages);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_per_page);

$sql = "SELECT * FROM icerik LIMIT $offset, $no_per_page";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
	//whatever you need to go here
}
mysqli_close($conn);
?>
<ul>
	<li><a href="index.php?sayfa=1">First Page</a></li>
	<li class="<?php if($page_no <= 1){ echo 'disabled'; } ?>">
	<a href="index.php<?php if($page_no <= 1){ echo '#'; } else { echo "?sayfa=".($page_no - 1); } ?>">Previous Page</a>
	</li>
	<li class="<?php if($page_no >= $total_pages){ echo 'disabled'; } ?>">
	<a href="index.php<?php if($page_no >= $total_pages){ echo '#'; } else { echo "?sayfa=".($page_no + 1); } ?>">Next Page</a>
	</li>
	<li><a href="index.php?sayfa=<?php echo $total_pages; ?>">Last Page</a></li>
</ul>

 

The reason you are not getting any errors (from your first post) is you probably don't have error reporting enabled.  

Add this to the beginning of your php script:

	error_reporting(E_ALL | E_NOTICE);
	ini_set('display_errors', '1');

And then add an echo of your query statement and read it.

Edited by ginerjm

jarvis.

The code you suggested did not work.

while($row = mysqli_fetch_array($result)){ //whatever you need to go here }

I don't understand what to write in this field.

Parse error: syntax error, unexpected token "<", expecting end of file in C:\xampp\htdocs\blog\orta.php on line 44

line 44 -> <ul>

 

13 minutes ago, maviyazilim said:

Barand

I copied and pasted the code you provided. same as your suggested code

it's not exactly the same. when there are php variables inside of a string, the type of quotes around the string matter. double-quotes are required to get the php variables to be replaced with their values.

20 minutes ago, mac_gyver said:

it's not exactly the same. when there are php variables inside of a string, the type of quotes around the string matter. double-quotes are required to get the php variables to be replaced with their values.

I added double quotes. the result did not change. not working

How about adding echo of the query string so we can all see what you have created?  Something like:

$sql = "select xxxxx sss xxx ";

echo "sql is $sql";

Then cut and paste what comes out on your screen when you run it and put it into your next post.

7 minutes ago, ginerjm said:

How about adding echo of the query string so we can all see what you have created?  Something like:

$sql = "select xxxxx sss xxx ";

echo "sql is $sql";

Then cut and paste what comes out on your screen when you run it and put it into your next post.

I didn't fully understand what you mean. i am a beginner.

 

When I created a test "icerik" table of 55 records and ran your code (with the double quotes around the query) it worked fine. This is the code I ran (changed the record counting bit)

$results_per_page = 10;

$sql='SELECT count(*) FROM icerik';        // get count of records in table
$result = $conn->query($sql);
$number_of_results = $result->fetch_row()[0];

$number_of_pages = ceil($number_of_results/$results_per_page);

$page = $_GET['sayfa'] ?? 1;

$this_page_first_result = ($page-1)*$results_per_page;

$sql="SELECT * FROM icerik order by icerik_id desc limit $this_page_first_result, $results_per_page";
$result = mysqli_query($conn, $sql);

foreach ($result as $r) {                // output list of results
    echo $r['icerik_id'] . ' - ' . $r['name'] . '<br>';
}

echo '<hr>';
for ($page=1;$page<=$number_of_pages;$page++) {
  echo '<a href="?sayfa=' . $page . '">' . $page . '</a>&emsp;';
} 

RESULTS (page 1)

image.png.e825da5f89feeeac844959a590a57533.png

RESULTS (Page 6)

image.png.222d00fde86f71a98bbe75fef130d4d2.png

I want  you to add that last line (echo) to your script right after the spot where the $sql var is defined and then run your script.  You should see the output of that echo on your screen.  Copy and past it into a post here.  Pretty simple, eh?

 

Barand.

I have 16 records in my database. It gave 16 error codes, all the same, for 16 records. all error codes for the same line.

The line with the error code is below.

43   echo $r['icerik_id'] . ' - ' . $r['name'] . '<br>';

 

Warning: Undefined array key "name" in C:\xampp\htdocs\blog\orta.php on line 43
16 -

Warning: Undefined array key "name" in C:\xampp\htdocs\blog\orta.php on line 43
15 -

Warning: Undefined array key "name" in C:\xampp\htdocs\blog\orta.php on line 43

That means that whatever items you have in the array $r there  are none named 'name'.  Do this before the first error line:

echo "r is <pre>",print_r($r,true),"</pre>";

and it will show you each and every item in that array.  Then you will understand and can begin to search your prior code to see why you don't have a 'name' item.

I don't know why I am helping you out here since you refuse to do the other thing I asked.....

That $sql line was a representation of YOUR select line.  As I explained later put the echo after that line.

And your latest use of my code is showing you that you have 4 items in your $r array:

icerik_id

baslik

yazi

yazar

 

So where is this 'name' item?  Where are you trying to add it to the $r array?  Read your code - debug it.

And if this is above you and you don't want to do the work, it's time to get out.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.