|
Posted by The Eclectic Electric on 08/04/06 07:47
If anyone's interested, I woke up with a solution - the 'additional' item
was being passed as an array, which I was then attempting to explode. I
changed the name in the form to remove the "[]" and it started working.
Well, after I was reminded that "==" means equal and "=" is an assignment
operator!!!
+e
"The Eclectic Electric" <nospam@spamispoo.spam> wrote in message
news:kQrAg.11$iZ1.6@newsfe1-win.ntli.net...
> I'm a noob to PHP and by way of teaching myself I thought I'd make an
> application that I could actually use. It's very simple - a page reads a
> shopping list from a file (if it exists) and accepts new items as well.
> The information is passed to a new page and parsed into a usable shopping
> list that can be printed off. Two items are passed - an array built from
> a SELECT MULTIPLE (this all works fine) and what I thought would be an
> array, but turns out to be a string (I now understand why), built from a
> TEXTAREA.
>
> I've been very diligently working my way through every noobish problem
> that I've come across, searching forums and re-reading code a hundred
> times before having a "D'OH" moment (e.g. forgetting to put a $ on the
> front of a variable I referenced). I've now hit a brick wall.
>
> My initial code "works" in terms of file i/o (as what I thought was the
> first element of the array contains carriage returns and new line
> characters), but on the screen the "additional" items are displayed as one
> item. To get around this issue, I attempted to explode the "additional"
> form element and then my original code (which expected this to be an
> array) should have worked fine. BUT IT DOESN'T. In fact, when I explode
> the contents of $_GET['additional', either directly or by passing to a
> string first, it comes out as "Array". Most frustrating of all is that I
> can still see it in the URL bar! I don't understand what's going on!
> Aaaaargh!
>
> I'll put the form and the whole of the page at the bottom for reference,
> but the pertinent parts of the code are here. Any help would be GREATLY
> appreciated!
>
> +e
>
>
> ****
> This puts the contents of 'additional' on screen and writes them to file
> ****
> IF ( array_key_exists( 'additional', $_GET ) )
> {
> $additional_array = $_GET['additional'];
> @ $fnum = fopen("$docroot/shoplist.txt",'a+');
> IF ( !$fnum )
> {
> ECHO 'Ooh, eck! It\'s all gone tits up </BR>';
> }
> ELSE
> {
> $loop_limiter = ( count($additional_array) );
> FOR ( $looper = 0; $looper < $loop_limiter; $looper++ )
> {
> ECHO '[ ] '.$additional_array[$looper].'</BR>';
> $writeline = $additional_array[$looper]."\n";
> IF ( $looper = 0 )
> {
> $writeline = "\r\n".$writeline;
> }
> FWRITE ($fnum, $writeline, strlen($writeline));
> }
> FCLOSE($fnum);
> }
> }
>
> ****
> Trying to fix this, the code success goes backwards - this puts "Array" on
> screen and writes the same to file
> ****
> IF ( array_key_exists( 'additional', $_GET ) )
> {
> $get_string = $_GET['additional'];
> @ $fnum = fopen("$docroot/shoplist.txt",'a+');
> IF ( !$fnum )
> {
> ECHO 'Ooh, eck! It\'s all gone tits up </BR>';
> }
> ELSE
> {
> $additional_array = explode( "\r\n", $get_string );
> // This does the same -
> // $additional_array = explode( "\r\n", $_GET['additional'] );
> $loop_limiter = ( count($additional_array) );
> FOR ( $looper = 0; $looper < $loop_limiter; $looper++ )
> {
> ECHO '[ ] '.$additional_array[$looper].'</BR>';
> $writeline = $additional_array[$looper]."\n";
> IF ( $looper = 0 )
> {
> $writeline = "\r\n".$writeline;
> }
> FWRITE ($fnum, $writeline, strlen($writeline));
> }
> FCLOSE($fnum);
> }
> }
>
>
>
>
>
>
>
> ****
> The original form page - shoplist.php:
> ****
> <HTML>
> <HEAD>
> <title>Mr Cakey's Shopping List Generator</title>
> </HEAD>
> <BODY>
> <FORM ACTION="shoplist_create.php" METHOD="GET">
> <?php
> $docroot = $_SERVER['DOCUMENT_ROOT'];
> @ $fnum = fopen("$docroot/shoplist.txt",'r');
> IF ( ! $fnum )
> {
> ECHO 'No previous items detected.</BR>';
> }
> ELSE
> {
> ECHO '<P>Previously selected items:</P>';
> ECHO '<SELECT NAME="existing[]" SIZE=20 MULTIPLE="yes">';
> WHILE ( ! feof($fnum) )
> {
> $line = fgets($fnum, 41);
> ECHO '<OPTION>'.$line.'</OPTION>';
> }
> FCLOSE($fnum);
> ECHO '</SELECT>';
> }
> ?>
> <P>Enter additional items here (one per line):</P>
> <TEXTAREA NAME="additional[]" COLS=40 ROWS=10></TEXTAREA>
> <INPUT TYPE="submit" VALUE="Create shopping list">
> </FORM>
> </BODY>
> </HTML>
>
>
> ****
> The list parser - shoplist_create.php:
> ****
> <HTML>
> <HEAD>
> <title>Your Shopping List</title>
> </HEAD>
> <BODY>
> <H1>Your shopping list</H1>
> <?php
> $docroot = $_SERVER['DOCUMENT_ROOT'];
> IF ( array_key_exists( 'existing', $_GET ) )
> {
> $existing_array = $_GET['existing'];
> $loop_limiter = ( count($existing_array) );
> FOR ( $looper = 0; $looper < $loop_limiter; $looper++ )
> {
> ECHO '[ ] '.$existing_array[$looper].'</BR>';
> }
> }
>
> IF ( array_key_exists( 'additional', $_GET ) )
> {
> // This gets the contents of 'additional' fine
> $additional_array = $_GET['additional'];
> // This is used to feed the explode further down
> // $get_string = $_GET['additional'];
> @ $fnum = fopen("$docroot/shoplist.txt",'a+');
> IF ( !$fnum )
> {
> ECHO 'Ooh, eck! It\'s all gone tits up </BR>';
> }
> ELSE
> {
> // This doesn't work ($get_string= line uncommented)
> // $additional_array = explode( "\r\n", $get_string );
> // Nor does this (")
> // $additional_array = explode( "\r\n", $_GET['additional'] );
> // But commenting both results in the correct information being written to
> the file and to the contents of $additional_array being echoed to the
> screen
> $loop_limiter = ( count($additional_array) );
> FOR ( $looper = 0; $looper < $loop_limiter; $looper++ )
> {
> ECHO '[ ] '.$additional_array[$looper].'</BR>';
> $writeline = $additional_array[$looper]."\n";
> IF ( $looper = 0 )
> {
> $writeline = "\r\n".$writeline;
> }
> FWRITE ($fnum, $writeline, strlen($writeline));
> }
> FCLOSE($fnum);
> }
> }
>
> IF ( !(isset($existing_array)) && !(isset($additional_array))
> )
> {
> ECHO 'Your shopping list was empty. Do you grow your own?';
> }
> ?>
> </BODY>
> </HTML>
>
Navigation:
[Reply to this message]
|