displaying datas

Discussion in 'PHP' started by badmasketa, Mar 14, 2008.

  1. #1
    how can i display the datas from the mysql table except last one row?
    i need to display all the records except last one.
    can u guys please help me out!!!
     
    badmasketa, Mar 14, 2008 IP
  2. Marc Fraser

    Marc Fraser Peon

    Messages:
    283
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's some example code:

    
    
    $query = mysql_query("SELECT * FROM tbl"); // our query
    
    $num = mysql_num_rows($query); // see how many there are
    
    $exlast = $num - 1; // take one away from total
    
    $query = mysql_query("SELECT * FROM tbl LIMIT ".$exlast.""); // this is our query.. now you can display the data however you want!
    
    PHP:
    Hope this helps.
     
    Marc Fraser, Mar 14, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    
    
    $query = mysql_query("SELECT * FROM tbl"); // our query
    
    for($i=1;$i<mysql_num_rows($query);$i++)
    {
    
    $thisResult = mysql_fetch_array($query);
    
    echo $thisResult['date_or_whatever_fieldname_you_need'];
    
    }
    
    
    PHP:
     
    jestep, Mar 14, 2008 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    <?
    $link = mysql_connect("localhost", "mysql_user", "mysql_password");
    mysql_select_db("database", $link);

    $result = mysql_query("SELECT * FROM table1", $link);
    $num_rows = mysql_num_rows($result);
    if ($numrows)
    {
    $result2 = mysql_query("SELECT * FROM table1 limit 0,$numrows-2", $link);
    ......//print all in loop


    }

    ?>
    Regards

    Alex
     
    kmap, Mar 14, 2008 IP
  5. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #5
    i dont want to display the last one and i want the datas to be shorted in descending order except last one...
     
    badmasketa, Mar 14, 2008 IP
  6. Marc Fraser

    Marc Fraser Peon

    Messages:
    283
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    $query = mysql_query("SELECT * FROM tbl"); // our query
    
    $num = mysql_num_rows($query); // see how many there are
    
    $exlast = $num - 1; // take one away from total
    
    $query = mysql_query("SELECT * FROM tbl ORDER BY something DESC LIMIT ".$exlast.""); // this is our query.. now you can display the data however you want! 
    
    while($row = mysql_fetch_array($query)) {
    
    print $row['table_column_name_here'];
    
    }
    
    
    PHP:
     
    Marc Fraser, Mar 14, 2008 IP
  7. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #7
    THIS CODE DISPLAYS THE LAST DATA ALSO, i dont wanna show the last one.

    Can you please help me out??
    mY code is this :
    
                          <?
    include 'connect.php';
    $query = mysql_query("SELECT * FROM articles");
    $num = mysql_num_rows($query); 
    $exlast = $num - 1;
    $query = mysql_query("SELECT * FROM articles ORDER BY a_id DESC LIMIT ".$exlast.""); 
    while($row = mysql_fetch_array($query))
     {
    $id = $row["a_id"];
    $header = ucwords($row["header"]); 
    $short = $row["short"];
    $full = $row["full"]; 
    $pic = $row["pic"]; 
    $date = $row["a_date"];
    echo "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
      <tr>
        <td width=\"1%\"><img src=\"images/bull_dot.gif\" border=\"0\"></td>
        <td width=\"99%\" style=\"padding-left:5px;\"><a href=\"morearticles.php?id=$id\" class=\"ln\">$header<br></td>
      </tr>
    </table>";
    }
    mysql_free_result($query);
    ?>
    
    PHP:
     
    badmasketa, Mar 14, 2008 IP
  8. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    <?
    include 'connect.php';
    
    $rowsquery = "select count(*) as count from articles");
    $rowsresult = mysql_query($rowsquery);
    $rowscount = mysql_fetch_array($rowsresult);
    $rows = $rowscount["count"];
    $row--;
    
    $query = "select * from articles order by a_id desc limit $rows";
    
    while($row = mysql_fetch_array($query))
     {
    $id = $row["a_id"];
    $header = ucwords($row["header"]);
    $short = $row["short"];
    $full = $row["full"];
    $pic = $row["pic"];
    $date = $row["a_date"];
    echo "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
      <tr>
        <td width=\"1%\"><img src=\"images/bull_dot.gif\" border=\"0\"></td>
        <td width=\"99%\" style=\"padding-left:5px;\"><a href=\"morearticles.php?id=$id\" class=\"ln\">$header<br></td>
      </tr>
    </table>";
    }
    mysql_free_result($query);
    
    ?>
    
    PHP:
    You've got alot of really redundant code in there though, you should clean it up.
     
    Christian Little, Mar 14, 2008 IP
  9. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #9
    IT STILL SHOWS THE LAST ROW ALSO.......... :( i m fed up.. :(
     
    badmasketa, Mar 14, 2008 IP
  10. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Minor correct to the code I posted:

    change
    
    $row--;
    
    PHP:
    to
    
    $rows--;
    
    PHP:
    How many rows are in your table?

    What's the URL that this is running on? It might help if we actually see it.
     
    Christian Little, Mar 14, 2008 IP
  11. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #11
    well it again shows the last one also..

    there are 4 rows but this code exclude the first row i.e id=1 and displays the last row i.e row =4
     
    badmasketa, Mar 14, 2008 IP
  12. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Actually it should be showing the last row, I just looked over the logic here.

    The query is pulling the articles in order of their ID from greatest to least and leaving out the last one. But I'm betting the table is lined up in the opposite order like this:

    1
    2
    3
    4
    5

    So our query is telling it to output it in this order

    5
    4
    3
    2
    1

    And since we're telling it to drop the last record, it should be dispalying:

    5
    4
    3
    2

    Try this:

    
    <?
    include 'connect.php';
    
    $firstquery = "select a_id from articles order by a_id desc limit 1");
    $firstresult = mysql_query($firstquery);
    $lastrow = mysql_fetch_array($firstresult);
    $lastrowid = $lastrow[0]
    
    $query = "select * from articles where a_id != $lastrowid order by a_id desc";
    
    while($row = mysql_fetch_array($query))
     {
    $id = $row["a_id"];
    $header = ucwords($row["header"]);
    $short = $row["short"];
    $full = $row["full"];
    $pic = $row["pic"];
    $date = $row["a_date"];
    echo "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
      <tr>
        <td width=\"1%\"><img src=\"images/bull_dot.gif\" border=\"0\"></td>
        <td width=\"99%\" style=\"padding-left:5px;\"><a href=\"morearticles.php?id=$id\" class=\"ln\">$header<br></td>
      </tr>
    </table>";
    }
    mysql_free_result($query);
    
    ?>
    
    PHP:
     
    Christian Little, Mar 14, 2008 IP
  13. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #13
    shows this errror:
    Parse error: parse error, unexpected T_VARIABLE in a.php

    in this LINE
    $query = "select * from articles where a_id != $lastrowid order by a_id desc";
     
    badmasketa, Mar 14, 2008 IP
  14. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #14
    damn me!! i am so f****n weak in programming ... :(
     
    badmasketa, Mar 14, 2008 IP
  15. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Oops, minor error. It's actually a few lines above it. T_VAR errors occur when a character is out of place.

    Go up to $firstquery = and delete the ) at the end of the line.
     
    Christian Little, Mar 14, 2008 IP
  16. Marc Fraser

    Marc Fraser Peon

    Messages:
    283
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    NO NEED TO SHOUT!

    Perhaps you can explain your point more precise and more clearly. After all, this isnt paid work. This is me volunteering to help you - it's not compulsory - I think you've forgotten that.
     
    Marc Fraser, Mar 14, 2008 IP
  17. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #17
    i already deleted that ) but the problem occured due to lost of ; in
    $lastrowid = $lastrow[0]

    i corrected it and also added mysql_query in front of
    $query = "select * from articles where a_id != $lastrowid order by a_id desc";

    i think i m getting lil bit of php codes now...LOL

    if i got any problem then will contact you later...

    ANYWAYS, THANKS A LOT BRO...
    TAKE CARE
     
    badmasketa, Mar 14, 2008 IP
  18. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #18
    yeah bro i have that in mind.. i didnt mean to say like that but u think like that.. SORRY for INCONVINIENCE :(

    THANKS A LOT FOR YOUR HELP
     
    badmasketa, Mar 14, 2008 IP