|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
question about arrays
Hello,
I was wondering whether or not it was possible to open a file with the file() function so that it turns into an array, and then take that array and somehow be able to delete a particular element from the array. Like if I wanted to delete a certain message from a list of messages. |
|
#2
|
|||||
|
|||||
|
RE: question about arrays
Sure..try something like this:
php Code:
Beware though, that with real big files, this could eat up some memory....there are more efficient ways, but this way is really the easiest... |
|
#3
|
|||
|
|||
|
RE: question about arrays
thanks for the reply. I tried that out but got a couple of errors. Probably because I didn't explain it fully. Anyways this is what I am trying to do:
Code:
if(empty($newspass))
{
echo "You did not fill in a password. Please go back and fill in a password.";
exit;
}
elseif($newspass != $deletenews)
{
echo "Password incorrect. Please try again.";
exit;
}
else
{
$headlines = file("images/news.txt");
$headlines = array_reverse($headlines);
foreach($headlines as $key => $value) {
if($key != $news)
{
$newheadlines[] = $value;
}
}
@ $fp = fopen("images/news.txt", "w");
if(!$fp)
{
echo "Could not process file. Please try again later.";
exit;
}
else
{
fwrite($fp, $newheadlines);
fclose($fp);
echo "Deletion Successful!";
}
}
Basically what I am trying to do is from a form on a previous page where all the news topics are displayed, I have selection buttons for the user to choose which news headline they want deleted. (They also have a password to type in but that part it working fine.) Anyways I have it so that when they submit the form a $news variable is passed which gives a corresponding number such as 0, 1, 2, etc which is in line with the array keys. So what I am trying to do now is to use that foreach() function you told me about, compare the $key variable (which to my understanding is 0, 1, 2, etc), to the $news variable which will have a similar value, and basically for each array key, if it does not equal the $news variable, add it to a new array called $newheadlines[]. And in the event that $key does equal $news, that it will not be added to the new array. From there, I wanted to new array to replace the old array stored on news.txt . However when I used this code all that was left on the text file was the word "array". Any idea what I am doing wrong? Thanks a lot for any replies. |
|
#4
|
|||
|
|||
|
RE: question about arrays
change
$headlines = array_reverse($headlines); to just array_reverse($headlines); |
|
#5
|
|||
|
|||
|
RE: question about arrays
hrm still getting the same thing :/
thanks for the reply tho |
|
#6
|
|||||
|
|||||
|
RE: question about arrays
u sure? it should look like this..
php Code:
if it's only writing "Array" in your text file, then the $value being stored into $newheadlines is an array itself, therefore creating a multi-demensional array (otherwise known as a "matrix") you may want to run some tests on this just to see exactly what's going on.. something like this |
|
#7
|
|||
|
|||
|
RE: question about arrays
Ok I think I got it working correctly now. Apparently it was making a multidimensional array. I decided to try the array_splice() function to get it to work instead, and this is what I did and it worked. btw thanks for all your help, woulda taken me forever to get passed that lol.
Code:
$headlines = file("images/news.txt");
$number_news = count($headlines);
array_reverse($headlines);
foreach($headlines as $key=>$value) {
$section = explode("t", $value);
$section[3] = StripSlashes($section[3]);
$section[4] = StripSlashes($section[4]);
echo "<center><table width="720" border="0" cellspacing="0" cellpadding="0">";
echo "<tr><td rowspan="2" width="35"><img src="images/news_left.jpg" width="35" height="32"></td>";
echo "<td height="26" width="685"><font face="Arial, Helvetica, sans-serif"><b><font size="4">$section[3]</font></b></font>Â*-Â*<font face="Arial, Helvetica, sans-serif" size="2" color="#CCCCCC">$section[0]</font>Â*-Â*<a href="mailto:$section[2]" class="newsemail">$section[1]</a></td></tr>";
echo "<tr><td height="6" width="685" align="left"><img src="images/news_under.jpg" width="546" height="6"></td></tr>";
echo "<tr><td colspan="2" width="720" valign="top"><font face="arial" size="2" color="#999999">$section[4]</font></td></tr>";
echo "</table></center><br>";
echo "<p>";
echo "Â*<a href="$PHP_SELF?action=deletepw&id=$key" class="navbar">Delete</a><br>";
echo "</p>";
}
if($action == "deletepw") {
echo "<p>Please enter the password:</p>";
echo "<form name="newpass" method="post" action="$PHP_SELF?action=delete">";
echo "<input type="password" name="newspass" size="30">";
echo "<input type="hidden" name="id" value="$id">";
echo "<input type="submit" name="Submit" value="Delete News Item">";
echo "</form>";
}
if($action == "delete" && isset($newspass)) {
if($newspass != $pass) {
echo "Password incorrect.";
exit;
}
else {
$data = file('images/news.txt');
//this next line will remove the single news item from the array
array_splice($data,$id,1);
//now we open the file with mode 'w' which truncates the file
$fp = fopen('images/news.txt','w');
foreach($data as $element) {
fwrite($fp, $element);
}
fclose($fp);
echo "<center><b>Deletion Successful!</b></center><br>";
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > question about arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|