Forum Topic: PHP: Outputting updates

(365 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

thecoshman

Reply To Post Reply & Quote

Posted at: 6/17/07 12:50 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

PHP: main

Outputting updates stored on a database.

So this is my first tutorial, so it may not be the best. I am writing this for a friend, but I may as well upload here the help others.

What I am going to try to TEACH (rather the do for you) is how to load data from database using SQL then output it in a nice format. To save questions being asked, I am going to assume some basic knowledge of web technologies, so will explain what I am doing and trust you can work out the simple bits.

Firstly, we need to decide how our data is stored in the database. For this tutorial, we are going to be storing the updates for a site, so the table will be called ‘updates’. We will then have four fields in this table, The ID for each update, the DATE, the SUBJECT and the actual UPDATE. I wont go into how to make this table as most hosts have tools to help you do this. One thing to bare in mind though, you need to make sure the ID auto increments on each update and is set to be unique.

Now for the code, we first need the HTML stuff that starts all pages, which I will not give you. The first bit PHP code we will use is that which will connect us the our database.

<?php
$server = mysql_connect($host,$username,$password)
$databse = mysql_select_db($database)
?>

The first line connects to your database server, the second selects the actual database you want to use. If you want this code explain in more detail read this tutorial, http://www.newgrounds.com/bbs/topic.php?id=52 7971.

We now need to load that data from the database. This is done in one simple line,

<?php
$result = mysql_query("SELECT * FROM updates ORDER BY id DESC");
?>

This command will select all of the data from the updates table, order it by the ID going from highest to lowest.

We will now output the data to the browser.

while ($updates = mysql_fetch_array($result)){ /* for each row that the query returned */
?>
<div> /* make a new DIV for each update */
<p>
<?php
echo stripslashes($updates[‘subject’]); /* output the subject for this update */
?>
</p>
<p>
<?php
echo $updates[‘date’]; /* output the data of this update */
?>
</p>
<p>
<?php
echo nl2br(stripslashes($updates[‘update’])); /* output the actual contect of this update */
?>
</p>
</div> /* close the DIV for this update
<?php
}

This code will loop through for each update that is stored in the table. Each time it loops, it goes to the next highest ID (as that is what we said to order the data by). It will make a new DIV (this means that we can format the look of each update so that it appears in its own box) and output the subject, data and content of the update.

Right, that is it! You can now store your data in a nice simple database and have it shown back real simply. If you made a form that let you update the database and added in users, you will have yourself a simple forum!

* * * NOTES * * *

Obviously this code is very simple, the major flaw with it being that it will output EVERY update, so if you have 100 updates, all 100 will be shown to the user at once. This can be very simple to fix, and if people want me to, I will write a follow up tutorial explain how to do this.

while ($updates = mysql_fetch_array($result)){

Let me explain this line a bit more. ‘mysql_fetch_array()’ is an inbuilt PHP function. What it dose is turn the big long list of data the was retrieved via the SQL statement and turn it into a nice simple to manage array, with the name $updates. However, it works in a very nice different way to normal arrays. You do not use this to move from different elements in the array. As you called it as the condition for the while loop, it is automatically moved through each time the loop runs.

echo nl2br(stripslashes($updates[‘update’]));

I’m sure you know how the echo function works. But here we are echo-ing what Is returned after two functions have been run on the data. These two functions are because of how I have stored the data in the database. Firstly, the ‘stripslashes()’ function. When the data is stored into the data base, people can enter data the will harm it, to get around this, you can use a PHP function that makes it safe to give to SQL (SQL is very littoral, you tell it jump of a cliff and it will!) ‘stripslashes()’ just reverse this process. ‘nl2br()’ is another function the is need because of the way data is stored in a database. When data from a text area is stored, where ever you have pressed ‘enter’/’return’ it is stored as white space, but HTML dose not understand white space. ‘nl2br()’ is their for needed so that it adds in this white space in the form of the <br /> tag
j


None

cherries

Reply To Post Reply & Quote

Posted at: 6/17/07 01:44 PM

cherries LIGHT LEVEL 18

Sign-Up: 06/07/05

Posts: 4,576

So it's like a basic news system?


None

thecoshman

Reply To Post Reply & Quote

Posted at: 6/18/07 07:07 AM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

At 6/17/07 01:44 PM, cherries wrote: So it's like a basic news system?

yer realy. I dint want to say news system, as this cna be applied to anything.

for instance, outputitng a forum thread.

It to me some time to work out how to do this neatly, and my freind was stunped with it. So i had to write him a tut, and figured others may want be helped by it.


None

authorblues

Reply To Post Reply & Quote

Posted at: 6/18/07 08:56 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,362

At 6/17/07 12:50 PM, thecoshman wrote: Obviously this code is very simple, the major flaw with it being that it will output EVERY update, so if you have 100 updates, all 100 will be shown to the user at once. This can be very simple to fix, and if people want me to, I will write a follow up tutorial explain how to do this.

do people really need another tutorial to teach them how to use the LIMIT clause? because i know it took me all of about a minute to learn how to append "LIMIT x" to my SQL query.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

different

Reply To Post Reply & Quote

Posted at: 6/18/07 09:19 AM

different DARK LEVEL 35

Sign-Up: 07/08/04

Posts: 3,764

At 6/18/07 08:56 AM, authorblues wrote: do people really need another tutorial to teach them how to use the LIMIT clause?

Apparently. But then I guess he'd have to cover some form of pagination as well.

we play iPhone, daily game reviews, twitter.


None

authorblues

Reply To Post Reply & Quote

Posted at: 6/18/07 09:32 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,362

At 6/18/07 09:19 AM, different wrote: Apparently. But then I guess he'd have to cover some form of pagination as well.

not in all cases, but pagination, imo, is a matter of style, and really doesnt need teaching.
it would turn out to be nothing more than an over-glorified LIMIT and COUNT() lesson.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

thecoshman

Reply To Post Reply & Quote

Posted at: 6/18/07 10:15 AM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

I know using limit is a baisc thing, that is why i didnt put it in their, but some people may have problems applying it to their news system or what ever.


None

alertG

Reply To Post Reply & Quote

Posted at: 6/18/07 10:30 PM

alertG DARK LEVEL 14

Sign-Up: 06/07/07

Posts: 91

Few things you forgot to mention:

1) The filename must be ".php" for those who don't know.
2) just put LIMIT # after DESC to limit the output.
3) the $username, $password, etc. must be set. otherwise it is useless :P

Just for the people new to php.


None

authorblues

Reply To Post Reply & Quote

Posted at: 6/18/07 10:37 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,362

my jaw is on the floor. ive never seen such... im dumbfounded...

At 6/18/07 10:30 PM, alertG wrote: 1) The filename must be ".php" for those who don't know.

that is covered in the intro to PHP thread. a person following the tutorials would already know this. it does not need to be re-addressed in every PHP: Main thread (but, im sure you have no idea what PHP: Main is)

2) just put LIMIT # after DESC to limit the output.

previously addressed in this thread. as the author of this tutorial, he decided not to address the sometimes complicated issue of pagination. not only does it not need to be re-addressed, it may not even bear mentioning within this thread.

3) the $username, $password, etc. must be set. otherwise it is useless :P

that is a given. now i know youre saying "not everyone will know this", but these are obvious things. even as a beginning programmer, i was aware that the computer could not read my mind. i follow technology periodicals fairly religiously, and i am/was aware that no such technology existed.

Just for the people new to php.

they wouldnt be here if they were. sorry, but your input isnt needed, and kinda makes you look like the anti-saviour. sorry.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Kurt-1

Reply To Post Reply & Quote

Posted at: 12/29/07 09:28 AM

Kurt-1 LIGHT LEVEL 21

Sign-Up: 06/04/03

Posts: 7,956

I suppose this is relevant to this script, too.

Use this to create the required tables.

To input date, use CURDATE or CURTIME

CREATE TABLE `updates` (
`subject` VARCHAR( 50 ) NOT NULL ,
`date` VARCHAR( 50 ) NOT NULL ,
`update` VARCHAR( 5000 ) NOT NULL
) ENGINE = innodb;

None

Kurt-1

Reply To Post Reply & Quote

Posted at: 12/29/07 09:35 AM

Kurt-1 LIGHT LEVEL 21

Sign-Up: 06/04/03

Posts: 7,956

Sorry for double posting

Ignore that last post.
Use THIS to create a table.

CREATE TABLE `test` (
`subject` TINYTEXT NOT NULL ,
`date` DATETIME NOT NULL ,
`update` TEXT NOT NULL
) ENGINE = innodb;

Forgot to define types.


All times are Eastern Standard Time (GMT -5) | Current Time: 03:55 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!