We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,449 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

max(id) and limt 10, but use them in different places

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){
mysql_set_charset('utf8');
$maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 10";
$maxpost10 =mysql_query($maxpost1);
 return($maxpost10);
    }
$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 .
Thanks...

3
Contributors
4
Replies
23 Hours
Discussion Span
9 Months Ago
Last Updated
5
Views
weemy
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
function sectionposts($section_id){
    mysql_set_charset('utf8');
    $maxpost =mysql_query("SELECT * from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 10");
    if(mysql_num_rows($maxpost)){
        while ($rows = mysql_fetch_array($maxpost, MYSQL_NUM)){
            $output[] = $rows;
        }
    return($output);
    }
}

function formatpost($array, $rank){
    if(isset($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.

diafol
Keep Smiling
Moderator
10,841 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,536
Skill Endorsements: 61

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){
    mysql_set_charset('utf8');
    $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);

}
<?php 
$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 :) !

weemy
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

@weemy

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.

LastMitch
Industrious Poster
4,377 posts since Mar 2012
Reputation Points: 149
Solved Threads: 351
Skill Endorsements: 47

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

weemy
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0786 seconds using 2.74MB