Multidimensional arrays issue

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
leoden
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 8:48 pm

Multidimensional arrays issue

Post by leoden »

Hi there can anyone help with this prog.

Its intended function is to build a multidimensional array of file details ( based on selected db record) which is then passed to a mailing function which attaches the aforementioned files to the mail. Everything works fine EXCEPT I only get one entry in my multidimensioned array. The code (please ignore hardcoded values I havent designed an interface yet!)

Code: Select all

while ($row = $salehighlights->fetchrow ())
{ 
$file_array = array($counter=>array("file" => "../Images/Catalogue_Thmbs/365/365lot" . $rowї'LotNo'] . ".jpg", "mimetype" => "image/jpeg", "filename" => "365lot" . $rowї'LotNo'] . ".jpg"));

$message .= "Lot " . $rowї'LotNo'] . " " . $rowї'Decription'] . " SOLD FOR £" . $rowї'HammerPrice'] ."\n\n"  ;
		$counter = $counter + 1;
	}
I do appreciate that the variable $file_array is being cleared out every iteration, but I dont understand how to APPEND the sub arrays into the main array.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

use

Code: Select all

$file_array[] = ...
to append an element to $file_array.
What's the purpose of $counter? if you use

Code: Select all

$file_array = array();
while ($row = $salehighlights->fetchrow ())
{
	$file_array[] = array(
			"file" => "../Images/Catalogue_Thmbs/365/365lot" . $row['LotNo'] . ".jpg",
			"mimetype" => "image/jpeg",
			"filename" => "365lot" . $row['LotNo'] . ".jpg"
		);
}
each key/index of $file_array is equivalent to your $counter-variable
leoden
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 8:48 pm

Post by leoden »

Cheers volka that works great.

The reason I used counter was that I copied this code from a mail example the orignal code was

Code: Select all

$file_array = array(0=>array('file'=>'/complete/path/to/myfile.jpg',

'mimetype'=>'image/jpeg',

'filename'=>'myfile.jpg'));
So due to my lack of understanding regarding arrays I thought that everytime the loop iterated the array '$file_array' was being loaded with the new array in position '0' so I whacked a counter in to see if that helps.

I did do alot of searching regarding multidimensional array but all the examples used hard coding rather than dynamic data!
Post Reply