PHP news bulletin site

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andyt_2000_uk@yahoo.co.uk

    PHP news bulletin site

    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($_SESS ION['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($qu ery, $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.h tml">Add
    News</a><br />'; echo '<br><a href="logout.ph p">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_arr ay($result); echo '<br> Message: ' .
    stripslashes($r ow['Message']) . '<br />'; } echo '<br><a
    href="addNews.h tml">Add News</a><br />'; echo '<br><a
    href="logout.ph p">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($_SESS ION['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.h tml">Return to Message</a><br />'; exit; } $news
    = addslashes($new s); //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('".$news ID."','".$news. "','".$_SES SION['username']."','".$currDat e."'
    )"; $result = mysql_query($ne wsQuery); 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.ph p">Logout</a><br />'; } else { echo "You are not logged
    in"; } ?>



    Cheers

  • john.d.mann@sbcglobal.net

    #2
    Re: PHP news bulletin site

    andyt_2000_uk@y ahoo.co.uk wrote:[color=blue]
    > 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($_SESS ION['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($qu ery, $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.h tml">Add
    > News</a><br />'; echo '<br><a href="logout.ph p">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_arr ay($result); echo '<br> Message: ' .
    > stripslashes($r ow['Message']) . '<br />'; } echo '<br><a
    > href="addNews.h tml">Add News</a><br />'; echo '<br><a
    > href="logout.ph p">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($_SESS ION['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.h tml">Return to Message</a><br />'; exit; } $news
    > = addslashes($new s); //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('".$news ID."','".$news. "','".$_SES SION['username']."','".$currDat e."'
    > )"; $result = mysql_query($ne wsQuery); 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.ph p">Logout</a><br />'; } else { echo "You are not logged
    > in"; } ?>
    >
    >
    >
    > Cheers
    >[/color]

    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="newsfunct ions.php?action =edit&id=' . $newsitem[id] .
    '>Edit</a>');
    echo ('<a href="newsfunct ions.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.p hp (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

    Comment

    Working...