Help with basic function for returning forum threads

Discussion in 'Web Design and Development' started by Cabbit, Nov 17, 2008.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    The site i am creating just now has a forum and many comments sections, i realized since the code was identical in order to keep them all consistent i could make a basic function that could just be called for so that there would only be one port for bug fixing and design updates.

    my problem is that these two lines in the function cause it to stop and i do not know why as it worked fine outside the function.

    PHP:
        // The edit button //
        
    if ($usrname == $row["username"] || $session->userlevel == '8' || $session->isAdmin()) 
        { 
            echo 
    '<span class="button"><a href="edit_comment.php?id='.$row["id"].'">Edit</a></span>';
        }

        
    // The delete button //
        
    if ($session->userlevel == '8' || $session->isAdmin()) 
        {
            echo 
    '<span class="button"><a href="show_comments.php?id=$id&delete='.$row["id"].'">Delete</a></span>';
        }


    Here is the full code

    PHP:
    <?php
    function forum_thread($row$number$edit_button$delete_button) {
        
    // Function for displaying a thread //
        
    global $session;
        
    // Begin output of the thread //
        // ************************** //
        
        // The container //
        
    echo '<div class="thread_container">';
            
        
    // Send the date for processing and return it //
        
    $date date_convert($row['date']);
        
    // End of date processing //
        
        // The header contains the subject, post date and the thread number //
        
    echo '    <div class="thread_title">
                    <div class="thread_subject">Subject: '
    .$row["subject"].'
                    <span class="thread_number">'
    .$date.'</span></div>  
                    <span class="thread_right">#'
    .$number.'</span>
                </div>'
    ;
        
        
    // Returns ether the users avatar or the no avatar if the user has none
        
    if ($row['avatar'] == '' || $row['avatar'] == 'not entered'
        { 
        }
        else 
        { 
            
    $avatar '<div class="thread_avatar"><img width="110px" src="../images/avatars/'.$row["avatar"].'" alt="'.$row["avatar"].'\'s avatar" /></div>';
        }
        
        
    // The profile and mail me variables //
        
    $mail_me '<a href="../mail/compose.php?user='.$row['username'].'">
                        <img src="../images/layout/mail_new.png" alt="mail"  height="14px" hspace="5px"> 
                    </a>'
    ;
        
    $my_profile '<a href="../user_service/user_profile.php?&user='.$row['username'].'">
                            <img src="../images/layout/friends.jpg" alt="profile" height="20px"> 
                        </a>'
    ;
                        
        
    // inner containing element for the thread //
        
        
    echo '<div class"thread_inner_container">';
        
        
    // Returns the poster information //
        
        
    echo '<div class="thread_userstats">';
        echo 
    $avatar;
        echo 
    '<div class="thread_username">'.$row['username'].'</div>';
        echo 
    '<div class="thread_userrank">'.user_rank($row['username']).'</div>';
        echo 
    '<div class="thread_usertools">'.$mail_me$my_profile.'</div>';
        echo 
    '</div>';
        
        
    // The body text of the thread, comment for comments and body for forum topics //
        
    echo '<div class="thread_body">';
        if (isset(
    $row["comment"]))
        {
            
    $body $row["comment"];
        }
        else
        {
            
    $body $row["post"];
        }
        
    $quote_start "\[quote\]";
        
    $string $body
        
    $new_string preg_replace("/".$quote_start."/i""<div class=\"quote\">"$string);
        
    $stingie $new_string;
        
    $quote_end "\[\/quote\]";
        
    $com_string preg_replace("/".$quote_end."/i""</div>"$stingie);
        if (
    $com_string == "") {
            
    $com_string $body;
        }
        echo 
    stripslashes($com_string);
        
        
    // The button box //
        
    echo '<div class="thread_button_box">';

        
    // The edit button //
        
    if ($usrname == $row["username"] || $session->userlevel == '8' || $session->isAdmin()) 
        { 
            echo 
    '<span class="button"><a href="'.$edit_button.'">Edit</a></span>';
        }

        
    // The delete button //
        
    if ($session->userlevel == '8' || $session->isAdmin()) 
        {
            echo 
    '<span class="button"><a href="'.$delete_button.'">Delete</a></span>';
        }
        
        
    // End of the button box //
        
    echo '</div>';
        
        
    // End of Thread body //
        
    echo '</div>';
        
        
    // end of the inner container //
        
    echo '</div>';
        
        
    // End of container //
        
    echo '</div>';
    // End function //
    }
    ?>
     
  2. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #2
    What forum software is this, and are you adding all that code into one of the forum scripts as a hack?

    You're referencing class object members (i.e. $session->userlevel) which is normally used within a class but I don't see the class being initiated anywhere in your function (different scope). If you're hacking within the forum framework then at the top of your function add: "global $session;" otherwise initiate the class and whatever dependencies it might have to use the forum framework outside of the forum. Same for "$usrname" assuming it is spelled right, it's not referenced anywhere else in the function so global import that as well, guessing.

    -jim
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    The forum software is my own, not a off the shelf. Though it has grown cumbersome and i keep forgetting how some stuff works. I think its smack on the nose with the scope there.
    The other functions and classes are all in another file that i call at the start of each page so i just need to call them into this function now. thanks for the help.
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    Actually, it's great to see someone creating and customizing their own forum and showing some original thought. Bravo to you! BTW, in all seriousness - as a code developer you should add more comments into your code. And with respect to class creation, there are numerous tools available to help you document classes for easy reference later on, as well all forget our own stuff as time passes. Been there, done that!

    :)

    -jim
     
  5. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #5
    Thanks for the complement and i will work on adding more comments to my coding, though i the comments in that code is well beyond what i usually do.
     

Share This Page