Page 1 of 1

Problem with multi-dimensional array inside a For loop

Posted: Sat Sep 15, 2007 8:58 pm
by dangcookie
scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've struggled with this for the past 2 hours, and I could really use a second pair of eyes.  This has to be about the simplest thing, and yet I can't get it to work.  

I create and fill a multi-dimensional array called $form_fields inside a FOR loop.  When I exit the FOR loop, I cannot print the values of the array.  I can, however, print the values while inside of the FOR loop.  I don't recall there being any variable scope issues relating to nested loops, so I am completely at a loss here.  I make no function calls in my code, so that's not an issue.  As for the rest of the code, I've created a simple form with 10 rows, with 3 fields per row, gather video ID, song title, and artist name data from the user.  When the user submits the data for the form (POST method), I reload the data into the form, and it works just fine.  But below the form, I want to create a special preview for the user, with the data displayed in particular format.  No big deal.  But I can't get a single value from the previously loaded array to print to the screen (outside of the FOR loop in which it is created).  Help!  Here is the FOR loop and I show you where I try to print both inside and outside of the FOR loop:

Code: Select all

<?php
		for($counter = 1; $counter <= 10; $counter++)
		{
			// ASSIGN FORM ROW VALUES TO VARIABLES:
			
			$vidID = "txtVidID_".$counter;
			$artistName = "txtArtist_".$counter;
			$songTitle = "txtSong_".$counter;
			$vidID=$_POST[$vidID];
			$artistName=$_POST[$artistName];
			$songTitle = $_POST[$songTitle];
						
			// POPULATE ARRAY WITH FORM VALUES:
			$form_fields = array($counter=>array(vidID=>$vidID, artistName=>$artistName, songTitle=>$songTitle));
			
// THE FOLLOWING LINE PRINTS JUST FINE -- AS IT IS INSIDE THE FOR LOOP:
			//print($form_fields[$counter][artistName]);
		}

// THE FOLLOWING LINE WILL NOT PRINT AND I HAVE NO IDEA WHY:
			print($form_fields[1][artistName]);
	
?>
Thank you to anyone who can shed some light!

Sincerely,
David


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Sep 15, 2007 9:11 pm
by volka
$form_fields = array($counter=>array(vidID=>$vidID, artistName=>$artistName, songTitle=>$songTitle));
This does not append a new element to the array $form_fields but assigns a new array with one one element $counter=>xyz.

try

Code: Select all

$form_fields = array();
for($counter = 1; $counter <= 10; $counter++)
{
  $vidID = isset($_POST["txtVidID_".$counter]) ? $_POST["txtVidID_".$counter]: '?';
  $artistName = isset($_POST["txtArtist_".$counter]) ? $_POST["txtArtist_".$counter]: '?';
  $songTitle = isset($_POST["txtSong_".$counter]) ? $_POST["txtSong_".$counter] : '?';

  $form_fields[$counter] = array('vidID'=>$vidID, 'artistName'=>$artistName, 'songTitle'=>$songTitle);
}

echo $form_fields[1]['artistName'], " ";
print($form_fields);
instead.

Posted: Sat Sep 15, 2007 9:59 pm
by s.dot
Your array keys are also in need of quotes. :P

'key' => $value (notice the single quotes)
and $array['value'] (again the single quotes)

This will throw out notices all over the place and fill up your error log pretty quickly. ;)

Posted: Mon Sep 17, 2007 5:31 am
by Hemlata
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello [b]dangcookie,[/b]

In your script, when you print the array [b]within for loop[/b], it[b] prints [/b]the data, but when you print it [b]outside the loop[/b], it [b]doesnt[/b]. The reason is [b]wrong format for array calling..[/b]

If you print the complete array [b]within loop[/b], you will be getting result as..

Code: Select all

Array
(
    [1] => Array
        (
            [vidID] => 1
            [artistName] => test
            [songTitle] => test title
        )
)
and if you are printing this complete array outside, the result will be..

Code: Select all

Array
(
    [10] => Array
        (
            [vidID] => 
            [artistName] => test
            [songTitle] => 
        )
)
means the array for just last counter value... This is because you are crating new array all the time with you code..

Code: Select all

// POPULATE ARRAY WITH FORM VALUES:
	$form_fields = array
	(
		$counter=>array
		(
			'vidID'=>$vidID
			, 'artistName'=>$artistName
			, 'songTitle'=>$songTitle
		)
	);
The solution could be to create the array in the following manner so that, now you can get the result for print($form_fields[1][artistName]); .

Code: Select all

$form_fields[$counter] = array
	(
		'vidID'=>$vidID
		, 'artistName'=>$artistName
		, 'songTitle'=>$songTitle
	);
Regards,


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]