Tutorial System - What I Need/What I Know

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Tutorial System - What I Need/What I Know

Post by xEzMikex »

Allright my goal is to make a tutorial system with the following features.

-Add tutorials(80%)
-Delete tutorials(80%)
-Automatically add tutorial names to the list(40%)
-Have a count of how many tutorials there is beside the subject name(5%)

The percentage beside them is how sure I am that I CAN do the tast.

Details - I know how to add information and display it on a single page, Well I would like to create some sort of code so when I add tutorials to the database I can display it by a link, Somthing like this "www.test.com/tutorials?php=1" I want it so this will show the tutorial on the page with my template.

This is the code im working with so far.

Code: Select all

<?php
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM phptuts ORDER BY `id` DESC";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    while($row = mysql_fetch_assoc($result)) {

	    echo "<b>Tutorial By : </b>"."<b>".$row['name']."</b><br>";
        echo "<b>Date : </b><b>".$row['date']."</b><br><br>";
		echo $row['content']."<br><br>";
    }
}
else {
    echo "No rows found!";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);
?>
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

First of all you need to add a link to the page that will display the results.

Change the following code to:

Code: Select all

echo "<a href = 'view_tutorial.php?id=".$row['id']."'><b>Tutorial By : </b>"."<b>".$row['name']."</b></a><br>"; 
        echo "<b>Date : </b><b>".$row['date']."</b><br><br>"; 
        echo $row['content']."<br><br>";
This will create a link to the next page (view_tutorial.php?id=1 for example)

next create a new page called view_tutorial.php and adapt your sql query like this:

Code: Select all

<?php 
// open connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 

$id = $_GET['id'];

// create query 
$query = "SELECT * FROM phptuts where id = '$id'"; 



        echo "<b>Tutorial By : </b>"."<b>".$row['name']."</b><br>"; 
        echo "<b>Date : </b><b>".$row['date']."</b><br><br>"; 
        echo $row['content']."<br><br>"; 



// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 
?>
You would also want to consider validating the input as users could add anything to the URL e.g view_tutoral.php?id=something else

You would also need to add something if the result is empty.
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Post by xEzMikex »

Thanks alot, I undestand that and am going to try it now. When I am done I will post my results.
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Post by xEzMikex »

Allright well I ran into some problems. I got the link and all but when I click the link it doesnt work.

EDIT

Nevermind the file name was spel wrong... But now there is no data in the feilds like "Posted By:" and "Date:" its empty beside them.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

sorry I forgot to execute the query please add this code back in

Code: Select all

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
$row = mysql_fetch_assoc($result);

        echo "<b>Tutorial By : </b>"."<b>".$row['name']."</b><br>"; 
        echo "<b>Date : </b><b>".$row['date']."</b><br><br>"; 
        echo $row['content']."<br><br>";
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Post by xEzMikex »

Thank you so much! I need to fix it up a little bit and add some more feilds but soon as it is done and on the net I will link you, Thank you so much.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

No Probs. Anything to help
Post Reply