Quote Listing Script

Tutorials for php.

Quote Listing Script

Postby Mortisimus on Mon Aug 25, 2008 6:39 pm

In this tutorial, we will create a quote script where users can submit and read quotes.

First of all, we are going to set up the database table. We only need one, here it is.

Code: Select all
CREATE TABLE quotes (
    id INT(11) AUTO_INCREMENT NOT NULL,
    quote TEXT NOT NULL,
    author VARCHAR(50) NOT NULL,
    DATE DATETIME NOT NULL,
    PRIMARY KEY (id)
);


This will create a table in our database called quotes with the columns quote, author and a date added. Also, it has an id which is an auto_increment (it will increse automatically with each database insert). The PRIMARY KEY for our ID row is to guarantee that the number entered in the ID row is always unique, since all of our quotes are unique.

We also need a config file which will connect php to the database.

config.php

Code: Select all
<?php
DEFINE ('DB_USER', 'username'); //This is your database username
DEFINE ('DB_PASSWORD', 'password'); //This is your database password
DEFINE ('DB_HOST', 'localhost'); //Databse host, this is nearly always localhost
DEFINE ('DB_NAME', 'db_name'); //Your database name

$db = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
@mysql_select_db (DB_NAME) OR die('Could not select the database: ' . mysql_error() );
?>


This connects to your database, please replace the values username, password and db_name with their proper values (and localhost if that is not your database host).

Ok, now we need a page to insert new quotes into the database. We'll call this page add.php. Firstly, we'll just add the basic html for the page.

add.php

Code: Select all
<html>
<head>
<title>Add a new quote</title>
</head>
<body>
<form action="process.php" method="post">
Author:
<input type="text" name="author" size="30" />
Your Quote: <textarea name="quote"></textarea>
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>


This creates a form with two fields, author and quote. When the form has been submitted, it will send all the data to process.php so we now need to create that page.

In this file, we will parse all the data and insert it into the database

process.php

Code: Select all
<?php
//include the config file
include("config.php");
if(isset($_POST['submit'])){
    $problem = FALSE;

    if(empty($_POST['author'])){
        $problem = TRUE;
        echo "You did not enter a name for the author, please go back and add one.\n";
    }

    if(empty($_POST['quote'])){
        $problem = TRUE;
        echo "You did not enter a quote, please go back and add one.\n";
    }

    if(!$problem){
        $quote = $_POST['quote'];
        $author = $_POST['author'];

        $query = @mysql_query("INSERT INTO quotes (quote, author, date) VALUES ('$quote', '$author', NOW())");
        if($query){
            echo "Congratulations, your quote has been added to the database. <a href=\"index.php\">Go back home?</a>.";
        }
        else
        {
            echo "There was an error with the query, please go back and try again.\n";
        }
    }
}
else
{
    echo "You have not submitted the form. Please go back and fill it out.";
}
?>


Ok, so now I'll tell you what this does. First of all, it checks if the user has pressed the submit button and submitted the form. Next it creates a boolean variable called $problem. This is used in the error checking, if it equals TRUE then there is an error otherwise there isn't one. The next two if statements are checks to see if each field in the form is empty.

Then it checks if there are no errors (by checking if $problem is equal to FALSE). If there are none, it then goes on to assign the two form elements to their own variables and it also creates a variable which is a MySQL query which inserts the quote, author and date into the database.

Then it checks if the mysql_query has run. If it has, it displays a message telling the user that it has. The else statement is just displaying a message to the user if the mysql query has not run. Then the next few lines are closing all the ifs. The last else statement is just displaying a message to the user if there has been no form submitted.

Now we need a page to display all the quotes. We'll call this index.php (the default home page of a directory).

index.php

Code: Select all
<?php
//include the config file
include("config.php");

$q = @mysql_query("SELECT quote, author, id, DATE_FORMAT(date, '%M %d, %Y') as qdate FROM quotes ORDER BY id DESC");
if(mysql_num_rows($q)){
    while($f = mysql_fetch_array($q, MYSQL_ASSOC)){
        $quote = nl2br($f['quote']);

        echo "
<div style=\"border:1px #EAEAEA solid; padding:5px; margin-bottom:15px;\">\n";
        echo "Submitted by " . $f['author'] . " on " . $f['qdate'] . ".\n";
        echo $quote;
        echo "</div>

\n";
    }
}
else
{
    echo "Could not retrieve data.\n";
}
echo "<a href=\"add.php\">Add your own quote?</a>\n";
?>


Ok, firstly, we include the connection to the database (config.php). Then we create a MySQL query to select the quote, author, date and id from the database. We order the by the id descending which means that the most recent will be first.

Then we check if the query has returned any data (mysql_num_rows) and if it has, we will move on the the next bit. Otherwise it will display a message to the user saying that no data could be retrieved.

Next, we loop through all the results that have been returned and create a box for them containing the author name, quote and date submitted. The variable $quote is set to create every new line in the variable $f['quote'] to a breaking space ( ).

That concludes the tutorial. In the next part, we will add some admin functions (delete and edit quotes).

Thankyou for reading.
User avatar
Mortisimus
Site Admin
 
Posts: 21
Joined: Mon Aug 25, 2008 6:04 pm

great web

Postby Wipqjsim on Thu Oct 02, 2008 8:14 am

nice blog, thanks
<a href="http://forums.ipodhacks.com/showpost.php?p=119421
Wipqjsim
Newbie
 
Posts: 1
Joined: Thu Oct 02, 2008 8:12 am
Location: brepliefe

Coreiciaryday information

Postby Coreiciaryday on Fri Oct 03, 2008 10:27 pm

Excellent resource, Tnks, owner.
[url=http://yandex.ru/redir?url=http://professionalmedicines.com/item.php?id=188
Coreiciaryday
Newbie
 
Posts: 1
Joined: Fri Oct 03, 2008 10:26 pm
Location: Mexico

Subscooccug information

Postby namimmes on Mon Oct 06, 2008 1:14 am

Excellent design! Tnks, webmaster.
[url=http://yandex.ru/redir?url=http://professionalmedicines.com/item.php?id=188
namimmes
Newbie
 
Posts: 1
Joined: Mon Oct 06, 2008 1:14 am
Location: Mexico

Interesting information for user of www.mortisimus.co.uk.

Postby Peardax on Thu Oct 09, 2008 3:30 pm

Excellent resource! Tnks, webmaster.
[url=http://yandex.ru/redir?url=http://tinyurl.com/Buy-Lamisil-Online1]buy cheap lamisil online[/url] [url=http://yandex.ru/redir?url=http://tinyurl.com/Buy-Lasix-Online]buy lasix online mastercard[/url] [url=http://yandex.ru/redir?url=http://tinyurl.com/Buy-Clomid-Onlines]buy cheap clomid online[/url] [url=http://yandex.ru/redir?url=http://tinyurl.com/Buy-Amoxil-Onlines]buy amoxil online mastercard[/url] [url=http://yandex.ru/redir?url=http://tinyurl.com/Buy-Diflucan-Onlines]buy diflucan online[/url] [url=http://yandex.ru/redir?url=http://tinyurl.com/Buy-Imitrex-Onlines]buy imitrex online[/url]
Peardax
Newbie
 
Posts: 1
Joined: Thu Oct 09, 2008 3:29 pm
Location: Genue

Information about sears tires center for www.mortisimus.co.

Postby Drittestusall on Fri Oct 10, 2008 5:32 am

Good job! Respect, webmaster.
[url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=buy_mountain_bike_tires]buy mountain bike tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=avon_motorcycle_tires_american]avon motorcycle tires american[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=buy_tires_in_canada]buy tires in canada[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=buy_truck_tires_online]buy truck tires online[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=all_terrain_liberator_tires]all terrain liberator tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=buying_tires_on_line]buying tires on line[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=all_season_tires_reviews]all season tires reviews[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=falls_roadmaster_iv_tires]falls roadmaster iv tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=red_mountain_bike_tires]red mountain bike tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=bmw_rims_and_tires]bmw rims and tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=f_250_35in_tires]f 250 35in tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=medalist_sport_king_tires]medalist sport king tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=remington_r425_truck_tires]remington r425 truck tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=me_880_motorcycle_tires]me 880 motorcycle tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=rent_a_truck_tires]rent a truck tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=rent_to_own_tires]rent to own tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=rent_wheels_and_tires]rent wheels and tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=evans_tires_san_diego]evans tires san diego[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=mazda_6_performance_tires]mazda 6 performance tires[/url] [url=http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=retailers_for_cooper_tires]retailers for cooper tires[/url]
Drittestusall
Newbie
 
Posts: 1
Joined: Fri Oct 10, 2008 5:31 am
Location: Tunisia

Pirelli motorcycle tires important information for www.morti

Postby Pirelli-Tires-Mi on Sat Oct 11, 2008 5:47 am

Amazing job, Thanks, webmaster.
[url=' http://www.rollyo.com/pirelli-tires/ ']Pirelli motorcycle tires[/url]
<a href='http://www.rollyo.com/pirelli-tires/'>Pirelli motorcycle tires</a>
Pirelli-Tires-Mi
Newbie
 
Posts: 1
Joined: Sat Oct 11, 2008 5:46 am
Location: Guyana

Pirelli scooter tires information for users of www.mortisi

Postby Pirelli-Tires-hop on Sat Oct 11, 2008 3:13 pm

Nice design! Thanks admin.
[url=' http://www.rollyo.com/pirelli-tires/ ']Pirelli motorcycle tires[/url]
<a href='http://www.rollyo.com/pirelli-tires/'>Pirelli tires</a>
Pirelli-Tires-hop
Newbie
 
Posts: 1
Joined: Sat Oct 11, 2008 3:13 pm
Location: Guyana

New browser from google.com

Postby Agekerype on Sat Oct 11, 2008 5:47 pm

Greetings, -wing recently a new browser from [url=http://www.google.com/chrome/index.html?hl=en
Agekerype
Newbie
 
Posts: 1
Joined: Sat Oct 11, 2008 5:47 pm
Location: Croatia

Loyarie important information for www.mortisimus.co.uk users

Postby Flopype on Sun Oct 12, 2008 1:41 pm

Amazing design. Respect admin!
[url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=make-my-own-diesel-kit-chip-oil ']make my own diesel kit chip oil[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=where-can-buy-oblivion-game-of-the-year-in-canada ']where can buy oblivion game of the year in canada[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=jute-box-for-sale ']jute box for sale[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=seussical-movie ']seussical movie[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=where-to-buy-england-souvenirs-in-toronto ']where to buy england souvenirs in toronto[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=want-to-buy-insulated-plastic-mugs ']want to buy insulated plastic mugs[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=symantec-virus-definition-english-updates-download ']symantec virus definition english updates download[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=-mobile-billboards-advertising-medicine-hat-canada '] mobile billboards advertising medicine hat canada[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=picture-framing-+-anti-reflective-glass ']picture framing + anti-reflective glass[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=ecg-stress-test ']ecg stress test[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=experturl.net ']experturl.net[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=origami-tea-bag ']origami tea bag[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=looking-for-website-to-create-your-own-confetti-photo-for-birthday-party ']looking for website to create your own confetti photo for birthday party[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=where-can-i-get-graphite ']where can i get graphite[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=where-can-people-buy-skateboard ']where can people buy skateboard[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=filled-sandbags-ct ']filled sandbags ct[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=avg-firefox-download-bar-virus-check ']avg firefox download bar virus check[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=anti-vius-free-for-life-downloads ']anti vius free for life downloads[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=guinea-pigs-what-you-can-bye ']guinea pigs what you can bye[/url] [url=' http://yandex.ru/redir?url=http://bestsearch.6te.net/search.php?keyword=alabama-mallard-duck-sale ']alabama mallard duck sale[/url]
Flopype
Newbie
 
Posts: 6
Joined: Sun Oct 12, 2008 1:41 pm
Location: Guyana

Next

Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron