Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    7,554
    Tokens
    0

    Latest Awards:

    Default Determine if variable is last in a while statement [+6rep]

    G'day y'all!

    Quick question, how do i determine if a variable is the last in a while statement? Say the following is my while statement, i don't want there to be the "<hr>" after the last entry..

    PHP Code:
    while($row mysql_fetch_array($query)){
                    
    $id $row['id'];
                
                    
    $from $row['from'];
                    
    $from username($from);
                    
                    
    $message $row['message'];
                    
    $date $row['date'];
                    
                    echo(
    "<p><strong>id</strong>: $id<br />
                          <strong>from</strong>: 
    $from<br />
                          <strong>message</strong>: 
    $message<br />
                          <strong>date</strong>: 
    $date<br />
                          <hr>
                         "
    );
                } 

    No need to comment on the code apart from how to determine the last variable plz.

  2. #2
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    I'm not sure about this but couldn't you use strpos()?
    Back for a while.

  3. #3
    Join Date
    Apr 2009
    Location
    United Kingdom
    Posts
    1,111
    Tokens
    100

    Latest Awards:

    Default

    Just do a mysql_num_rows and then in the while increase a var by one each loop. Then if the mysql_num_rows resource = the number it's currently on, don't echo the <hr>

  4. #4
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    7,554
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by BoyBetterKnow View Post
    Just do a mysql_num_rows and then in the while increase a var by one each loop. Then if the mysql_num_rows resource = the number it's currently on, don't echo the <hr>
    I used your method. cheers!

    and i +repped you both

  5. #5
    Join Date
    Apr 2009
    Location
    United Kingdom
    Posts
    1,111
    Tokens
    100

    Latest Awards:

    Default

    Quote Originally Posted by Blinger View Post
    I used your method. cheers!

    and i +repped you both
    Thanks very much. I need rep haha.

  6. #6
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    7,554
    Tokens
    0

    Latest Awards:

    Default

    hmm.. it looks like it?


    was my end code btw

  7. #7
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    If you want an easier way; without incrementing, checking the numbers, or a wasteful
    num_rows

    It puts the <hr> on all of them.. then just removes the last one and echos out the combined string.

    PHP Code:
    while($row mysql_fetch_array($query)) {
        
    $string .= "<p><strong>id</strong>: {$row["id"]}<br />";
        
        
    $from username($row["from"]);
        
    $string .= "<strong>from</strong>: {$from}<br />";
        
        
    $string .= "<strong>message</strong>: {$row["message"]}<br />";
        
    $string .= "<strong>date</strong>: {$row["date"]}<br />";
        
    $string .= "</p><hr />";
    }

    echo 
    substr_replace($string"", -6); 

  8. #8
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    7,554
    Tokens
    0

    Latest Awards:

    Default

    Your code looks more complicated!

    So i have a few questions: what does the ".=" thing mean? I presume it means $string .= $string+"</p><hr />"; ?

    And also, what does the last line mean? That is the most confusing part!

  9. #9
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:

    Default

    It just removes the last 6 characters from $string, not anything super complicated

    Quote Originally Posted by Blinger View Post
    Your code looks more complicated!

    So i have a few questions: what does the ".=" thing mean? I presume it means $string .= $string+"</p><hr />"; ?

    And also, what does the last line mean? That is the most confusing part!

  10. #10
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    7,554
    Tokens
    0

    Latest Awards:

    Default

    oh right! +rep to you too then

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •