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
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

 

Link to comment
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

 

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?  ???

Link to comment
Share on other sites

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 ""; }

?>

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.