You are here: Re: PHP news bulletin site « PHP Programming Language « IT news, forums, messages
Re: PHP news bulletin site

Posted by john.d.mann@sbcglobal.net on 01/12/06 12:34

andyt_2000_uk@yahoo.co.uk wrote:
> Hi guys,
>
> having a little trouble working out how i'm going to do this. At the
> moment, users can log in and click a link to add news. This opens a
> page to enter the message and submit it to the database. These are then
> shown on the homepage.
>
> Questions are how can i go about letting the users edit or delete the
> messages? I think i need to add a link to the messages with the newsID
> that the users can then click to select that message. They can then be
> given a button to delete or edit it.
>
> So how do i add a link to the messages that will contain that messages
> ID so i can reload it?
>
> Sorry if its a little confusing, tired now.
>
> Cheers for any help.
>
> Heres what i have so far, this is the index page that is displayed with
> the messages on.
>
> Code:
>
> <?php session_start(); if(isset($_SESSION['username'])){ echo "Welcome
> ".$_SESSION['username']. '<br />'; //connect to mysql @ $db =
> mysql_pconnect('localhost', 'root', 'chase'); if(!$db){ echo 'Error:
> Could not connect to the database.'; } //connect to database
> mysql_select_db('dbHelpdesk'); $query = "select * from news"; $result =
> mysql_query($query, $db); $num_results = mysql_num_rows($result);
> $_SESSION['numNews']=$num_results; if($num_results == 0 ){ echo 'There
> are no current news items.'; echo '<br><a href="addNews.html">Add
> News</a><br />'; echo '<br><a href="logout.php">Logout</a><br />';
> exit; }else echo '<br> Number of News Items ' . $num_results . '<br
> />'; //get news items for ($a=0; $a < $num_results; $a++){ $row =
> mysql_fetch_array($result); echo '<br> Message: ' .
> stripslashes($row['Message']) . '<br />'; } echo '<br><a
> href="addNews.html">Add News</a><br />'; echo '<br><a
> href="logout.php">Logout</a><br />'; } else { echo "You are not logged
> in"; } ?>
>
>
>
> Next we have the page where i'm adding the messages.
>
> Code:
>
> <?php session_start(); if(isset($_SESSION['username'])){ $news =
> $HTTP_POST_VARS['message']; //echo '<br>' . $news . '<br />';
> if(!$news){ echo 'Please make sure you fill in the message box.'; echo
> '<br><a href="addNews.html">Return to Message</a><br />'; exit; } $news
> = addslashes($news); //connect to the db to post news @ $db =
> mysql_pconnect('localhost', 'root','chase'); if(!$db){ echo 'Error:
> Could not connect to the database.'; exit; }
> mysql_select_db('dbHelpdesk'); $newsID = $_SESSION['numNews'] + 1 ;
> $currDate = date('Y-m-d'); $newsQuery = "insert into news
> values('".$newsID."','".$news."','".$_SESSION['username']."','".$currDate."'
> )"; $result = mysql_query($newsQuery); if ($result){ echo
> mysql_affected_rows() . ' news item added to database'; } echo '<br><a
> href="index.php">Return to Main Page</a><br />'; echo '<br><a
> href="logout.php">Logout</a><br />'; } else { echo "You are not logged
> in"; } ?>
>
>
>
> Cheers
>

Add another field to your news table in the database called id. You can
then either have it auto-increment or you can set it yourself -
auto-increment is the preferred way, since you do not have to worry
about what it is. When you fetch the news items, output the contents of
the id field into your output, say like:

echo $newsitem[text];
echo ('<a href="newsfunctions.php?action=edit&id=' . $newsitem[id] .
'>Edit</a>');
echo ('<a href="newsfunctions.php?action=delete&id=' . $newsitem[id] .
'>Delete</a>');

This codes the news item's id into the link for you when it puts it on
the page. On your newsfunctions.php (in the example), you would use:

$the_action = $_GET['action'];
$the_id = $_GET['id'];

And then use your sql functions to determine which news item you are
operating on:

if $the_action = 'edit'
{
$sql = ('select * from news where id = ' . $the_id);
// run the sql and drop results in an array, edit the text
// field and put back in the database

} else if $the_action = 'delete' {
$sql = ('select * from news where id = ' . $the_id);
// run the sql and drop results into an array, ask the user
// if they are sure they want to delete, and remove the row
// from the database. drop it to an array so you can show
// the entry or even a preview of it so they know it's the
// right one :)

} else {
echo ('invalid function');
// or whatnot
}

Hope that helps you out some. It's what I do, and would do in your case.

-John D. Mann

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация