i have tow tables, posts and sections, i want to get the last 10 posts WHERE section = 1,
but use the 10 results in different places, i make a function

            function sectionposts($section_id){
$maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 10";
$maxpost10 =mysql_query($maxpost1);
$query2 = sectionposts(6);
while ($rows = mysql_fetch_array($query2)){

echo $rows['title'];
echo $rows['id'];
echo $rows['image_section'];
echo $rows['subject'];
echo $rows['image_post'];

how can in take this ten results but use them with different places ,and keep them Arranged from one to ten .

Edited by weemy

5 Years
Discussion Span
Last Post by weemy
function sectionposts($section_id){
    $maxpost =mysql_query("SELECT * from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 10");
        while ($rows = mysql_fetch_array($maxpost, MYSQL_NUM)){
            $output[] = $rows;

function formatpost($array, $rank){
        $r = $array[$rank];
        $output = "<h3>{$r['title']}</h3>";
        $output .= "<h4>{$r['subject']}</h4>";
        $output .= "<p>{$r['image_post']}</p>";
        $output .= "<img src='{$r['some_image']}' />";
        return $output;

$sData = sectionposts(6);

echo formatPost($sData, 0);


echo formatPost($sData, 1);


echo formatPost($sData, 7);

You could do something like that. NOT TESTED.


Nice answers and it's so helpful, but i didn't make the point well, what am i trying to do is :

1- get ten posts starts from max(id) to "$max(id)-9" form posts table.

2- using the max(id) in the top of the page and then useing $max(id)-1 in a defendant place of the page.

3- i had already generated a code

`Inline Code Example Here

    function getmax_id_with_minus ($minus){
    $maxid ="SELECT max(id) FROM posts";
    $maxid1 =mysql_query($maxid);
        while ($maxid_row = mysql_fetch_array($maxid1)){
                $maxid_id = $maxid_row['0'];
                $maxid_minus = $maxid_id - $minus;
    $selectedpost1 = "SELECT * FROM posts WHERE id = $maxid_minus";
    $query_selectedpost =mysql_query($selectedpost1);
        return ($query_selectedpost);

$ss = getmax_id_with_minus (8);
while ($rows = mysql_fetch_assoc($ss)){
$main_post_1 = $rows;


but i found anouther problem, that, if the client had deleted a post as id = 800 "so there aren't id = 800 in DB" so when i get the max id minus $NUM from it, and this operation must be equal id = 800, so i have a programing mistake here, how can i take care of something like that.

anyway "really" thanks again :) !



Nice answers and it's so helpful, but i didn't make the point well,

You have to understand that Daniweb is forum where people learn things and share knowledge. What diafol gave is good but you are adding stuff now.


thanks for makeing this point Mr.LastMitch, i have choosed the forum site to ask and get answers because i have read alot of topics and good topics here, that what pushes me to ask here

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.