turn txt into array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

turn txt into array

Post by shiznatix »

Code: Select all

$file = "poll.txt";
$open = fopen($file, "r+");
while (!feof($open)){
$read = fgets($open, 999999);
}
explode (":", $read);
print $readї0];
print '<br>';
print $read&#1111;1];
print '<br>';
print $read&#1111;2];
print '<br>';
print $read&#1111;3];
print '<br>';
fclose($open);
is my code, the txt file is this

1:2:3:4

so it should display
1
2
3
4
but it is displaying nothing at all. what am i doing wrong
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you didn't store the returned array from explode.
Cronikeys
Forum Commoner
Posts: 35
Joined: Sun Jan 16, 2005 9:14 am

Post by Cronikeys »

Also, perhaps a while loop for the array to display? $read[$i] sort of thing :)

That way it will automatically match the ammount of contents to how much it displays ;)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

how do i store the array from explode? i have a real hard time with arrays sorry.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

$var = explode()
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

i tried a few quick things like

Code: Select all

$var = explode(":", $read);
print $var;
and

Code: Select all

$var = explode(":", $read);
print $var&#1111;0];
but neither worked. so can u elaborate a lil more
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

$file = "poll.txt";
$open = fopen($file, "r+");
while (!feof($open))&#123;
    $read = fgets($open, 999999);

    $var = explode (":", $read);
    print $var&#1111;0];



&#125;
    fclose($open);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's probable that the file reads are ~screwy.. in that once you leave the file, $read isn't 1:2:3:4, but rather a blank line.

rehfeld has one way.. here's another, real compact way:

Code: Select all

print_r(explode(':',trim(file_get_contents($file))));
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

poll.txt containes
aaa, bbb, ccc, ddd

data.txt containes
0,0,0,0

my code is

Code: Select all

$file = "poll.txt";

print "<form action='index.php' method='post'>";

$open = fopen($file, "r+");
	while (!feof($open))&#123;
	$read = fgetss($open, 99999);
	$read = explode(",", $read);
	$count = count($read);
		for ($i=0; $i<$count; $i++)&#123;
		print "<input type='radio' name='option' value='" . $i . "'>" . $read&#1111;$i] . "<br>";
		&#125;
	&#125;
fclose($open);
print "<input type='submit' value='submit' name='submit>";
print "</form>";

if (isset($_POST&#1111;'submit']))&#123;
$file2 = "data.txt";
$open2 = fopen($file2, "r+");
$option = $_REQUEST&#1111;'option'];

	while(!feof($open2))&#123;
	
	$read2 = fgetss($open, "9999999");
	$read2 = explode(",", $read2);
	$count2 = count($read2);

		for ($i=0; $i<$count2; $i++)&#123;
			if ($i == $option-1)&#123;
			print "$i option was selected <br>";
			&#125;else
			print "$i was not selected <br>";
		&#125;
	&#125;
	fclose($open2);
&#125;
i want that when you click submit it will say like 1 was selected and then the rest will say the number was not selected. but when i hit submit it just throws it into a continous loop and i dunno whats going on. im drunk and confused please help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

untested here:

Code: Select all

$output = '<form action="index.php" method="post">';

$options = array_map('trim',explode(',',file_get_contents('poll.txt')));

foreach($options as $k => $option)
  $output .= '<input type="radio" name="option" value="' . $k . '" />' . $option . '<br />';

$output .= '<input type="submit" value="vote" name="submit" /></form>';

if(isset($_POST&#1111;'option']) && isset($options&#1111;$_POST&#1111;'option']]))
&#123;
  function stripNum($val) &#123; return intval(trim($val)); &#125;
  $votes = array_map('stripNum',explode(',',file_get_contents('data.txt')));
  $votes&#1111;$_POST&#1111;'option']]++;
  foreach($votes as $k => $v)
    $output .= $k . ' was ' . ($k == $_POST&#1111;'option'] ? '' : 'not ') . 'selected<br />';
  $fp = fopen('data.txt','wt');
  fwrite($fp, implode(',',$votes));
  fclose($fp);
&#125;

echo $output;
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

sorry thats outa my league, ill try to understand what went on there by asking a few questions so i can code it my own way (so i learn cause thats what the name of my game is)



1) what does the .= thing do? whats up with that?

2) when can you use the actual question mark like it seams you did here

Code: Select all

$output .= $k . ' was ' . ($k == $_POST&#1111;'option'] ? '' : 'not ') . 'selected<br />';
can u comment almost every word of that? i dont quite understand.

3) also isnt just the 1st part of this enough? whats with the second part

Code: Select all

if(isset($_POST&#1111;'option']) && isset($options&#1111;$_POST&#1111;'option']]))
4) and what about file_get_contents? whats the diffrence between that and like fopen, and fgets, and fgetss and such.

5) also if you could explain to me the foreach loop i would be most appreciative, i understand the use of the while loop and the for loop but the foreach loop dosnt make any sence to me. please explain



sorry if im a bother but im just trying to learn. also if there is anything else important there then if u would comment on it. im kinda a newbie but i promise im not just copying and pasting, im really really trying hard to learn. thanks
Serengeti
Forum Newbie
Posts: 19
Joined: Sat Jan 22, 2005 1:58 am

answer to #1

Post by Serengeti »

answer to #1
from http://www.zend.com/zend/tut/using-strings.php
Strings can be joined using the concatenate operator (.).

$first_name = 'Charlie';
$last_name = 'Brown';
$full_name = $first_name . ' ' . $last_name;

A common need in PHP is to create a large HTML string. The assignment (=) and concatenate (.) operators can be combined (.=) to make this easier.

$html = '<table>';
$html .= '<tr><td>number</td><td>square</td></tr>';
for ( $i=0 ; $i<10 ; $i++) {
$square = $i * $i;
$html .= '<tr><td>' . $i . '</td><td>' . $square . '</td></tr>';
}
$html .= '</table>';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've already explained it to shiz via IM. Sorry, I forgot to post a note stating as such.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

HAHAHAHAHAHA it all makes sence now! genius!
Post Reply