04-09-2005
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 82
|
***SOLVED*** Help me with automated news script?
I'm fairly new to php and i successfully made a automated news script but i have a few questions on enhancing it.
First off, the news is displayed from oldest(on the top) and newest(on the bottom), but how would i flip that so the new news is displayed on top?
Secondly, i need someway of deleting unwanted news, can anyone help me here?
Thanks in advance.
Last edited by ViciOuS; 04-13-2005 at 09:48 PM.
|
|
|
04-10-2005
|
#2 (permalink)
|
|
code anyone?
Join Date: Feb 2004
Location: New Zealand
Posts: 590
|
Quote:
|
Originally Posted by ViciOuS
First off, the news is displayed from oldest(on the top) and newest(on the bottom), but how would i flip that so the new news is displayed on top?
|
simple. goto your SQL query, and add "ORDER BY id DESC" on the end, assuming you have an id column. if not, add one! this will return the same results but in reverse order (i.e. newest first)
Quote:
|
Originally Posted by ViciOuS
Secondly, i need someway of deleting unwanted news, can anyone help me here?
|
you need to make a secure panel (use sessions, hashing/encryption for passwords etc, check out this tutorial on how to make a membership system) and in that panel, make a page that lists all the entries, you can click the title of one and goto another page, where you use the SQL query: "DELETE FROM yournewstable WHERE id = '5'". this is an example, and would delete the database row with id = 5.
have fun 
|
|
|
04-10-2005
|
#3 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 82
|
Thanks a bunch man, I really appreciate it. I actually made an admin control panel before i started workin on the news script, lol.. but thanks for the help on that subject anyways.
I got another question. On the form that i use to post news, if i use line breaks in the form, it doesn't produce it on the news.php page. Why is this?
Last edited by ViciOuS; 04-10-2005 at 04:43 AM.
|
|
|
04-10-2005
|
#4 (permalink)
|
|
Registered User
Join Date: Mar 2005
Location: London, ON
Posts: 12
|
because html doesn't render white space. push the letter a then hit the space key 1000 times then hit t and save it as an html file. open it and watch it produce 'a t'.
solution: nl2br();
put it around your content IE nl2br( $r['news'] );
|
|
|
04-10-2005
|
#5 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 82
|
I don't know what to say, lol.. it works flawlessly and i've never been so happy in my life.
Anyways, another issue:
I've got a delete news page that displays the news in my admin cp, but how would you make it to where you click the title and it takes you to another page where you can delete it?.. Sorry if I don't make much sense but like I said, i'm somewhat of a newbie to php and I really want to get better.
|
|
|
04-10-2005
|
#6 (permalink)
|
|
Sheep Worrier
Join Date: Sep 2003
Location: Portsmouth, UK.
Posts: 4,073
|
Instead of deleting the old news, why don't you alter your new scripts so that it only shows the newest 'n' number of entries? That way you can have news archive functionality, a clean frontpage, and avoid the need to constantly delete old news (which will quickly become tedious, trust me...).
__________________
|
|
|
04-10-2005
|
#7 (permalink)
|
|
Red Dawn
Join Date: May 2004
Location: Eastern Europe
Posts: 302
|
To do what Mog said just use the 'LIMIT n' handle in your sql query with the n being the number of results you want to get.
__________________
|
|
|
04-10-2005
|
#8 (permalink)
|
|
code anyone?
Join Date: Feb 2004
Location: New Zealand
Posts: 590
|
solution: something like this:
Code:
<?php
if($_GET['article_id']){
$sql = "DELETE FROM news WHERE id = '".$_GET['article_id']."' LIMIT 1";
# ...
} else {
$sql = "SELECT id, Title FROM news ORDER BY id DESC";
while($row = mysql_fetch_assoc(mysql_query($sql)))
echo '<p>
<a href="'.$_SERVER['PHP_SELF'].'?article_id='.$row['id'].'">Edit: '.$row['Title'].'</a>
</p>';
}
?>
obviously run some checks on the var like:
Code:
if(!is_numeric($_GET['article_id'])) die("article_id is not a numeric number1");
Last edited by scrowler; 04-10-2005 at 09:41 AM.
|
|
|
04-10-2005
|
#9 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 82
|
Quote:
|
Originally Posted by Man1c M0g
Instead of deleting the old news, why don't you alter your new scripts so that it only shows the newest 'n' number of entries? That way you can have news archive functionality, a clean frontpage, and avoid the need to constantly delete old news (which will quickly become tedious, trust me...).
|
Well i'ts not that I want to delete old news, it's just that, I want other admins to be able to delete unwanted news alltogether. Like say an admin makes some news but he forgot a whole paragraph and he needs to delete it and try again.
Here is my DelNews.php It just gives me the error saying news_id isnt a number
Code:
<?php
session_start();
// Define session and password variables
$pw = $_SESSION['password'];
$ad_pw = "******";
$enpw = md5($ad_pw);
// Check to see if they are logged in..
if ($enpw != $pw){
header("Location: Loggedin.php");}
mysql_connect('localhost','ryan_clanad','******');
mysql_select_db('ryan_clanad');
if(!is_numeric($_GET['news_id'])) die("news_id is not a numeric number1");
if($_GET['news_id']){
$sql = "DELETE FROM news WHERE id = '".$_GET['news_id']."' LIMIT 1";
} else {
$sql = "SELECT id, title FROM news ORDER BY id DESC";
while($row = mysql_fetch_assoc(mysql_query($sql)))
echo '<p>
<a href="'.$_SERVER['PHP_SELF'].'?news_id='.$row['id'].'">Edit: '.$row['title'].'</a>
</p>';
}
?>
|
|
|
04-11-2005
|
#10 (permalink)
|
|
code anyone?
Join Date: Feb 2004
Location: New Zealand
Posts: 590
|
see what happens if you remove that line
|
|
|
04-11-2005
|
#11 (permalink)
|
|
Local Biorust Beast
Join Date: Oct 2003
Location: San Diego, CA, USA
Posts: 2,253
|
Something tells me that GET variables are always strings, part of the HTTP Specification I believe (could be wrong), so is_numeric would return false always with a GET variable.
|
|
|
04-11-2005
|
#12 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 82
|
Well, I kinda scrapped the old delete.php and made a new script that does both edit and delete. But once again i'm having troubles. Could it be because of the GET variable is bringing me false info?
Ok, this is what i've got for my EditNews_interface.php
Code:
<?php
include('mysql_connect.php');
$sql = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row = mysql_fetch_array($sql))
{
?>
<html><head><title></title>
<style>
<!--
a{text-decoration:none}
//-->
</style>
<style>
a:visited{color:blue; text-decoration:none; }
a:active{color:red; text-decoration:none; }
a:link{color:red; text-decoration:none; }
a:hover{color:orange; text-decoration:underline; }
</style>
</head>
<BODY background="../images/image_22.gif">
<div align="left">
<TABLE width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
<?php echo $row['title'] . ' | Posted by: ' . $row['name'] . ' on ' . $row['date'] . ' ID# ' . $row['id']; ?></font></b>
</td>
</tr>
<tr>
<td>
<?php echo "[ <a href=\"EditNews.php?id=\$id\">Edit Post</a> ] [
<a href=\"DelNews.php?id=\$id\">Delete Post</a> ]"; ?>
</td>
<br>
</tr>
</TABLE>
</div>
</body>
</html>
<?php
}
?>
My DeleteNews.php
Code:
<?php
session_start();
// Define session and password variables
$pw = $_SESSION['password'];
$ad_pw = "******";
$enpw = md5($ad_pw);
// Check to see if they are logged in..
if ($enpw != $pw){
header("Location: Loggedin.php");
}else{
include('mysql_connect.php');
$id = $_GET['id'];
if(isset($id)) {
mysql_query("DELETE FROM news WHERE id='$id'") or die("Could not delete news post"
. mysql_error());
}else{
echo 'Deletion of news failed, system error, no id specified...';
}
}
?>
EditNews.php
Code:
<?php
include('mysql_connect.php');
$ID = $_GET['id'];
$sql = mysql_query("SELECT * FROM news WHERE id='$ID'");
$row = mysql_fetch_object($sql);
?>
<div align="center">
<TABLE width="80%" valign="center">
<tr bgcolor="#EEEEEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
News: </td>
<td><TEXTAREA Name="news" Rows="15" Cols="55"><?php echo $row->news; ?></TEXTAREA></td>
</tr>
</TABLE>
<TABLE width="80%" valign="center">
<tr bgcolor="#CCCCCC">
<td><center>
<INPUT type="submit" tabindex="5" name="submit" value="Add News">
</center></td>
</tr>
</TABLE>
[ <a href="Loggedin.php" target="iframe">Back to Admin CP</a> ]
</div>
And finally, my Handle_EditNews.php
Code:
<?php
include('mysql_connect.php');
$news = $_POST['news'];
$ID = $_GET['ID'];
if(!isset($ID)) {
echo 'A system error occured and the editing was unable to finish';
}else{
$worked = mysql_query("UPDATE news SET news='$news' WHERE id='$ID'");
if(!$worked) {
die('Didn\'t work because' . mysql_error());
}else{
echo 'You have successfully updated the news.';
}
}
?>
I've still got a AddNews.php and a news.php but those are working fine. It's just, my EditNews_interface.php page is not working properly. The Edit links all have the same url, and same as the delete links.
When i click an edit link, it takes me to my editnews page but theres nothing in the textarea, and when i click on a delete link, it takes me to a blank page in my iframe.. not even an error 
|
|
|
04-11-2005
|
#13 (permalink)
|
|
Local Biorust Beast
Join Date: Oct 2003
Location: San Diego, CA, USA
Posts: 2,253
|
in your links, you use $id, where is that assigned, perhaps you mean $row['id'] instead? Right now, $id is null, nothing there, which would not error, but not return anything either.
|
|
|
04-11-2005
|
#14 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 82
|
Hmm, I tryed declaring $id as $_GET['id'], then I tryed putting in $row['id'] instead of $id, then I tryed a few other things but they all seem to do absolutely nothing
It's displaying all the edit links as the same link, so when you get a visited link, all the edit links for every row turn into visited links (aka the color blue :-\) Same goes with the delete links.
It feels as if the solution to this mess is on the tip of my tounge.. i'ts very frustrating lol.
|
|
|
04-11-2005
|
#15 (permalink)
|
|
Red Dawn
Join Date: May 2004
Location: Eastern Europe
Posts: 302
|
Quote:
|
Originally Posted by Order
Something tells me that GET variables are always strings, part of the HTTP Specification I believe (could be wrong), so is_numeric would return false always with a GET variable.
|
Yeh i got the same feeling...
Just do a $somevar = $_GET['someid'] + 0;
That should change it to int if its a number, but if its a string it will not change it, just add a zero at the end of the line. And keep the is_numeric check when using this.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +1. The time now is 07:02 PM. Content Relevant URLs by vBSEO 3.2.0
|