473,287 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


Viewing Posts

By Blair Ireland
Senior Editor, TheScripts.com

We will be making use of functions in this application. For those of you not aware of what a function in PHP is, it basically is a program within the program. Generally it allows you to save time writing code, and makes your programs more efficient. User-Defined functions in PHP look like the following;

function yourfirstfunction ($arg_1, $arg_2) {
    echo "Example function.\n";
    $retval = $arg_1 . " " . $arg2;
    return $retval;
}

$arg_1 and $arg_2 are the two arguments you sent into the function when calling it. $retval is the valued returned by the function, which, in this case, is a string with your first and second arguments, separated by a space.

Our application will consist of a few files, one of which will contain all of our functions in it. Why? This makes bug-fixes easy, and efficiently done. Not only do you really have to look in one file, but, updating your code is easy as well. You should adopt this method of coding into future projects as well, I know you will appreciate it when the time comes.

There are two functions that I always now seem to have, showheader(), and showfooter(). They just show a header, or a footer of your html. Therefore, you can lay off of having to copy and paste it all over, and just call the function. The only argument I have in these two is in the showheader function, which is the page title.

function showheader($title) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> <?php echo $title ?> </TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
<?php
}

function showfooter() {
?>
</BODY>
</HTML>
<?php
}

Time to create a function now to read our messages first. This will contain all of the table HTML as well.

function viewpost ($topicID) {

Here we pass into the function our $topicID, which defines what topic we are speaking of.

$topic_query = mysql_query("SELECT TopicName FROM topics WHERE (ID=$topicID)");

Using this $topicID, we can select the name of this topic by it's ID Number.

$topic = mysql_fetch_array($topic_query);

$topic is now the array containing the information from our query, which, in this case, is just the topic name.

?>
We close the PHP tag so we can just output normal HTML

<TABLE BORDER=0 WIDTH=100% CELLSPACING=3 CELLPADDING=5>
    <TR>
        <TD>
        <FONT COLOR="#000000" FACE="Arial,Verdana,Helvetica" size=-1>
        <b>Topic: </b>
        <BR><?php echo $topic['TopicName'] ?>
        <P>
        <a href="http://www.yoursite.com/index.php3"><FONT COLOR="#000000">Return Back to Topic Listing</a>
        <P>
        <a href="http://www.yoursite.com/add-post.php3?topicID=<?php echo $topicID ?>"><FONT COLOR="#000000">Make a Post</a>
        </TD>
    </TR>
</TABLE>
<P>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
    <TR VALIGN=TOP ALIGN=LEFT>
        <TD WIDTH=100%>
            <TABLE BORDER=0 BGCOLOR="#000000" CELLSPACING=1 CELLPADDING=1 WIDTH=100%>
                <TR>
                    <TD COLSPAN=3 BGCOLOR="#C0C0C0" WIDTH=100%>
                        <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=100%>
                            <TR>
                                <TD>
                                <P><B><FONT COLOR="#000000" FACE="Trebuchet MS,Arial,Helvetica">Topic Posts</FONT></B>
                                </TD>
                            </TR>
                        </TABLE>
                    </TD>
                </TR>
<?php
$post_query = mysql_query("SELECT * FROM posts WHERE (TopicID='$topicID') ORDER BY TimeStamp");

Back to the PHP part.... and now we have to do a query to determine what posts are in this topic. It outputs them by their date, the oldest being first.

    while ($post = mysql_fetch_array($post_query)) {

This loop will repeat for all of the returns from the query.

?>
    <TR>
    <TD WIDTH=82% BGCOLOR="#FFFFFF" HEIGHT=28 VALIGN=TOP>
        <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=100%>
            <TR>
                <TD>
                <P><FONT SIZE="-1" FACE="Trebuchet MS,Arial,Helvetica"><?php echo $post['Post'] ?></FONT>

Here we just output the post saved in our database.

                </TD>
            </TR>
        </TABLE>
    </TD>
    <TD WIDTH=18% BGCOLOR="#C0C0C0" VALIGN=TOP>
        <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=100% >
            <TR>
                <TD>
                <P ALIGN=center><FONT SIZE="-1" FACE="Trebuchet MS,Arial,Helvetica" color=#000000>Posted By: <A HREF="mailto:<?php echo $post['Email'] ?>"> <FONT COLOR=#000000><?php echo $post['Name'] ?></A>

And here is our author of this post.

                <BR><BR>
                <A HREF="http://www.yoursite.com/edit-post.php3?postID=<?php echo $post['ID'] ?>"><FONT COLOR=#000000>Edit Your Post</FONT>

All message boards seem to have this feature now. If you mess up in your post, you can edit it. This file will be discussed later.

                <BR><BR>
                <A HREF="http://www.yoursite.com/delete-post.php3?postID=<?php echo $post['ID'] ?>"><FONT COLOR=#000000>Delete Your Post</FONT>

Same deal, just deleting your post. Also, this will be discussed later.

                </TD>
            </TR>
        </TABLE>
    </TD>
</TR>
<?php
}

Here we end our loop.

if (mysql_num_rows($post_query) < 1) {
?>
<TR height=300>
    <TD WIDTH=100% BGCOLOR="#FFFFFF" HEIGHT=28 VALIGN=TOP colspan=2>
    <CENTER><FONT SIZE="-1" FACE="Trebuchet MS,Arial,Helvetica">
    <BR><B>There Are No Posts Currently For This Topic</B><BR><BR></CENTER>
    </TD>
</TR>
<?php } ?>

Wondering why this bit of code is here? If the result of our query returns less then 1 result, that means the topic does not have any posts as of yet. We just let everyone know it.

</TABLE>
</TD></TR>
</TABLE>
<?php
}
?>

And our function is complete. It is a very simple one indeed, nothing really going on here. It was nearly all made up of HTML really, with the queries as needed. So we plop that into our function file, lets call it, um, functions.php3. Creative am I not? You can view it here

« What You Need Add or Edit Post »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.