Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old December 18th, 2003, 10:01 PM
sapac sapac is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adelaide,SA,Australia
Posts: 9 sapac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Request: News Tutorial

Hey,

In regards to the news tutorial - in the later stages, when i go to read in the file 'news.txt' into the variable $data as to print it out and show the user what they are about to delete....i am returned an empty array.

I am therefore also unable to actually delete that news item from the storage file.

I looked in the forum and read that if you dont close the file after writing/apending it, then this can happen....but i have checked my code and i have explicitly closed the file each time after opening it.

I would appreciate any and all feedback that can be provided.

Cheers,

Luke

Reply With Quote
  #2  
Old January 5th, 2004, 01:40 AM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: Request: News Tutorial

It is really hard to say without seeing your actual code...can we see the code that reads in the file and results in an empty array?

Reply With Quote
  #3  
Old January 5th, 2004, 10:55 PM
sapac sapac is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adelaide,SA,Australia
Posts: 9 sapac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Request: News Tutorial

<HTML>
<HEAD>
<Title>News Example - CodeWalkers</title>
</HEAD>
<BODY>
<?PHP
/*
I CANNOT seem to get the form to hold onto the 'news'
submitted in the text box when the password is incorrect.
It does, however, hold onto the persons name....???
*/



echo "$action";


// Validate the information submitted via the form.
if($_POST[submit]) {

// Check that a name has been entered
if(!$_POST[name]) {
echo "<center><font color=red> You must enter a name</font></center>";
exit;
}

// Display a welcome message
else{
$name = $_POST[name];
echo "<center>Hey $name!</center>";
}

// Check that some news has been entered
if(!$_POST[news]) {
echo "<center><font color=red>You must enter some news</font></center>";
exit;
}

// Impose some conditions on what is allowed
// in the news
elseif(strstr($_POST[news],"|")) {
$news = $_POST[news];
echo "<center>$name, the news cannot contain the pipe symbol - |</center>";
echo "<center>You wrote: $news </center>";
exit;
}

// Check that a password has been entered
if($_POST[password] == 'getme'){
echo "<center>Your form has been submitted</center>";

// Now that the password has been entered and the form
// submitted, then as long as we still have their name
// and news....write to the file.
$fp = fopen('news.txt','a');
if(!$fp) {
echo "Error opening file!";
exit;
}
// Create a line of news to be added to the file storage.
$line = date("d.m.y") . "|" . date("h:i:s") . "|" . $_POST[name];
$line .= "|" . $_POST[news];
$line = str_replace("rn","<BR>",$line);
$line .= "rn";
// Write the line to the file
fwrite($fp, $line);
if(!fclose($fp)) {
echo "Error closing file!";
exit;
}
}
else {
echo "<center>$name, you have entered a Bad Password!<BR>
Please try again.</center>";
exit;
}

// Now we are going to display the information

// First read the data from the file into the array.
// We use the 'file' function which puts each line
// of the file as a new element in the array.

$data = file('news.txt');
$data = array_reverse($data); // Reverse the array to get the order in which the news was entered

foreach($data as $key => $element){
$element = trim($element); // Strip whitespace from begining and end of each element
$pieces = explode("|", $element); // Create strings from each element that seperated by the '|' character
// Display the news
echo "On " . $pieces[0] . " at " . $pieces[1] . ", " . $pieces[2] . " told us " " . $pieces[3]. ""<BR>";

// Add options to Edit or Delete a particular news item
echo "&nbsp;<a href="$PHP_SELF?action=delete&id=$key">Delete</a>n";
echo "&nbsp;<a href="$PHP_SELF?action=edit&id=$key">Edit</a>n";
echo "<BR><BR>n";

}
}
// Explicitly set the $action variable as register_globals are turned off
$action=$_GET['action'];

/*=============================
The code to delete a news item.
=============================
*/
if($action == 'delete' && isset($_POST[deletepass] )){

// If the action is delete AND the password is correct, then delete the news item
if($_POST[deletepass]=="delete"){

// Refresh the $data variable for consistency
$data = file('news.txt');
echo "At the first stage, data is:".array_values($data);

// Remove 1 element from $data, starting at index $id
array_splice($data,$id,1);

// Rewrite the storage file with the new $data array
$fp = fopen('news.txt', 'w');

foreach($data as $element){
fwrite($fp, $element);
}

fclose($fp);
echo "Item has been deleted!<BR>n";
echo "<a href="$PHP_SELF">Go Back</a>n";
exit;
}
else
{
echo "Bad Password";
}
}

// Check if a delete action is required
if($action=='delete'){
echo "<H2>You are about to delete the following news item:</H2>";
$data = file('news.txt');
$element = $data[$id];
echo "The element is: $element";
$element = trim($data[$id]); // Strip the desired news item of white space
echo "element: $element";
$pieces = explode("|",$element); // Create strings from the news item seperated by the '|' character
$values = array_values($data);
echo "Pieces23: $values";

// Display the news item to be deleted.
echo "On ".$pieces[0]." at ".$pieces[1].", ".$pieces[2]." told us " ".$pieces[3].""<BR>";
echo "<H3>Are you sure you want to delete this news item? If so, press the 'Delete' button.</H3>";

// Display the Delete button
echo "<FORM ACTION="$PHP_SELF?action=delete" METHOD="POST" NAME="deleteform">n";
echo "Password:<BR>n";
echo "<INPUT TYPE="password" SIZE="30" NAME="deletepass"><BR>n";
echo "<INPUT TYPE="hidden" NAME="id" VALUE="$id">n";
echo "<INPUT TYPE="submit" NAME="deletebutton" VALUE="Delete">n";
echo "</FORM>n";
exit;
}

// Show the form again upon resubmission if either
// the persons name or their news is missing
// or if their password is incorrect.
if(!$_POST[name]|| !$_POST[news] || !$_POST[password]=='getme')
{
?>
<center>
<FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry">
Your name:<BR>
<INPUT TYPE="text" SIZE="30" NAME="name" VALUE="<?=$_POST[name]?>"><BR>
The News:<BR>
<TEXTAREA NAME="news" COLS="40" ROWS="5" VALUE="<?=$_POST[news]?>"></TEXTAREA><BR><BR>
News Password:<BR>
<INPUT TYPE="password" SIZE="30" NAME="password"><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="Post it!"><BR>
</FORM>
</center>
<?PHP
}




?>
</BODY>
</HTML>

Reply With Quote
  #4  
Old January 5th, 2004, 11:56 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: Request: News Tutorial

Does the news still show correctly?

Does the news.txt file actually have data in it?

Reply With Quote
  #5  
Old January 6th, 2004, 12:27 AM
sapac sapac is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adelaide,SA,Australia
Posts: 9 sapac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Request: News Tutorial

Yes the news still shows correctly once you have submitted it with the right password...

And yes the news file does contain data.

Reply With Quote
  #6  
Old January 6th, 2004, 12:54 AM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: Request: News Tutorial

So, what makes you think $data is empty?

Reply With Quote
  #7  
Old January 6th, 2004, 02:12 AM
sapac sapac is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adelaide,SA,Australia
Posts: 9 sapac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Request: News Tutorial

Because, when i press Delete next to an news item, and it then goes to the 'confirm delete' page (as such)....it is supposed to display the news item that the user wishes to delete so that they can confirm their desicion...

However it does not display this news item!!

The place where it should display is empty!!

Did i explain this clearly enough?? Im guessing you have tried running the code?

Reply With Quote
  #8  
Old January 19th, 2004, 04:28 AM
sapac sapac is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adelaide,SA,Australia
Posts: 9 sapac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Still cant get it....

Hey hey,

Was wondering if someone might be able to take the time to look at my code again and see if they can find out where im going wrong...

It still doesn't display the news item when the user clicks on the delete button.


Reply With Quote
  #9  
Old January 19th, 2004, 05:17 AM
nawlej nawlej is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Dallas, Tx. USA
Posts: 2,008 nawlej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 7 m 51 sec
Reputation Power: 4
RE: Request: News Tutorial

You might want to try to include the path in your file() function, just a thought.

$data = file('/path/to/dir/of/news.txt');

Reply With Quote
  #10  
Old January 19th, 2004, 11:34 PM
sapac sapac is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adelaide,SA,Australia
Posts: 9 sapac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Path specification made no diff...

I put in the path of the 'news.txt' file in each call i made to 'file()' but it made no diff - i mean it was reading the file fine in the sections where the news is displayed after a user has entered a new item.

I was just thinkin that the fact that i explicitly set the '$action' variable (due to register globals being turned off) might be making a diff??

AND i cant seem to figure out why i have put the statement:

echo "$action";

at the top of my script....

Any thoughts??

ALSO....is there any difference in using _POST[variable] and HTTP_POST_VARS[variable]???

AND(2): Assume everything works fine....is there any way i can re-direct my users to the page that displays the news once they've successfully deleted an item?? At the moment, upon the 'Go Back' link i just re-direct to 'PHP_SELF' which merely takes me back to the 'Delete Confirmation' page.

Id really appreciate any and all help that can be provided!!

Reply With Quote
  #11  
Old July 7th, 2004, 11:57 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Request: News Tutorial

i have same prob sapac, anyone have solution

Reply With Quote
  #12  
Old July 8th, 2004, 02:50 PM
MrTrendyRockr MrTrendyRockr is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Duluth, Georgia, USA
Posts: 114 MrTrendyRockr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to MrTrendyRockr
RE: Request: News Tutorial

Learn to use MySQL.. it will make your life a lot easier when dealing with News Systems. With using a DB, you won't have to continously open and close and reopen and reclose a file. All of this opening and closing, as you have seen, can cause a lot of headaches, and bugs in your code.

Reply With Quote
  #13  
Old July 8th, 2004, 03:11 PM
MrTrendyRockr MrTrendyRockr is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Duluth, Georgia, USA
Posts: 114 MrTrendyRockr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to MrTrendyRockr
RE: Request: News Tutorial

To take people back to the original news page, I would probably user the header tag at the end of the delete_entry script:

php Code:
Original - php Code
  1.  
  2. #delete_entry.php
  3.  
  4. #do all of your deleting here
  5.  
  6. header("Location:http://whatever.com/news.php");



Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > Request: News Tutorial


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |