SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with an array

    Notice this array, which is a result-set generating by cakephp.

    Array
    ([0] => Array
    ( [Inventory] => Array
    ( [id] => 38 [title] => Komatsu - 2001 [description] => Great Value Excavator. [cost] => $75,000 [ispublish] => 1 [created] => 2007-05-04 13:37:13 [modified] => 2007-05-04 13:37:13 )
    [Photo] => Array
    (
    [0] => Array ( [id] => 24 [img] => gary_dunham_ver2.gif [inventory_id] => 38 )
    [1] => Array ( [id] => 25 [img] => gary_dunham_ver3.gif [inventory_id] => 38 )
    [2] => Array ( [id] => 23 [img] => gary_dunham_ver1.gif [inventory_id] => 38 )
    )
    )
    [1] => Array
    ( [Inventory] => Array
    ( [id] => 40 [title] => John Deere 2002 [description] => What an Excellent Buy. [cost] => $32,000 [ispublish] => 1 [created] => 2007-05-05 15:10:29 [modified] => 2007-05-05 15:10:29 )
    [Photo] => Array
    (
    [0] => Array ( [id] => 27 [img] => ist2_2055425_medium_sized_excavator.jpg [inventory_id] => 40 )
    [1] => Array ( [id] => 26 [img] => ist2_3130412_backhoe.jpg [inventory_id] => 40 )
    )
    )
    )

    i'm having troubles trying to print out this array. the desired result would look something like this:

    [Img][title][cost] [view details]

    what i need is only one of the keys from the [Photo] array to be printed, but i have no idea how to do this, or loop through more than 1 basic array at a time.

    this was my attempt which was obviously flawed:

    PHP Code:
    <?php foreach ($inventories as $inventory): ?>
    <tr>
        <td><?php echo $photos['0']['Photo']['img'];?></td>
        <td><?php echo $inventory['Inventory']['title']; ?></td>
        <td><?php echo $inventory['Inventory']['description']; ?></td>
        <td><?php echo $inventory['Inventory']['cost']; ?></td>
    </tr>
    <?php endforeach; ?>
    hopefully this is enough info and someone might be able to assist

  2. #2
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And where is a problem? With photo I guess?

    Then use this:
    PHP Code:
    <?php foreach ($inventories as $key => $inventory): ?>
    <tr>
        <td><?php echo $photos[$key]['img'];?></td>
        <td><?php echo $inventory['Inventory']['title']; ?></td>
        <td><?php echo $inventory['Inventory']['description']; ?></td>
        <td><?php echo $inventory['Inventory']['cost']; ?></td>
    </tr>
    <?php endforeach; ?>

  3. #3
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Mike but i get this:

    Notice: Undefined variable: photos in C:\apache2triad\htdocs\garydunhamequipment\app\views\inventories\index.thtml on line 14

  4. #4
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    type $Photo instead of $photos

  5. #5
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, i've already tried that and it didn't work either.

  6. #6
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show some code that is above that one?

  7. #7
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mike,

    it's in a view layer of my app. There's really nothing else i'm looking to do besides work with that array and other code isn't really relevant to it. i have those values that i need to take that array, and turn into something like

    <table>
    <tr>
    <td>Image1</td>
    <td>Title 1</td>
    <td>Description 1</td>
    <td>Cost 1</td>
    </tr>
    <tr>
    <td>Image2</td>
    <td>Title 2</td>
    <td>Description 2</td>
    <td>Cost 2</td>
    </tr>
    </table>

    as you can see, part of the challenge is taking only one of [Photo] array and displaying it. i know i need to write a custom function or maybe even a class to sort this but need some assistance in figuring this out.

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm having some trouble reading the indenting.

    Is it...
    $arr[0]['Photo'][0]['img']
    or ...
    $arr[0]['Inventory']['Photo'][0]['img']
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rockit, which array are you print_r()'ing there, $inventory or $photos? You could try something like this, although I'm not sure it would work:
    PHP Code:
    <?php foreach ($inventories as $key => $inventory): ?>
    <tr>
        <td><?php echo $inventory['Photo']['0']['img'];?></td>
        <td><?php echo $inventory['Inventory']['title']; ?></td>
        <td><?php echo $inventory['Inventory']['description']; ?></td>
        <td><?php echo $inventory['Inventory']['cost']; ?></td>
    </tr>
    <?php endforeach; ?>
    I assume you're print_r()'ing $inventory, could you print_r() $photos for us as well or tell us what values you expect to be stored in it?

    samsm: If you're on Firefox, Right click > View source and you can see the original tab indenting of his print_r output. Here's the indented version:
    Code:
    Array
    ([0] => Array
    	( [Inventory] => Array
    		( [id] => 38 [title] => Komatsu - 2001 [description] => Great Value Excavator. [cost] => $75,000 [ispublish] => 1 [created] => 2007-05-04 13:37:13 [modified] => 2007-05-04 13:37:13 )
    
    		[Photo] => Array
    			(
    				[0] => Array ( [id] => 24 [img] => gary_dunham_ver2.gif [inventory_id] => 38 )
    				[1] => Array ( [id] => 25 [img] => gary_dunham_ver3.gif [inventory_id] => 38 )
    				[2] => Array ( [id] => 23 [img] => gary_dunham_ver1.gif [inventory_id] => 38 )
    			)
    		)
    [1] => Array
    	( [Inventory] => Array
    		( [id] => 40 [title] => John Deere 2002 [description] => What an Excellent Buy. [cost] => $32,000 [ispublish] => 1 [created] => 2007-05-05 15:10:29 [modified] => 2007-05-05 15:10:29 )
    		[Photo] => Array
    			(
    				[0] => Array ( [id] => 27 [img] => ist2_2055425_medium_sized_excavator.jpg [inventory_id] => 40 )
    				[1] => Array ( [id] => 26 [img] => ist2_3130412_backhoe.jpg [inventory_id] => 40 )
    			)
    	)
    )

  10. #10
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Akash Mehta View Post
    rockit, which array are you print_r()'ing there, $inventory or $photos? You could try something like this, although I'm not sure it would work:
    PHP Code:
    <?php foreach ($inventories as $key => $inventory): ?>
    <tr>
        <td><?php echo $inventory['Photo']['0']['img'];?></td>
        <td><?php echo $inventory['Inventory']['title']; ?></td>
        <td><?php echo $inventory['Inventory']['description']; ?></td>
        <td><?php echo $inventory['Inventory']['cost']; ?></td>
    </tr>
    <?php endforeach; ?>
    I assume you're print_r()'ing $inventory, could you print_r() $photos for us as well or tell us what values you expect to be stored in it?

    samsm: If you're on Firefox, Right click > View source and you can see the original tab indenting of his print_r output. Here's the indented version:
    Code:
    Array
    ([0] => Array
    	( [Inventory] => Array
    		( [id] => 38 [title] => Komatsu - 2001 [description] => Great Value Excavator. [cost] => $75,000 [ispublish] => 1 [created] => 2007-05-04 13:37:13 [modified] => 2007-05-04 13:37:13 )
    
    		[Photo] => Array
    			(
    				[0] => Array ( [id] => 24 [img] => gary_dunham_ver2.gif [inventory_id] => 38 )
    				[1] => Array ( [id] => 25 [img] => gary_dunham_ver3.gif [inventory_id] => 38 )
    				[2] => Array ( [id] => 23 [img] => gary_dunham_ver1.gif [inventory_id] => 38 )
    			)
    		)
    [1] => Array
    	( [Inventory] => Array
    		( [id] => 40 [title] => John Deere 2002 [description] => What an Excellent Buy. [cost] => $32,000 [ispublish] => 1 [created] => 2007-05-05 15:10:29 [modified] => 2007-05-05 15:10:29 )
    		[Photo] => Array
    			(
    				[0] => Array ( [id] => 27 [img] => ist2_2055425_medium_sized_excavator.jpg [inventory_id] => 40 )
    				[1] => Array ( [id] => 26 [img] => ist2_3130412_backhoe.jpg [inventory_id] => 40 )
    			)
    	)
    )
    worked properly, many tks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •