PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

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:
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  
Old June 23rd, 2002, 11:47 PM
Silencermvm Silencermvm is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Needham, MA, United States
Posts: 4 Silencermvm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old June 24th, 2002, 02:27 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: question about arrays

Sure..try something like this:

php Code:
Original - php Code
  1. <?
  2. $messages = file('somefile.txt');
  3. foreach($messages as $value) {
  4.   //in this if statement you could also do a strstr or ereg or whatever comparision you want
  5.   if($value != 'whatever you want') {
  6.     $newmessages[] = $value;
  7.   }
  8. }
  9. //now you can write $newmessages back to the file...
  10. ?>


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...


Reply With Quote
  #3  
Old June 24th, 2002, 04:12 AM
Silencermvm Silencermvm is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Needham, MA, United States
Posts: 4 Silencermvm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old June 24th, 2002, 05:01 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: question about arrays

change

$headlines = array_reverse($headlines);

to just

array_reverse($headlines);

Reply With Quote
  #5  
Old June 24th, 2002, 05:21 AM
Silencermvm Silencermvm is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Needham, MA, United States
Posts: 4 Silencermvm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: question about arrays

hrm still getting the same thing :/

thanks for the reply tho

Reply With Quote
  #6  
Old June 24th, 2002, 08:49 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: question about arrays

u sure? it should look like this..
php Code:
Original - php Code
  1.  
  2. $headlines = file("images/news.txt");
  3. array_reverse($headlines);
  4. foreach($headlines as $key=>$value)
  5. {
  6.     if($key != $news)
  7.     {
  8.         $newheadlines[] = $value;
  9.     }
  10. }

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
php Code:
Original - php Code
  1.  
  2. $headlines = file("images/news.txt");
  3. array_reverse($headlines);
  4. foreach($headlines as $key=>$value)
  5. {
  6.     if($key != $news)
  7.     {
  8.         $newheadlines[] = $value;
  9.     }
  10.     print("$key: $value - ");
  11.     print("{$newheadlines[$value][0]}<br>n");
  12. }

Reply With Quote
  #7  
Old June 24th, 2002, 03:13 PM
Silencermvm Silencermvm is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Needham, MA, United States
Posts: 4 Silencermvm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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>";

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > question about arrays


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway