Echo Array Data

I’m new to all of this and am not sure how to retrieve certain data. Here’s my query…


$sql = 'SELECT item.*, attributes.*
          FROM item
          LEFT JOIN attributes
          ON item.ProductNumber = attributes.ProductNumber
          WHERE item.ProductNumber = 9503731
          ORDER BY attributes.AttributePriority';
$result = mysql_query($sql);
while ($row = mysql_fetch_array($results))  {
   $ProductNumber = $row['ProductNumber'];
   $Image = $row['ItemImage'];
   $AttribPriority =  $row['AttributePriority'];
   $AttribName = $row['AttributeName'];
   $AttribValue =  row['AttributeValue'];
   $Att_array = array($AttPriority, $AttName, $AttValue);
}

This is the information I am having trouble rendering properly. It is my ATTRIBUTES table. Every product in the ITEMS table has the potential for up to 20 attributes where I would like to display both the AttributeName & AttributeValue. I am trying to echo each attrib name & value.

This is how the data is stored in the ‘attribute’ table:

ProductNumber AttributePriority AttributeName AttributeValue
9503731 4 Clerk Totalizers [Nom] 4
9503731 5 Compartments 4 Bill Drawer
9503731 5 Compartments 5 Coin Drawer

It was previously suggested that I set up my code like this:


$getAll_array  = array();
while ($row = mysql_fetch_array($results))  {
   array_push($getAll_array, $row);
}

If I print_r($getAll); I see all the data within the array. I’m just not sure how to retrieve the rows individually so I can place the variable in HTML for display. I apologize in advance if I’m not being 100% clear.

Thanks for any help.

Hi mainstreetop, welcome to the forums,

Your first bit of code will assign the values of whatever the last $row is to the variables inside the while{} as each pass will overwrite the older assigned values.

Probably not what you want it to do.

Putting them into an array is a good idea. Using print_r (or var_dump) will show you what that array looks like. i.e. the $key -> $values

Once you know this information it’s easier to figure out how to get at the parts you’re interested in. What does that (at least a small portion of it) look like?

To retieve each element of the array?


foreach($getAll as $key=>$val)
{
echo $key . '->'. $val;
}

Mittineague & mjpr

Thank you both for taking the time to respond.

@ Mittineague…
Your explanation helped me realize that I only needed to put the ‘attributes’ fields within the while(). So, this has been solved - thank you.

@ Mittineague & mjpr…
What I still do not understand is how to selectively retrieve array data. If I were to place the $results in an array …

$all_array = array();
  $result = mysql_query($query)  or die(mysql_error());
  while ($row = mysql_fetch_array($result)) {
   array_push($all_array,$row);
  }
  

I get what’s below. There is a lot more, but I cut it off for the sake of of space…

Array ( [0] => Array ( [0] => 764 [RecordID] => 764 [1] => 5099858 [ProductNumber] => 5099858 [2] => ALE [Prefix] => ALE [3] => 36650 [StockNumber] => 36650 [4] => ALERA [MFRName] => ALERA [5] => 36 [MFRId] => 36 [6] => 201 [BrandId] => 201 [7] => 40799 [SkuGroupId] => 40799 [8] => 63 [ProductClassId] => 63 [9] => L [AssemblyCode] => L [10] => N [RecycleInd] => N [11] => N [EPACPGCode] => N [12] => N [MSDSInd] => N [13] => Y [StatusCode1] => Y [14] => Includes locking cabinet, three-outlet power strip [PackageIncludes] => Includes locking cabinet, three-outlet power strip [15] => Adjustable Steel Monitor Cart w/Cabinet, 4-Shelf, 28 x 24 x 56, Black [LongItemDesc] => Adjustable Steel Monitor Cart w/Cabinet, 4-Shelf, 28 x 24 x 56, Black [16] => ALE36650_1_1.JPG [ItemImage] => ALE36650_1_1.JPG [17] => NOA.JPG [ItemAltImage] => NOA.JPG [18] => Y [ItemStatus] => Y [19] => F [NonReturnableCode] => F [20] => N [SmallPackageInd] => N [21] => [ValuePack] => [22] => Adjustable Steel Monitor Cart;ALERA;Monitor Cart;Steel Monitor Cart [Keywords] => Adjustable Steel Monitor Cart;ALERA;Monitor Cart;Steel Monitor Cart [23] => N [WarrantyInd] => N [24] => [WarrantyComments] => [25] => A [ShipClassCode] => A [26] => Y [GreenInd] => Y [27] => [GreenInfo] => [28] => 1049 [ListAmount] => 1049 [29] => EA [InventoryUnit] => EA [30] => 108 [ItemWeight] => 108 [31] => 31 [ItemWidth] => 31 [32] => 0 [ItemLength] => 0 [33] => 38 [ItemHeight] => 38 [34] => 0 [BoxWeight] => 0 [35] => 0 [BoxWidth] => 0 [36] => 0 [BoxLength] => 0 [37] => 0 [BoxHeight] => 0 [38] => EA [BoxUOM] => EA [39] => 108 [CartonWeight] => 108 [40] => 31 [CartonWidth] => 31 [41] => 27 [CartonLength] => 27 [42] => 38 [CartonHeight] => 38 [43] => EA [CartonUOM] => EA [44] => 2147483647 [UPCRetail] => 2147483647 [45] => 5099858 [46] => ALE [47] => 36650 [48] => 21 [AttributePriority] => 21 [49] => Adjustability [AttributeName] => Adjustability [50] => Height adjusts from 40" to 56". [AttributeValue] => Height adjusts from 40" to 56". [51] => Alera� [BrandName] => Alera� [52] => ALERA_LOGO.JPG [BrandLogo] => ALERA_LOGO.JPG [53] => 764 [54] => 5099858 [55] => ALE [56] => 36650 [57] => Alera® Adjustable Steel Monitor Cart with Power S [SkuGroupName] => Alera® Adjustable Steel Monitor Cart with Power S [58] => 40799 [59] => Black adjustable steel monitor cart has three-outlets, 25 ft. cord and casters. [SummarySellingStatement] => Black adjustable steel monitor cart has three-outlets, 25 ft. cord and casters. [60] => Holds monitors up to 27" wide. [SellingPoint1] => Holds monitors up to 27" wide. [61] => Height adjusts for personal viewing levels. [SellingPoint2] => Height adjusts for personal viewing levels. [62] => Wheels enable mobility with ease. [SellingPoint3] => Wheels enable mobility with ease. [63] => Nonfuming. [SellingPoint4] => Nonfuming. [64] => For wood, asphalt, linoleum, vinyl, vinyl composition, rubber, terrazzo, concrete and gymnasium floors. [SellingPoint5] => For wood, asphalt, linoleum, vinyl, vinyl composition, rubber, terrazzo, concrete and gymnasium floors. [65] => Treatment will not harm waxed or finished surfaces. [SellingPoint6] => Treatment will not harm waxed or finished surfaces. [66] => Floors will appear brighter and more lustrous. [SellingPoint7] => Floors will appear brighter and more lustrous. [67] => Wall or table mount. [SellingPoint8] => Wall or table mount. [68] => Optional battery back-up for printing during power outages. [SellingPoint9] => Optional battery back-up for printing during power outages. [69] => Requires time cards, sold separately. [SellingPoint10] => Requires time cards, sold separately. [70] => Height adjusts from 40" to 56". Holds monitors up to 27" wide. Three-outlet power strip with 25-ft. cord and storage bracket. 4" casters, two locking. [ShortSellingCopy] => Height adjusts from 40" to 56". Holds monitors up to 27" wide. Three-outlet power strip with 25-ft. cord and storage bracket. 4" casters, two locking. [71] => Height adjusts from 40" to 56". Holds monitors up to 27" wide. Three-outlet power strip with 25-ft. cord and storage bracket. 4" casters, two locking. [MediumSellingCopy] => Height adjusts from 40" to 56". Holds monitors up to 27" wide. Three-outlet power strip with 25-ft. cord and storage bracket. 4" casters, two locking. [72] => Height adjusts from 40" to 56". Holds monitors up to 27" wide. Three-outlet power strip with 25-ft. cord and storage bracket. 4" casters, two locking. [LongSellingCopy] => Height adjusts from 40" to 56". Holds monitors up to 27" wide. Three-outlet power strip with 25-ft. cord and storage bracket. 4" casters, two locking. [73] => Affordable white uncoated paper plates. Lightweight design allows plates to be use for multiple purposes. [ConsolidatedCatalogCopy] => Affordable white uncoated paper plates. Lightweight design allows plates to be use for multiple purposes. [74] => ALE36650_1_1.JPG [SkuGroupImage] => ALE36650_1_1.JPG [75] => NOA.JPG [SkuGroupAltImage] => NOA.JPG [76] => NOA.JPG [SkuGroupIcon] => NOA.JPG ) [1] => Array ( [0] => 764 [RecordID] => 764 [1] => 5099858 [ProductNumber] => 5099858 [2] => ALE [Prefix] => ALE [3] => 36650 [StockNumber] => 36650 [4] => ALERA [MFRName] => ALERA [5] => 36 [MFRId] => 36 [6] => 201 [BrandId] => 201 [7] => 40799 [SkuGroupId] => 40799 [8] => 63 [ProductClassId] => 63 [9] => L [AssemblyCode] => L [10] => N [RecycleInd] => N [11] => N [EPACPGCode] => N [12] => N [MSDSInd] => N [13] => Y [StatusCode1] => Y [14] => Includes locking cabin …

I do not understand how to pick & choose what I want out of this array. Also, because it’s in a while statement, it looks like each row is being called again for every attribute. Hopefully, that made sense???

The print_r shows that $all_array is an array and that each member contains an array of numerical and associative key value pairs (152 of them!).

[0] => Array ( 
	[0] => 764 
	[RecordID] => 764 
	[1] => 5099858 
	[ProductNumber] => 5099858 
	[2] => ALE 
	[Prefix] => ALE 
	[3] => 36650 
	[StockNumber] => 36650 
	[4] => ALERA 
	[MFRName] => ALERA 
	[5] => 36 
	[MFRId] => 36 
	[6] => 201 
	[BrandId] => 201 
	[7] => 40799 
	[SkuGroupId] => 40799 
	[8] => 63 
	[ProductClassId] => 63 
	[9] => L 
	[AssemblyCode] => L 
	[10] => N 
	[RecycleInd] => N 
.......
	)
[1] => Array ( 
	[0] => 764 
	[RecordID] => 764 
	[1] => 5099858 
	[ProductNumber] => 5099858 
	[2] => ALE 
	[Prefix] => ALE 
	[3] => 36650 
	[StockNumber] => 36650 
	[4] => ALERA 
	[MFRName] => ALERA 
	[5] => 36 
	[MFRId] => 36 
	[6] => 201 
	[BrandId] => 201 
	[7] => 40799 
	[SkuGroupId] => 40799 
	[8] => 63 
	[ProductClassId] => 63 
	[9] => L 
	[AssemblyCode] => L 
	[10] => N 
	[RecycleInd] => N 
.......

So

echo $all_array[0][0];
// outputs 764
echo $all_array[0]['RecordID'];
// outputs 764
foreach ($all_array as $row_array )
{
	echo $row_array['RecordID'] . "<br />\
";
}
// outputs 764<br />
// 764<br />
// ..... 

Thank you Mittineague for explaining it further. It is starting to make sense. I really appreciate you following my thread. My background is peddling these products - not building a site (obviously). With a little more practice, though, maybe I’ll get this thing where I think it should be.

Thanks again.

Glad to help. Working with arrays, especially multi-dimensional arrays, can be a bit overwhelming at first. But it gets easier.