1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how can i display the first 3 elements from the last 10 ??

Discussion in 'PHP' started by crazy.works, Feb 24, 2009.

  1. #1
    hello,
    i have table called news and columns called news_id, news_name and newS_data.

    i added alot of news and deleted alot of them too. cauze of that i got alot of them which arent in arranged numbers inside the unique column news_id

    so i wanna display the first 3 news from the last 10 ,beside i wanna handle each data alone in variable (iam no gonna display the 3 in list with while )

    i mean i wanna got the 3 news like that
    $first_news_name = .....
    $first_news_data = ...
    $second_news_name = .....
    $second_news_data = ...
    $third_news_name = .....
    $third_news_data = ...

    so if it was arranged number with no delete , i would make it like that

    
    $result = mysql_query("SELECT * FROM news ORDER BY news_id DESC ");
    if (mysql_num_rows($result)) {
    $row = mysql_fetch_array($result);
            $the_last_id = $row['$news_id'];
      }
    
    $before_10_news = $the_last_id-10;
    $before_9_news = $the_last_id-9;
    $before_8_news = $the_last_id-9;
    
    $result2 = mysql_query("SELECT * FROM news WHERE news_id = '$before_10_news'");
    if (mysql_num_rows($result2)) {
    $row = mysql_fetch_array($result2);
            $first_news_name = $row['news_name'];
            $first_news_data = $row['news_data'];
      }
    
    
    
    PHP:
    and the smame way for $second_news and third_news
    but now the numbers arent in arrange , so please help me to display them

    thanks
     
    crazy.works, Feb 24, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't really understand what you're after here. Don't you just want the last three items? Is the number 10 relevant or just a red herring?

    $sql = "select news_name, news_data from news order by news_id desc limit 3";
    $st = mysql_query($sql) or die(mysql_error());
    $stupid_names = array('first', 'second', 'third');
    $stupid_name_index = 0;
    while ($row = mysql_fetch_assoc($st))
    {
       ${"{$stupid_names[$stupid_name_index]}_news_name"} = $row['news_name'];
       ${"{$stupid_names[$stupid_name_index]}_news_data"} = $row['news_data'];
       $stupid_name_index++;
    }
    Code (markup):
    That oughta do it.
     
    SmallPotatoes, Feb 24, 2009 IP