Jump to content

[SOLVED] Spliting data into pages


saad|_d3vil

Recommended Posts

I've tried to split data into group of 10 from a txt file but I need help

 

In the txt file each group is one line and I want 10 line to be put into something like ?page#1 page#2...

 

then it will display 1-10 if the url has ?page#1, 11-20 if the page is #2 ...

 

What would the script be?

 

my code is this which i am using

<?php
$con = mysql_connect("localhost","apnimusk","password");
if (!$con){



die('Could not connect: ' . mysql_error());
}

mysql_select_db("apnimusk_other", $con);
if(isset($_GET['id']) && is_numeric($_GET['id'])){



$result = mysql_query("SELECT * FROM `joke` WHERE `id` = '".$_GET['id']."'");



echo "<table border='1'>



<tr>




</tr>";


while($row = mysql_fetch_array($result)){





echo "<tr>";





echo "<td>" . $row['name'] . "</td>";	



echo "</tr>";


echo "<tr>";




echo "<td>" . $row['text'] . "</td>";





echo "</tr>";


}



echo "</table>";
}else{



$result = mysql_query("SELECT * FROM `jokes`");



echo "<table border='1'>



<tr>



<th>Jokes</th>


</tr>";



while($row = mysql_fetch_array($result)){





echo "<tr>";








echo "<td>" ."<a href='?id=".$row['id']."'>".$row['name']."</a>" . "</td>";





echo "</tr>";



}



echo "</table>";
}
mysql_close($con);
?>

Link to comment
https://forums.phpfreaks.com/topic/122087-solved-spliting-data-into-pages/
Share on other sites

Wait, you say text file but I see no mention of a text file in the above code.

Here is a simple pagination script, using a text file. You could easily mod it for MySQL though.

 

<?php

$perPage = 10; //10 items per page

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

$data = file("text.txt");

//Get the total pages
$totalPages = ceil(count($data) / $perPage);

//If they are trying to view a page outside the range, stop them.
if($page > $totalPages){
$page = $totalPages;
}

//Get the start position
$from = ($page * $perPage) - $perPage;

//Now show the data
for($i = $from; $i < $from + $perPage; $i++){
echo $data[$i]."<br>";
}

//Build links.
echo "<br><br>";

$pages = array();
for($i = 1; $i <= $totalPages; $i++){
if($i == $page){
	$pages[] = "<b>{$i}</b>";
}else{
	$pages[] = "<a href='?page={$i}'>{$i}</a>";
}
}

echo implode(" ", $pages);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.