Add News Feature

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Add News Feature

Post by FOsk8r »

Alright, I have my post.html (html form) and my post.php (php fuctions saving the name and content to my db).
Now that I have the enteries in my db, I would like to post the 5 newest entries on my index.php page in desending order.
Also I would like to have a archives of all of the enteries on a seperate page (Will show all articles, even ones on index.php).

POST.HTML

Code: Select all

<form name="addnews" method="post" action="post.php">
<p class="bodymd">Name<br>
<input type="text" name="name">
</p>
<p class="bodymd">Content<br>
<textarea name="content" rows="10" cols="50"></textarea>
</p>
<p class="bodymd">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Form">
</p>	  
</form>

POST.PHP

Code: Select all

<?php 
if (($_POST['name'] == "") || ($_POST['content'] == "")) 
{ 
   echo "Invalid Name or Content";
   echo "<br>";
   echo "<a href='post.html'>Back</a>";
exit; 
}


$conn = mysql_connect("localhost","username","password") or die ("Couldn't connect to DB."); 
$db = mysql_select_db("db") or die(MySQL_Error());  // change DB to your database name.. 
$sql = "INSERT INTO news (id, name, content)". "VALUES ('NULL', '".$_POST['name']."', '".$_POST['content']."')"; 
$result=MySQL_Query($sql) or die(MySQL_Error());



{
echo "<b>Your Content Was Added</b>";
echo "<br>";
echo "Name:  ";
echo $_POST['name'];
echo "<br>";
echo "Content:  ";
echo $_POST['content'];
}
?>
Some how on the index.php page I need to get the top 5 article id's from the db, and list them in desending order (from the highest to the lowest).
And the same for the archives page, which will just show all of the articles.
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

you can use
їcode]
$query="SELECT -columns-- FROM TABLE WHERE ..condition.. ORDER BY COLUMN DESC Limit 0,5" ї/code]
will show the top 5 records descending...

-is that you are looking for?
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

so it will show the top 5 no matter if its like 15-10 or like 35-20?
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

LIMIT index,no_of_records

will return the no of records from that index

say if I have 30 records from query
then
limit 10,2 will return
2 records starting from the index 10

you can read more about it from http://www.mysql.com documentation page
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

So is there anyway for it to only take the top 5 article id's no matter how many id's there are total?
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

you case use

select * from table where $condition order by id desc limit 0,5

will return top five id/records/news/ from db table no matter how many records you have in table
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

Thanks devork.
now how would I post that on my page, like this?

Code: Select all

<?php
$query="SELECT * FROM news WHERE $condition  ORDER BY ID DESC Limit 0,5";
?>
So what would $condition = to?
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

$condtion can be according to date or you can leave it if you just want to show the top 5 enteries inserted into db
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

so that will echo the top 5?

Dont I need to use somekind of echo?
User avatar
FOsk8r
Forum Newbie
Posts: 15
Joined: Thu Dec 04, 2003 8:23 pm

Post by FOsk8r »

This is what I tried

top5.php

Code: Select all

<?php
$conn = mysql_connect("localhost","username","password") or die ("Couldn't connect to DB."); 
$db = mysql_select_db("db") or die(MySQL_Error());  // change DB to your database name..  
$query = mysql_query("SELECT FROM news ORDER BY id DESC Limit 0,5");
echo "$query";
?>
I got nothing, its just a blank page.
Any thoughts?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

FOsk8r wrote:

Code: Select all

<?php
$conn = mysql_connect("localhost","username","password") or die ("Couldn't connect to DB."); 
$db = mysql_select_db("db") or die(MySQL_Error());  // change DB to your database name..  
$query = mysql_query("SELECT FROM news ORDER BY id DESC Limit 0,5");
echo "$query";
?>
Try this:

Code: Select all

<?php
$conn = mysql_connect("localhost","username","password") or die ("Couldn't connect to DB."); 
$db = mysql_select_db("db") or die(MySQL_Error());  // change DB to your database name..  
$query = mysql_query("SELECT FROM news ORDER BY id DESC Limit 0,5");
while($row=mysql_fetch_row($query)){
  echo $row[0];
}
?>
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

use Weirdan code it is good :)
clear some basics;
when you query the db then it will return you the result in the form of records set, so to use those records you have to traverse for displaying them that this code is doing.

Code: Select all

while($row=mysql_fetch_row($query))&#123; 
  echo $row&#1111;0]; 
&#125;
$row will have the whole one record
and $row[0] means to get the first column of that records

Code: Select all

recs 
      col1  col2    col3
--------------------------
---|------|-------|------|
---|------|-------|------|
---|------|-------|------|
---|------|-------|------|
---|------|-------|------|
I think you better read some good tutorial on using mysql with php
read the php manual ;)
Post Reply