Jump to content

[SOLVED] Not sure how to do this...


Aureole

Recommended Posts

Ok so here's the deal, basically the following code queries the database and shows all articles in the following format:

 

ID Title Edit Delete

 

This is all good but what if I had a thousand articles? It will show a thousand, how can I make it so say 20 are shown per page kind of like pagination I guess.

 

<?php
if (in_array($member['id'], $allowednewsmanage)) {

$query = "SELECT id, title FROM news";
$result = @mysql_query($query);
if ($result) {
echo "ID Title Delete Edit<br />";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "{$row['id']} {$row['title']} <a href=\"news/delete.php?id={$row['id']}\">Delete</a> <a href=\"news/edit.php?id={$row['id']}\">Edit</a><br />";
}
} else {
echo "There are no news articles to display.";
}
} else { echo ""; }
?>

Link to comment
https://forums.phpfreaks.com/topic/61776-solved-not-sure-how-to-do-this/
Share on other sites

it a long process but heres the great start for you

 

$query = "SELECT id, title FROM news limit 10,20";

 

that means you will query the record on the 10th up to the 20th record

 

now your prob is the link on the bottom or top but theres allot of codes out there

 

it a long process but heres the great start for you

 

$query = "SELECT id, title FROM news limit 10,20";

 

that means you will query the record on the 10th up to the 20th record

 

Thanks for the start.

 

now your prob is the link on the bottom or top but theres allot of codes out there

 

I don't understand what you mean when you say the link on the bottom or top.

 

Anyway I will try search Google.com for something but I don't know what to search for, what should I type?  ???

I got it working by using the following code, now I'm going to try find a way so you can click Next or Previous and it shows the results with ajax. ;D Anyone got any ideas?

 

<?php 

if (in_array($member['id'], $allowednewsmanage)) {

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

// Define the number of results per page 
$max_results = 5;

$from = (($page * $max_results) - $max_results);  

// Perform MySQL query on only the current page number's results 

$query = mysql_query("SELECT * FROM news LIMIT $from, $max_results"); 

echo "ID Title Delete Edit<br />";
while($row = mysql_fetch_array($query)){ 
echo "{$row['id']} {$row['title']} <a href=\"news/delete.php?id={$row['id']}\">Delete</a> <a href=\"news/edit.php?id={$row['id']}\">Edit</a><br />";
}

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM news"),0); 

$total_pages = ceil($total_results / $max_results); 

echo "<center>Select a Page<br />"; 

if($page > 1){ 
    $prev = ($page - 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> "; 
} 

for($i = 1; $i <= $total_pages; $i++){ 
    if(($page) == $i){ 
        echo "$i "; 
        } else { 
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
    } 
} 

if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>"; 
} 
echo "</center>";

} else { echo ""; }

?>

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.