Jump to content

paging is not happening


maviyazilim

Recommended Posts

1 hour ago, maviyazilim said:

The line with the error code is below.

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

Despite requests, you refused to show any code that output the query results. On top of that you used "SELECT * " which gives us no information whatsoever about the columns you are selecting.

As I told you, I created a test "icerik" table. This had columns "icerik_id" and "name" - which is why I output a column called "name".

You need to use your column names, whatever they are.

  • Like 1
Link to comment
Share on other sites

Barand

I have added as a header. sorted the contents. I don't want that.

 

I want to paginate. The titles of the article itself plus the entire table are already listed on the homepage. Clicking on 1 and 2 does not change the homepage at all. thats the problem. There are 16 contents. No matter which page you click on, there is always the last 10 content on the homepage.

Link to comment
Share on other sites

ginerjm.

I did not reject anything. I do not know english. I'm using google translate. I have very little knowledge. I don't understand what you are saying. I will do whatever you want me to write where with details or code samples. I can send you all the files I have if you want. If you say that this much detail and information is not enough.

 

Link to comment
Share on other sites

Post ALL of your code here.  ALL of it.  I will copy it from here and see how bad it is.  We will start from there.  If it is very bad that may help me to help you by knowing what skills you have.

One chance.   I repeat:   ALL OF YOUR CODE.  No links.  Just code.

Link to comment
Share on other sites

<?php
error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');
 ?>
<div id="orta">
<?php
$sorgu = mysqli_query($conn,"select * from icerik order by icerik_id desc limit 10");

while($goster=mysqli_fetch_assoc($sorgu)) {
  echo '<div id="konu">';
  echo '<div id="baslik">';
  echo $goster['baslik'];
  echo '</div>';
  echo '<div id="yazi">';
  echo substr($goster['yazi'],0,300);
  echo '</div>';
  echo '<div id="yazar">';
  echo $goster['yazar'];
  echo '</div>';
  echo '</div>';
}

?>
<div id="sayfalama">
<?php

$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> ';
}


?>

</div>
</div>
 

Link to comment
Share on other sites

Try this:

<?php
session_start();
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
$results_per_page = 10;
if (!isset($_POST['sayfa']))
{
	$page = 1;
	//   find total # of rows
	$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);
	$this_page_first_result = 1;
}
elseif(isset($_POST['sayfa'])
{
	$page = $_POST['sayfa'];
	$number_of_pages = $_POST['number_of_pages'];
	$number_of_results = $_POST['number_of_results'];
	$this_page_first_result = (($page - 1) * $results_per_page) + 1;
}
else
{
	echo "Invalid response from page";	//  ERROR NO GET, NO POST
	exit();
}
//  get set of rows
$q = "select * from icerik
		order by icerik_id desc
		limit $results_per_page offset $this_page_first_result";
$sorgu = mysqli_query($conn, $q);

//*******************************
//  Show the page
echo 'div id="orta">';

//   show set of rows
while($goster = mysqli_fetch_assoc($sorgu))
{
	$fld1 = $goster['baslik'];
	$fld2 =	substr($goster['yazi'], 0, 300);
	$fld3 = $goster['yazar'];

	$code=<<<heredocs
	<div id="konu">
		<div id="baslik">
		$fld1
		</div>
		<div id="yazi"
		$fld2
		</div>
		<div id="yazar">
		$fld3
		</div>
	</div>
heredocs;
	echo $code;
}
//*******************************
//   show the form of page numbers
echo '<div id="sayfalama">';
echo "<form method='POST' action='index.php'>";

// change to "type='hidden'" after testing
echo "<input type='text' name='number_of_pages' value='$number_of_pages'>";

// change to "type='hidden'" after testing
echo "<input type='text' name='number_of_results' value='$number_of_results'>";
echo '<br><br>';
for ($i = 1; $i <= $number_of_pages; $i++)
{
	//   show page # choices
	echo "<input type='submit' name='sayfa' value='$i' size=2>";
	echo "&nbsp;&nbsp;";
}
echo "</form>";
echo "</div>";	// sayfalama
echo "</div>";	// orta
exit();

 

Link to comment
Share on other sites

Pagination is simple to do, but you need to actually want to learn it.

 

Here's what I call a mini tutorial on how to do it though it uses PDO instead of mysqli, but the principle is the same.

 

Here's a function that I wrote:

/*
 * Pagination Format
 * Read all the data from the database table in an array format
 */
function readData($pdo, $table, $page, $perPage, $offset) {
    $sql = 'SELECT * FROM ' . $table . ' WHERE page=:page ORDER BY date_added DESC LIMIT :perPage OFFSET :blogOffset';
    $stmt = $pdo->prepare($sql); // Prepare the query:
    $stmt->execute(['perPage' => $perPage, 'blogOffset' => $offset, 'page' => $page]); // Execute the query with the supplied data:
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

Here's how to implement it:

/*
 * Using pagination in order to have a nice looking
 * website page.
 */

if (isset($_GET['page']) && !empty($_GET['page'])) {
    $current_page = urldecode($_GET['page']);
} else {
    $current_page = 1;
}

$per_page = 1; // Total number of records to be displayed:

/*
 * Grab total records (rows) in a database table
 */
function totalRecords($pdo, $table, $page = 'blog') {
    $sql = "SELECT count(id) FROM " . $table . " WHERE page=:page";
    $stmt = $pdo->prepare($sql);

    $stmt->execute([ 'page' => $page ]);
    return $stmt->fetchColumn();

}

/* Total number of records that NEEDS to be displayed */
$total_count = totalRecords($pdo, 'cms');


/* calculate the offset */
$offset = $per_page * ($current_page - 1);

/* calculate total pages to be displayed */
$total_pages = ceil($total_count / $per_page);

/* Figure out the Pagination Links */
$links = links_function('index.php', $current_page, $total_pages);

/* Finally, call for the data from the database table to display */
$cms = readData($pdo, 'cms', 'blog', $per_page, $offset);

I don't know if it will help the OP any, but it should help others as well. 😉 I just want to add figuring out the Links isn't that hard to figure out and it shouldn't affect the pagination part, but it's pretty useless without it. I figure I can't do everything. 😉😃

Edited by Strider64
Link to comment
Share on other sites

barand.

I got the above below. I deleted the query.

this is the result.

 

Fatal error: Uncaught TypeError: mysqli_fetch_assoc(): Argument #1 ($result) must be of type mysqli_result, string given in C:\xampp\htdocs\blog\orta.php:40 Stack trace: #0 C:\xampp\htdocs\blog\orta.php(40): mysqli_fetch_assoc('SELECT * FROM i...') #1 C:\xampp\htdocs\blog\index.php(4): include('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\blog\orta.php on line 40

 

line 40 : while($goster=mysqli_fetch_assoc($sorgu)) {

 

Link to comment
Share on other sites

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.