Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts

    php issue displaying

    I am using this code:
    PHP Code:
    echo 
    '<a href="'.
    "{$row['id']}".
    '">'.
    "{$row['name']}</a><br />" .
    "{$row['content']}<br />" .
    "Posted: {$row['author']}".
    '<hr color="white" size="1" />'
    to show all the posts from my database, what I was triyng to do was have a horizontal rule between each post but not below the last post or above the first post. Anyone know how to do this?

    -Thanks
    JustBlaze

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Not sure if this will help, but it might trigger an idea ...

    Always put the line on top, just don't do it for the 1st one.
    (that's the part I'm not sure about ... ignoring the 1st one).

    if(... it's the first one ...){
    echo"
    <div style=\"width:200px;\">
    <a href=\"{$row['id']}\">{$row['name']}</a><br />
    {$row['content']}<br />
    Posted: {$row['author']}
    </div>
    ";
    }
    else{
    echo"
    <div style=\"border-top:1px solid #000; width:200px;\">
    <a href=\"{$row['id']}\">{$row['name']}</a><br />
    {$row['content']}<br />
    Posted: {$row['author']}
    </div>
    ";
    }

  • Users who have thanked mlseim for this post:

    JustBlaze (11-12-2007)

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    yea that gave me an idea, but I need to figure out how to detect if it's the first or last post on the page.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,074
    Thanks
    2
    Thanked 320 Times in 312 Posts
    If this is in a loop, before the loop, set a variable - $first_pass = true; Then in the loop you can check if($first_pass){do something for the first pass} or if(!$first_pass){do something if not the first pass} and simply set the variable to false near the end of the loop after you have done any conditional testing of its value - $first_pass = false;
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    JustBlaze (11-12-2007)

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The other solution is using an array, although with large texts this might not be as useful as the others. But it might help with other situations, as in my opinion this is cleaner code.

    PHP Code:
    $list = array();
    $list[] = "<p>Item 1</p>";
    $list[] = "<p>Item 2</p>";
    $list[] = "<p>Item 3</p>";

    echo 
    implode("<hr />"$list); 

  • Users who have thanked aedrin for this post:

    JustBlaze (11-12-2007)

  • #6
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Well I thought what CFMaBiSmAd said would work but now that I thought about it I don't think it will.
    PHP Code:
    $query  "SELECT id, name, content, author FROM blog";
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
    {
    echo 
    '<a href="'.
    "{$row['id']}".
    '">'.
    "{$row['name']}</a><br />" .
    "{$row['content']}<br />" .
    "Posted: {$row['author']}"
    That's the important part of my code that shows the blog entries.
    Still can't figure out how to do what I'm trying to do, as the supplied
    examples don't seem to fit the situation.
    Last edited by JustBlaze; 11-12-2007 at 09:32 PM.

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    My feelings are hurt. I gave an equally good solution and I get no thanks.

    I call firstism! (Discrimination based on being the first or not)

  • #8
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by aedrin View Post
    My feelings are hurt. I gave an equally good solution and I get no thanks.

    I call firstism! (Discrimination based on being the first or not)
    I'm lost on how to use your suggestion though, I appreciate you posting it but can you help me implement it?, I'm still new to php so certain things I'm lost on.

    edit: does the same thing as mine and has the same issues
    Last edited by JustBlaze; 11-12-2007 at 11:07 PM.

  • #9
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    PHP Code:
    $i 0
    if ( $i )
    {
    display row with no bar
    }else{
    display row with bar

    You don't need to find the last row if the bar is above the tine of text, search for off by one error to understand why.

  • #10
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    not sure how that's suppose to work :/

    this is how it looks using this code:
    PHP Code:
    <?php 
    $v 
    $_GET['v'];
    if (
    $v == "cat")
    { echo 
    "Coming Soon";}
    else 

    include 
    'config.php';
    include 
    'opendb.php';

    $query  "SELECT id, name, content, author FROM blog";
    $result mysql_query($query);
    $num_rows mysql_num_rows($result);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        echo 
             
    '<a href="'.
             
    "{$row['id']}".
             
    '">'.
             
    "{$row['name']}</a><br />" .
             
    "{$row['content']}<br />" .
             
    "Posted: {$row['author']}<hr />"
     }
    include 
    'closedb.php';
    ?>


    I'm trying to have it so that there's no horizontal rule under the last post/row that is displayed.

    -Hope this lets you better understand my issue.

  • #11
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    ok... I have misunderstood you again what exactly do you mean by horizontal rule?

    I though you meant an image or such to divide the results up e.g

    result1
    --------
    result2
    --------
    result3

  • #12
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,074
    Thanks
    2
    Thanked 320 Times in 312 Posts
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #13
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    PHP Code:
    <?php 
    $v 
    $_GET['v'];
    if (
    $v == "cat")
    { echo 
    "Coming Soon";}
    else 

    include 
    'config.php';
    include 
    'opendb.php';
     
    $query  "SELECT id, name, content, author FROM blog";
    $result mysql_query($query);
    $num_rows mysql_num_rows($result);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        
    $i 0
        
    if ( $i )
        {
             echo 
             
    '<a href="'.
             
    "{$row['id']}".
             
    '">'.
             
    "{$row['name']}</a><br />" .
             
    "{$row['content']}<br />" .
             
    "Posted: {$row['author']}"
        
    $i++;

    }else{
    echo 
    "<hr />" 
             
    '<a href="'.
             
    "{$row['id']}".
             
    '">'.
             
    "{$row['name']}</a><br />" .
             
    "{$row['content']}<br />" .
             
    "Posted: {$row['author']}"
    }  
     }
    include 
    'closedb.php';
    ?>
    I was right with what I thought you meant I just didn't explain my code properly or add the $i++;

  • Users who have thanked psykx for this post:

    JustBlaze (11-13-2007)

  • #14
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Tried that but it shows a horizontal rule above the first post, the problem that I'm having with all these methods is that it runs the while statement multiple times until there are no more rows left to show, so it puts the horizontal rule on all of the posts.

    -Thanks for your effort though
    JustBlaze

  • #15
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    the above code didn't error?

    it should be $i = 0; not $i = 0
    and if ( $i == 0 )

    you really should do simple syntax checking.


  •  
    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
    •