|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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?
|
|
#3
|
|||
|
|||
|
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 " <a href="$PHP_SELF?action=delete&id=$key">Delete</a>n"; echo " <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> |
|
#4
|
|||
|
|||
|
RE: Request: News Tutorial
Does the news still show correctly?
Does the news.txt file actually have data in it? |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
RE: Request: News Tutorial
So, what makes you think $data is empty?
|
|
#7
|
|||
|
|||
|
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? |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
|||
|
|||
|
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'); |
|
#10
|
|||
|
|||
|
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!! |
|
#11
|
|||
|
|||
|
RE: Request: News Tutorial
i have same prob sapac, anyone have solution
|
|
#12
|
|||
|
|||
|
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.
|
|
#13
|
|||
|
|||
|
RE: Request: News Tutorial
|
![]() |
| Viewing: Codewalkers Forums > Other > Tutorials > Request: News Tutorial |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|