Drawing of a table

So, what do we need in a news tape? The news text itself, date �and let�s add the news heading. We take phpMyAdmin, create data base (but we can do without it). Click its heading in the bases list. Except blank table list of the current data base there are action forms in the right window as well. Find form �draw a table�. Write table�s name �news� and the number of fields �we need four. Insert information about table fields:
Code:
The name  Data type    Blank    Optional
of the 
field
news_id   MEDIUMINT    NOT NULL AUTO_INCREMENT
(for this field you need to put tick �primary�)

ntext     TEXT         NOT NULL
ntitle    VARCHAR(255)
ndate     DATETIME     NOT NULL
Field news_id isn�t necessary for the tape output, but we need it for the news administration. AUTO_INCREMENT means automatical value assignment of a field by drawing a new table line.
The news text will be saved in ntext. The length of one item may reach 65 kilobyte. Tick �NOT NULL� means that the field couldn�t be blank and by the attempt to insert a line with blank ntext field in the table, MySQL will oppose to it.
The news heading ntitle may be optional. Sometimes heading is out of place or difficult to think out. Maximum heading length in our table will be 255 symbols.
Field ndate contains date and time of the news. Of course, it couldn�t be blank because the table sorting is done by its means.
The request for the table drawing looks so:
Code:
CREATE TABLE news (news_id MEDIUMINT NOT NULL AUTO_INCREMENT, 
                            ntext TEXT NOT NULL, ntitle VARCHAR(255), ndate DATETIME NOT NULL, 
                            PRIMARY KEY(news_id));
Program code:
Code:
$request = "CREATE TABLE ... ";
mysql_query($request);
if (mysql_error())
  echo "Error in the request "$request". MySQL writes: ". mysql_error();
else
  echo "Table created ";
News record

The news is created by means of sending a request exactly as it happens by request for table creation or lines selection. Before insertion of the data in request, it�s better to process them with function addslashes to make MySQL perceive slashes as a part of the text:
mysql_query ("INSERT INTO news (ntitle, ntext, ndate) VALUES
("". addslashes($ntitle)."", "". addslashes($ntext). ", NOW())");
In the field of DATE type we insert current time with function �now�. If you need to insert non-current time, you may do so: '". date("Y-m-d H-i-s", $date). "', ...
This format is meant for DATETIME type. If the data type is DATE, you are to use "Y-m-d", if it is TIME use "H-i-s" accordingly. Variable DATE contains here date/time which are determined by mktime function.
Output of the news tape

So, we�ve already connected to the data base server and selected the base (mysql_connect and mysql_select_db). Now we need only to put out the records. We send a request, check possible errors and put the lines out.
Code:
$request = "SELECT ntext, ntitle, ndate FROM news";
Now we are left only to sort out the data. We change a request:
Code:
$request = "SELECT ntext, ntitle, ndate FROM news ORDER BY ndate DESC";
This means the sorting is led in the field �ndate� in decreasing order of the date (2000-12-05, 2000-12-04, ...). Sorting out in increasing order needs prefix ASC (ASCending), but usually sorting out in increasing order is set up as �default� and there�s no necessity in using this prefix.
Now put out the tape:
Code:
$request = "SELECT ntext, ntitle, ndate FROM news ORDER BY ndate DESC";
$result = mysql_query($request);
if (!mysql_error()) {

// Cycle extracting a line as a massive with index number
  while ($row = mysql_fetch_row($result)) {
    print("<tr><td><h3>". $row[1]. "</h3>");
    print("<font size=-1>". $row[2]. "</font>");
    print("<p align=justify>". $row[0]. "</p>");
    };
  }

/* in the case of an error in the data base the program shows up the server�s message 
   (of course, you can omit this control, but then PHP will oppose to the user else {
   print ("Error in the data base request "$request". MySQL writes ". mysql_error());
};

/* if no operations are provided next, you�d better to cleanse the memory at once */
mysql_free_result ($result);
There�s one more useful thing: if it is a news tape for example on the main page of the site, you are to reduce the news quantity (for example, to 10). We also need to show the date not as "2000-12-05 22-26-47", but as "5.12.2000 22:46":
Code:
$request = "SELECT ntext, ntitle, date_format(ndate,"%e.%m.%Y %H:%i")

as ndate1 FROM news ORDER BY ndate DESC LIMIT 10";
Reduce the news quantity to ten and we don�t need any conditions in �while� cycle. Function date_format formats to date as you like (letters as in PHP date function and �e� is the date of the month without zero at the beginning). Why do I write "as ndate1" instead of "as ndate"? Check it yourself (tip :look directive ORDER BY in the same request).
But it was just the very beginning. The main thing that is absent by the time is the possibility for the administrator to work over the news clicking with mouse instead of setting up SQL-requests. So,
The theory of administration mechanism

I recommend to work with it first in order not to be sorry later: the news tape is �nearly� ready only the redaction of the news isn�t written and you have to write requests "INSERT INTO news..." by hand every time. You may, of course, write news through the phpMyAdmin but it�s not that you need.
In the directory with site we create directory �admin� (or more originally, for example, �chief�). When the site will be exposed on WWW, put htaccess in this directory and create a password to prevent it from unneeded attendance.
Now let�s think what else we need for normal work with news. Form for the input of new news, processor of the form. What else? List of the news with possibility to click it in order to redact at once. Then you may put ticks in front of the news headings to mark the undesirable ones and remove them at once (it�s better to have �yes/no� confirmation). And then make the data input limited: instead of text fields we can use opening lists which could prevent from misprints.