maviyazilim Posted September 27, 2021 Share Posted September 27, 2021 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> '; } Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 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"; Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 not working. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 Thank you for sharing that useless piece of information. Imagine we don't know what you expect to see we don't know what you are actually seeing Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 when i click on first page i see last 10 content. When I click on the second page, I see the first 10 content. paging is not happening. It always shows the last 10 content no matter what page it is. No error codes appear either. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 It's surprising that it shows any results at all. There is no code to output the results of the query. What's the full code, in the sequence you have it? Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 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> '; } Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 27, 2021 Share Posted September 27, 2021 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> Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 3 hours ago, maviyazilim said: $sql='SELECT * FROM icerik order by icerik_id desc limit $this_page_first_result, $results_per_page'; That is not the same as the line of code I suggested. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 27, 2021 Share Posted September 27, 2021 (edited) 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 September 27, 2021 by ginerjm Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 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> Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 Barand I copied and pasted the code you provided. same as your suggested code Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 thank you ginerjm. I added the code you provided. no error codes were found. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 27, 2021 Share Posted September 27, 2021 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. Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 27, 2021 Share Posted September 27, 2021 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. Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 27, 2021 Author Share Posted September 27, 2021 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 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> '; } RESULTS (page 1) RESULTS (Page 6) Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 27, 2021 Share Posted September 27, 2021 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? Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 28, 2021 Author Share Posted September 28, 2021 ginerjm. sory. easy for you, hard for me. I could not understand the code you gave. It did not happen. Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 28, 2021 Author Share Posted September 28, 2021 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 28, 2021 Share Posted September 28, 2021 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..... Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 28, 2021 Author Share Posted September 28, 2021 ginerjm. sory. I do not refuse your help. What should I write in the place where x is written in the code you gave? I don't know this code structure. I do not know what to do. Quote Link to comment Share on other sites More sharing options...
maviyazilim Posted September 28, 2021 Author Share Posted September 28, 2021 ginerjm. These are the ones that come up after the code you gave r Array ( [icerik_id] => 14 [baslik] => 15 15 [yazi] => xxxxxxxx [yazar] => Admin ) Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 28, 2021 Share Posted September 28, 2021 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.