Page 1 of 1
turn txt into array
Posted: Fri Jan 21, 2005 5:46 pm
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ї1];
print '<br>';
print $readї2];
print '<br>';
print $readї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
Posted: Fri Jan 21, 2005 5:48 pm
by feyd
you didn't store the returned array from explode.
Posted: Fri Jan 21, 2005 6:42 pm
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

Posted: Fri Jan 21, 2005 6:46 pm
by shiznatix
how do i store the array from explode? i have a real hard time with arrays sorry.
Posted: Fri Jan 21, 2005 7:18 pm
by rehfeld
$var = explode()
Posted: Fri Jan 21, 2005 7:28 pm
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ї0];
but neither worked. so can u elaborate a lil more
Posted: Fri Jan 21, 2005 7:38 pm
by rehfeld
Code: Select all
$file = "poll.txt";
$open = fopen($file, "r+");
while (!feof($open)){
$read = fgets($open, 999999);
$var = explode (":", $read);
print $varї0];
}
fclose($open);
Posted: Fri Jan 21, 2005 8:37 pm
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))));
Posted: Fri Jan 21, 2005 10:07 pm
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)){
$read = fgetss($open, 99999);
$read = explode(",", $read);
$count = count($read);
for ($i=0; $i<$count; $i++){
print "<input type='radio' name='option' value='" . $i . "'>" . $readї$i] . "<br>";
}
}
fclose($open);
print "<input type='submit' value='submit' name='submit>";
print "</form>";
if (isset($_POSTї'submit'])){
$file2 = "data.txt";
$open2 = fopen($file2, "r+");
$option = $_REQUESTї'option'];
while(!feof($open2)){
$read2 = fgetss($open, "9999999");
$read2 = explode(",", $read2);
$count2 = count($read2);
for ($i=0; $i<$count2; $i++){
if ($i == $option-1){
print "$i option was selected <br>";
}else
print "$i was not selected <br>";
}
}
fclose($open2);
}
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.
Posted: Fri Jan 21, 2005 10:31 pm
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ї'option']) && isset($optionsї$_POSTї'option']]))
{
function stripNum($val) { return intval(trim($val)); }
$votes = array_map('stripNum',explode(',',file_get_contents('data.txt')));
$votesї$_POSTї'option']]++;
foreach($votes as $k => $v)
$output .= $k . ' was ' . ($k == $_POSTї'option'] ? '' : 'not ') . 'selected<br />';
$fp = fopen('data.txt','wt');
fwrite($fp, implode(',',$votes));
fclose($fp);
}
echo $output;
Posted: Fri Jan 21, 2005 11:56 pm
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ї'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ї'option']) && isset($optionsї$_POSTї'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
answer to #1
Posted: Sat Jan 22, 2005 12:06 pm
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>';
Posted: Sat Jan 22, 2005 12:31 pm
by feyd
I've already explained it to shiz via IM. Sorry, I forgot to post a note stating as such.
Posted: Sat Jan 22, 2005 9:46 pm
by shiznatix
HAHAHAHAHAHA it all makes sence now! genius!