[help] PHP news system problems.

Status
Not open for further replies.

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Well, i've been trying to figure out how to get my news to show up as their id (news.php?id=1) and i need it to auto-archive when there comes a new news. three news is the maximum to show up, but unfortunately i don't understand how to get this to work. Could you help me a little? ;]
 

X1M!

le troller
Jan 6, 2011
179
1
PHP:
<?php

$SELECTNEWS = MySQL_Query("SELECT * FROM `news` WHERE `id` = '".addslashes(htmlspecialchars($_GET['id']))."'");
if(MySQL_Num_Rows($SELECTNEWS) >= 1) {
$SELECTNEWS = MySQL_Fetch_Array($SELECTNEWS);
// USE $SELECTNEWS[''] TO GET NEWS DETAILS
}else{
echo "Invalid article";
}
?>
Told you so.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Well, I guess I could do it for you as it is easy.
Here's how:
PHP:
<?php
if(isset($_GET['id'])) {
$getNews = mysql_query("SELECT * FROM news WHERE id = '".$_GET['id']."'");
while($row1 = mysql_fetch_array($getNews, MYSQL_ASSOC)) {
echo $row1['title'];
etc
}
}elseif(!isset($_GET['id'])) {
$getLastNews = mysql_query("SELECT * FROM news ORDER by id LIMIT 3");
while($row2 = mysql_fetch_array($getLastNews, MYSQL_ASSOC)) {
echo $row2['title'];
etc
}
}elseif(!is_numeric($_GET['id'])) { //Against SQL Injection
echo "Invalid article";
}
?>
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
The last one gave me an error: "Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\search\news.php on line 94" which is only a "}". What may be the error?
 
Nov 23, 2010
307
2
Simply:
PHP:
<?php
mysql_connect('localhost', 'root', 'your pass goes here!'); // if you want protect that!
mysql_select_db('news_system');
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM news WHERE id='".$id."'");
$sql2 = mysql_query("SELECT * FROM news");
while($row = mysql_fetch_array($sql)) {
echo "<h1>".$row['title']."</h1>";
echo "<p>".$row['story']."</p>";
$id2 = $row['id'];
}
if($id !== $id2) {
echo '<p>Invalid article.</p>';
}
$n = mysql_num_rows($sql2);
if($n == '0') {
echo '<p>Non article exists right now!</p>';
}
?>
works .
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Doesn't actually work as it's supposed i guess.. My news system is in the database "search" and is used in the table "news", how do i get that code you gave me to work with that table?
 
Nov 23, 2010
307
2
Ehm , okay then :
PHP:
<?php
mysql_connect('localhost', 'root', 'your pass goes here!'); // change "your pass goes here!" to your mysql password!!
mysql_select_db('search'); // this is database "search".
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM news WHERE id='".$id."'");
$sql2 = mysql_query("SELECT * FROM news");
while($row = mysql_fetch_array($sql)) {
echo "<h1>".$row['title']."</h1>";
echo "<p>".$row['story']."</p>";
$id2 = $row['id'];
}
if($id !== $id2) {
echo '<p>Invalid article.</p>';
}
$n = mysql_num_rows($sql2);
if($n == '0') {
echo '<p>Non article exists right now!</p>';
}
?>
kk , table is good , database changed to "search" and also find somewhere that says : your pass goes here! and change it to your mysql password !
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
the "id" part is in the table "news", so when i add it to the news.php page it doesn't show anything.. ;[
 
Nov 23, 2010
307
2
Lool , bro! you need help for like news.php?id=1 , if you dont have id col in news table then create it then make a help thread in here!
Its impossible to make that ?id=1 if you dont have id col in news table!


OH BRO , sorry , i thought id col is not in news table , did u do like news.php?id=1 in link ?
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Thanks mate, now it works perfectly! But then i have the problem that my news on the main page doesn't stop unless i remove one of the news, i want news to be auto-archived after 3 news have been put up, the maximum news that's supposed to show is 3. All new news will be displayed, and the old ones will not. I've tried to get this myself, but unfortunately i failed.
 
Nov 23, 2010
307
2
PHP:
mysql_connect('localhost', 'root' , 'change your pass here!'); // change ur pass
mysql_select_db('search');
$sql = mysql_query("SELECT * FROM news ORDER BY id LIMIT 3");
$sql2 = mysql_query("SELECT * FROM news");
while($row = mysql_fetch_array($sql)) {
echo "<h1>".$row['title']."</h1>";
echo "<p>".$row['story']."</p>";
}
if(mysql_num_rows($sql2) == '0') {
echo '<p>Non article exists right now!</p>';
}
This is for main page , it will show only the last 3 news.
not tested , im in my cousin's house!
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Thanks mate, but i also need it to fetch the $row['url']. Like: Title has been updated-if you click it redirects to the news url-
 
Nov 23, 2010
307
2
thats impossible while link is going to be throught id , like news.php?id=1 , but if you want it like : news.php?title=Title of news here , then ill give you the source!
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
I managed to get it even though you said it is impossible,

PHP:
<?php
mysql_connect('localhost', 'root' , 'MY PASSWORD HERE'); // change ur pass
mysql_select_db('search');
$sql = mysql_query("SELECT * FROM news ORDER BY id LIMIT 3");
$sql2 = mysql_query("SELECT * FROM news");
while($row = mysql_fetch_array($sql)) {
echo "<a href='".$row['url']."'>".$row['title']."</a> - ";
}
if(mysql_num_rows($sql2) == '0') {
echo '<p>Non article exists right now!</p>';
}
?>

I changed these lines
PHP:
echo "<h1>".$row['title']."</h1>";
echo "<p>".$row['story']."</p>";
to this one line
PHP:
echo "<a href='".$row['url']."'>".$row['title']."</a> - ";
;]
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Nah, i added a simple column called "url" where i write in the url's manually. Like i write in "news.php?id=4" etc, and it fetches that in the linking process ;] used it in the last line of code i had too. lol
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Uhmm, i just found an error in the code you gave me. It does not let the newer news to get updated.. That means that the three old news still are there, and won't get changed unless i edit the news in the database.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
You should learn SQL before even thinking of doing a complete news system or a search bar/site, ANYWAYS.
Change:
PHP:
$sql*=*mysql_query("SELECT***FROM*news*ORDER*BY*id*LIMIT*3");
for
PHP:
$sql*=*mysql_query("SELECT***FROM*news*ORDER*BY*DESC LIMIT*3");
[mod] Thread closed [/mod]
 
Status
Not open for further replies.

Users who are viewing this thread

Top