saad|_d3vil Posted August 31, 2008 Share Posted August 31, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 31, 2008 Share Posted August 31, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
saad|_d3vil Posted August 31, 2008 Author Share Posted August 31, 2008 where i put your code in my code? 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.