Jump to content

BBCode on Tutorial System


iScott

Recommended Posts

Hi. I have this tutorial system, and I have tried and tried to get bbcode working on it. I have a bboce file which when I include on the file, my comments just get an error, but when I remove it its fine.

 

I also ignored this for a bit and tried to get the bbcode to work on the tutorials, but I can't seem to get it to work. Can someone please have a play around with this and see if they can get it to work? It will be much appreciated. Below are the codes and errors I get.

 

Comments Error (When include 'bbcode.php'; is insterted)

Warning: mysql_fetch_array() [function.mysql-fetch-array]: The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH. in /home/iscott/public_html/tutorials.php on line 316

 

BBCode.php

<?
function bbcode($content){ // our nice bbcode function
$content = nl2br(htmlspecialchars($content)); //  our message that we want to put the bbcode in

    $bbcode = array(  // The BBCode tags
       "'[b](.*?)[/b]'", // Bold Tag [b]Bold[/b]
       "'[i](.*?)[/i]'", // Italics Tag [i]Italics[/i]
       "'[u](.*?)[/u]'", // Underline Tag [u]Underlined[/u]
       "'[strike](.*?)[/strike]'", // Stroked Out Tag [strike]Strike[/strike]
       "'[img=(.*?)]'", // Image Tag
       "'[url=http://(.*?)](.*?)[/url]'", // Url Tag [url=http://yoursite.com]A website =D[/url]
       "'[url=http://(.*?)](.*?)[/url]'", // Another Url Tag [url]yoursite.com[url]
        );

      $html = array( // The HTML counter part of the tags
        "<strong>1</strong>", // Bold
        "<em>1</em>", // Italics
        "<u>1</u>", // Underlined
        "<strike>1</strike>", // Stroked out text
        "<a href='1' target='_BLANK'>2</a>", // Url 1 opens in a new window
        "<a href='1' target='_BLANK'>1</a>", // Url 2 opens in a new window
        "<img border='0' src='1'>", // Image
    
       );

      $content = preg_replace($bbcode, $html, $content); // replaces all BBCode tags with  their HTML counter parts
    return nl2br($content);
}

?>

 

Tutorials.php

<? include'header.php'; ?>

<center>
<div id="container">
<div class="content-top">
iScott » Tutorials <?
include 'includes/config.php';
$id = (int)$_GET['id'];
$query = mysql_query("SELECT title FROM tutorials WHERE id = '$id'");
if(!$query) { die(mysql_error()); }
$row = mysql_fetch_array($query);
echo"»  ".$row[title].""; 
?>
</div>

<? include'sidebar.php'; ?>
<div class="content-right">
<?php
//------------------------------------------
include "includes/config.php";
include "bbcode.php";

//------------------------------------------

//------------------------------------------
//echo out a navigation panel

//------------------------------------------

//------------------------------------------
//begin main navigation (tutorials.php?action=)

switch ($_GET['action']) {
        //------------------------------------------
        //this case adds a tutorial.
        //pretty self-explanitory
        //------------------------------------------
    case "addtutorial":
        //if the form to enter a new
        //tutorial hasn't been submitted,
        //show it
        if (!isset($_POST['add_tutorial'])) {
            echo "
		<div class=\"content-box\">
<h1>Add Tutorial</h1>

        <table border='0' cellpadding='0' cellspacing='0' width='500'>
        <form action='$self?action=addtutorial' method='post'>
            <tr>
                <td>Name:</td>
                <td><input type='text' name='name'></td>
            </tr>
            <tr>
                <td>Title:</td>
                <td><input type='text' name='title'></td>
            </tr>
		<tr>
                <td>Icon:</td>
                <td><input type='text' name='icon'><br />
<a target='new' href='icon_upload.php'>Upload Icon</a></td>
            </tr>
            <tr>
                <td>Category:</td>
                <td>
                    <select name='category'>
                        ";
            //now what we are doing here is looping through
            //the categorys table and getting all the
            //categorys and putting them into a select
            //so the user can select which category
            //the tutorial is on
            $query = mysql_query("SELECT * FROM tutorials_categorys ORDER BY id DESC") or die(mysql_error
                ());
            while ($row = mysql_fetch_array($query)) {
                echo "<option value='$row[id]'>$row[category]";
            }
            echo "
                    </select>
                </td>
            </tr>
                <tr>
                <td>Tutorial:</td>
                <td><textarea name='tutorial' cols='40' rows='10'></textarea></td>
            </tr>
            </tr>
                <tr>
                <td>Short Description:</td>
                <td><textarea name='short_description' cols='40' rows='2'></textarea></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type='text' name='email' maxlength='50'></td>
            </tr>
            <tr>
                <td>Show Email?</td>
                <td><input type='checkbox' name='show_email' value='1' checked></td>
            </tr>
            <tr>
                <td colspan='2'><center><input type='submit' name='add_tutorial' value='Submit New Tutorial'></center></td>
            </tr>
        </form>
        </table>
</div>
        ";
        }
        //else, error check, enter it
        elseif (isset($_POST['add_tutorial'])) {
            $name = mysql_real_escape_string(strip_tags($_POST['name']));
            $title = mysql_real_escape_string(strip_tags($_POST['title']));
		$icon = mysql_real_escape_string(strip_tags($_POST['icon']));
            $category = mysql_real_escape_string(strip_tags($_POST['category']));
            $tutorial = nl2br (strip_tags (mysql_real_escape_string($_POST['tutorial'])));
            $short_description = mysql_real_escape_string(strip_tags($_POST['short_description']));
            $email = mysql_real_escape_string(strip_tags($_POST['email']));
            $show_email = mysql_real_escape_string($_POST['show_email']);
            $date = date("d M Y");
            $time = time();

            //we begin error checking....
            $error_msg = array();
            if (empty($name)) {
                $error_msg[] = "Please insert a name!<br />";
            }
            if (empty($title)) {
                $error_msg[] = "Please insert a title!<br />";
            }
            if (empty($category)) {
                $error_msg[] = "Please insert a category!<br />";
            }
		  if (empty($icon)) {
                $error_msg[] = "Please insert a icon!<br />";
            }

            if (empty($tutorial)) {
                $error_msg[] = "Please insert the tutorial text!<br />";
            }
            if (empty($short_description)) {
                $error_msg[] = "Please insert a short description!<br />";
            }
            if (empty($email)) {
                $error_msg[] = "Please insert an email!<br />";
            }
            //print the errors, if any
            if (count($error_msg) > 0) {
                echo "<strong>ERROR:</strong><br>n";
                foreach ($error_msg as $err)
                    echo "$err";
            }
            //everythings ok, insert it to the DB
            else {
                $sql = "INSERT INTO tutorials (submitter, text, short_description, title, icon, cat_id, date_submitted, time_submitted, show_email, email, is_validated) VALUES ('$name', '$tutorial', '$short_description', '$title', '$icon', '$category', '$date', '$time', '$show_email', '$email', '0')";
                mysql_query($sql) or die(mysql_error());
                echo "<div class='content-box'>
			<h1>Added</h1>
			<p>Tutorial added for review!</p></div>";
            }
        }
        break;

        //------------------------------------------
        //this case gets the specified [iD] in the url
        //(tutorials.php?action=viewcategory&id=[iD]
        //and gets all the tutorials listed under that
        //category ID (cat_id)
        //------------------------------------------
    case "viewcategory":
        //if there is an ID given...
        if ($_GET['id']) {
            //get the id, put it into a variable, cast to an INT
            //(for security purposes)
            $id = (int)$_GET['id'];
            $query = mysql_query("SELECT * FROM tutorials WHERE cat_id = '$id' AND is_validated = '1'") or
                die(mysql_error());

            //if no results, show that there are no tutorials
            //for that category
            if (mysql_num_rows($query) == 0) {
                echo "
						<div class=\"content-box\">
<h1>Error</h1>
			No tutorials for this category yet!
			</div>";
            }
            //else, there is..show em
            else {
                echo "
						<div class=\"content-box\">
			<h1>Tutorials</h1>";
                //loop through the tutorials
                //show all tutorials
                while ($row = mysql_fetch_array($query)) {
                    echo "
				<div class='tutorial-list'>
<h1><a href='$self?action=viewtutorial&id=$row[id]'>$row[title]</a></h1>
<img src=\"images/icons/tutorials/$row[icon]\" alt=\"$row[title]\" title=\"$row[title]\" align=\"left\" />
$row[short_description]<br /><br /><br />
<span style='float: right'>
<div class=\"content-small-text\">Submitted: $row[date_submitted] | 
Views: $row[views] | 
Submitter: $row[submitter]</div></span>
<div style='clear: both'></div>
</div>

                ";
                }
			echo "</div>";
            }
        } else {
            echo "Please give me a category ID!";
        }
        break;

        //------------------------------------------
        //this case gets the given [iD]
        //action=viewtutorial&id=[iD]
        //and gets that tutorial ID from the database
        //and displays it!
        //------------------------------------------
    case "viewtutorial":
        //if there is an ID given..
        if ($_GET['id']) {
            //set $id to the URL id, cast to an INT
            //for security purposes
            $id = (int)$_GET['id'];

            //query the database
            $query = mysql_query("SELECT * FROM tutorials WHERE id = '$id'  ORDER BY id DESC") or die(mysql_error
                ());

            //if no rows returned...
            if (mysql_num_rows($query) == 0) {
                echo "That ID is not in the database!";
            }
            //else, show it!
            else {
                //update the views for this tutorial!
                $update_views = mysql_query("UPDATE tutorials SET views = views + 1 WHERE id = '$id'") or
                    die(mysql_error());

                //loop through the database
                while ($row = mysql_fetch_array($query)) {
                    echo "
<div class=\"tutorial-box-left\"><h1>$row[title]</h1>
<img src=\"images/icons/tutorials/$row[icon]\" alt=\"$row[title]\" title=\"$row[title]\" align=\"left\" />
<div class=\"content-small-text\">
Created on: $row[date_submitted]<br>
Created by: $row[submitter]<br>
Views: $row[views]
</div>
</div>
<div class=\"tutorial-box-right\"><h1>Description</h1>
$row[short_description]
</div>
<div style=\"clear: both\"></div>


<div class=\"content-box\" id=\"dog\">
<div class=\"content-small-text\" >
<div style=\"float: left\"><a href=\"javascript:collapse2.slideit()\">View Tutorial +</a></div>
</div><br />

<p>";
echo "$row[text]";
echo"</p>

</div>
<script type=\"text/javascript\">

//Syntax: var uniquevar=new animatedcollapse(\"DIV_id\", animatetime_milisec, enablepersist(true/false), [initialstate] )
var collapse2=new animatedcollapse(\"dog\", 1000, true)

</script>


                    ";
                    //----------------------------
                    //this part of the code
                    //checks to see if the submitter
                    //wants an email left for support
                    //----------------------------
                    if ($row['show_email'] == 1) {
                        echo "
           
                        ";
                    }
                    echo "
          ";
                }
                //--------------------------------
                //this is where we loop through the
                //comments table to show all the
                //comments for this tutorial
                //--------------------------------
                $comments = mysql_query("SELECT * FROM tutorials_comments WHERE tut_id = '$id' ORDER BY id ASC") or
                    die(mysql_error());

                //if there are no comments..
                if (mysql_num_rows($comments) == 0) {
                    echo "
<div class=\"content-box\">
<h1>Comments</h1>
The are no comments.
</div>
                ";
                }
                //else, show them!
                else {
                    
				echo "
			<div class='content-box'>
<h1>Comments</h1>
<ol class='comments'>
";//loop through them
$query = mysql_query("SELECT * FROM tutorials WHERE cat_id = '$id' AND is_validated = '1'") or
                die(mysql_error());
                    while ($row = mysql_fetch_array($comments, $query)) {
				echo"
<li><strong>$row[submitter]</strong> - Posted on $row[date_submitted]<br>
$row[text]</li>
";}
echo "
</ol>
</div>";
                                    }
                //show the form to enter comments
                echo "
			<div class=\"content-box\">
<h1>Add a Comment</h1>
<form action='$self' method='post'>
Name:<br />
<input type='text' name='name' maxlength='25'><br /><br />

Comment:<br />
<textarea name='message' cols='40' rows='10'></textarea><br />
<input type='submit' name='add_comment' value='Add Comment'> <span class='content-small-text'><em>[Please note that once submit has been clicked the comment <strong>has</strong> been submitted.]</em></span>
</form>
</div>


            ";
                //-----------------------------
                //if the comment submit form
                //HAS been submitted, enter info
                //to the database.
                //-----------------------------
                if (isset($_POST['add_comment'])) {
                    //strip all HTML tags
                    //and get rid of any quotes to prevent
                    //SQL injection
                    $message = mysql_real_escape_string(strip_tags($_POST['message']));
                    $name = mysql_real_escape_string(strip_tags($_POST['name']));
                    $time = time();
				$date = date("d M Y");


                    //use an array to store all error messages
                    $error_msg = array();
                    if (empty($message)) {
                        $error_msg[] = "Please enter a message!<br />";
                    }
                    if (empty($name)) {
                        $error_msg[] = "Please enter a name!<br />";
                    }
                    //print the errors
                    if (count($error_msg) > 0) {
                        echo "<strong>ERROR:</strong><br>n";
                        foreach ($error_msg as $err)
                            echo "$err";
                    }
                    //else, everything is ok, enter it in the DB
                    else {
                        $query = mysql_query("INSERT INTO tutorials_comments VALUES (NULL,'$id','$name', '$message', '$time', '$date')") or
                            die(mysql_error());

                    }
                }
                echo "</table>";
            }
        }
        //if not..
        else {
            echo "No ID specified!";
        }
        break;

        //------------------------------------------
        //default case, this is shown default
        //in this instance, we are going to make the default case show
        //all the categories that you can view tutorials on
        //------------------------------------------
    default:
        $query = mysql_query("SELECT * FROM tutorials_categorys") or die(mysql_error());

        //if the number of rows returned is 0, then say, no categories
        if (mysql_num_rows($query) == 0) {
            echo "
					<div class=\"content-box\">
<h1><font style='color: #FF0000'>Error</font></h1>
		No tutorial categories currently!
		</div>";
        }
        //if anything else, then there has to be categories. show em.
        else {
            echo "
					<div class=\"content-box\">
		<h1>Tutorial Categories</h1> ";
            //while loop to loop through the database and display results!
            while ($row = mysql_fetch_array($query)) {
                echo "
			<a href='$self?action=viewcategory&id=$row[id]'>$row[category]</a><br />
<div class='content-small-text'>$row[description]</div>


            ";
            }
		echo "</div>";
        }
        break;
}
//end navigation
//------------------------------------------

?>


<? include'ads.php' ; ?>

<? include'footer.php'; ?>

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/85236-bbcode-on-tutorial-system/
Share on other sites

preg_replace: you have to use \\ in front of the number when you want to replace the text

 

$html = array( // The HTML counter part of the tags
        "<strong>\\1</strong>", // Bold
        "<em>\\1</em>", // Italics
        "<u>\\1</u>", // Underlined
        "<strike>\\1</strike>", // Stroked out text
        "<a href='\\1' target='_BLANK'>\\2</a>", // Url 1 opens in a new window
        "<a href='\\1' target='_BLANK'>\\1</a>", // Url 2 opens in a new window
        "<img border='0' src='\\1'>", // Image
    
       );

It still didn't work. I still get this error on the comments.

Warning: mysql_fetch_array() [function.mysql-fetch-array]: The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH. in /home/iscott/public_html/tutorials.php on line 316

 

I think there has to be something like bbcode() around the tutorial but I have tried it and I just get an error.

 

This is my website. Thats what it does...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.