Page 1 of 1

Add News Feature

Posted: Sun Dec 28, 2003 11:20 am
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.

Posted: Sun Dec 28, 2003 11:27 am
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?

Posted: Sun Dec 28, 2003 11:31 am
by FOsk8r
so it will show the top 5 no matter if its like 15-10 or like 35-20?

Posted: Sun Dec 28, 2003 11:36 am
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

Posted: Sun Dec 28, 2003 11:40 am
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?

Posted: Sun Dec 28, 2003 11:43 am
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

Posted: Sun Dec 28, 2003 12:02 pm
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?

Posted: Sun Dec 28, 2003 12:30 pm
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

Posted: Sun Dec 28, 2003 1:12 pm
by FOsk8r
so that will echo the top 5?

Dont I need to use somekind of echo?

Posted: Sun Dec 28, 2003 2:00 pm
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?

Posted: Sun Dec 28, 2003 4:00 pm
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];
}
?>

Posted: Mon Dec 29, 2003 5:18 am
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 ;)