#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5

    Displaying items in array


    Hi there,

    I a $deals array and I want to echo out the brand names as <h2> with the sale items below within a container div. The big goal is to have about 15 different sports brands <h2> with around 10 - 20 sale items under it. (example below only shows 2 items so far).

    Code:
    <style type="text/css">
    	#wrapper
    	{
    		border: 1px dotted #ff6501;
    		border-top-left-radius: 10px;
    		border-top-right-radius: 10px;
    		background:url('../images/background.jpg');
    		margin: 6px;
    		width:200px;
    		height:290px;
    		float:left;
        }
    
        #wrapper .productTitle
        {
        	width:100%;
        	height:30px;
        	padding:0;
    	color:#000000;
    	font-size:8pt;
        	font-weight:bold;
        	text-align:center;
    	text-transform:none;
        }
    
        #wrapper h2  {
    		margin: 3px;
    		}
    
       #wrapper .description {
    		font-size: 10pt;
    		font-family: Arial, sans-serif;
    		font-weight: normal;
    		color:#000000;
    		height: 45px;
    		padding: 5px;
    
    	}
    
    	#wrapper .bottom {
        	font-size: 9pt;
        	width:200px;
        	height:10px;
        	font-family: Arial, sans-serif;
    	color:red;
            text-align:center;
    	border-top: 1px dotted #ff6501;
    	margin: 1px;
    	
       }
    	#wrapper .bottom p {
    		margin: 4px;
    
    }
    
    	</style>
    So far I have worked out how to echo the product div container, but not the sale information with it?

    PHP Code:
    <?php
    $deals
    = array("1"=>array("brand"=>"Nike "productTitle" => "Running Shoes",
                           "
    productImage" => "deals/runningshoes.jpg",
                           "
    productDescription" => " Fantastic Savings on Nike Running Shoes",
                           "
    rrp" => "&pound;69.99",
                           "
    salePrice" => "&pound;60.0"),
            array("
    2"=>array("brand"=>"Addidas", "productTitle" => "Football Boots",
                           "
    productImage" => "deals/footballboots.jpg",
                           "
    productDescription" => " Fantastic Savings on Addidas Football Boots",
                           "
    rrp" => "&pound;69.99",
                           "
    salePrice" => "&pound;60.0"),

                    );

                
            foreach (
    $deals as $deals)
            {
                echo "
    <div id=\'wrapper\'>"
                </div>";

            }
    ?>
    Really appreciate any help.

    Regards
    Volterony
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    the array expression is syntactically wrong, as you already tell by the broken syntax highlighting.

    The nesting of the elements also isn't right. You kind of tricked yourself by using false indentation. I guess you meant something like this:
    PHP Code:
    array(
        array(
            
    'brand' => ...
            , 
    'productTitle' => ...
            ...
        )
        , array(
            
    'brand' => ...
            , 
    'productTitle' => ...
            ...
        )
        ...

    However, if you want to actually group the sale items by the brand, you should make the brand name the "top level index" and have it point to an array of items:
    PHP Code:
    array(
        
    // Nike items
        
    'Nike' => array(
            array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            , array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            ...
        )
        
    // Adidas items
        
    'Adidas' => array(
            array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            , array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            ...
        )
        ...

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by Jacques1
    Hi,

    the array expression is syntactically wrong, as you already tell by the broken syntax highlighting.


    PHP Code:
    array(
        
    // Nike items
        
    'Nike' => array(
            array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            , array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            ...
        )
        
    // Adidas items
        
    'Adidas' => array(
            array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            , array(
                
    'productTitle' => ...
                , 
    'productImage' => ...
            )
            ...
        )
        ...

    Thanks for pointing that out. I see where it went wrong.

    So how to I actually display the items in the div container?

    Many thanks.

    Volterony
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by volterony
    So how to I actually display the items in the div container?
    With a nested loop. If you use the second structure from above (with the brands as the indices), you'd do something like this:
    PHP Code:
    foreach ($deals as $brand => $items) {
        
    // stuff for the brand (h2 etc.)
        
    echo '<div>';
        foreach (
    $items as $item) {
            
    // stuff for a particular item
        
    }
        echo 
    '</div>';

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by Jacques1
    With a nested loop. If you use the second structure from above (with the brands as the indices), you'd do something like this:
    PHP Code:
    foreach ($deals as $brand => $items) {
        
    // stuff for the brand (h2 etc.)
        
    echo '<div>';
        foreach (
    $items as $item) {
            
    // stuff for a particular item
        
    }
        echo 
    '</div>';

    Brilliant. I'll give that a try, thanks for your help.

    Volterony
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Hi there,
    I working with a php multidimentional array and I am trying to echo the items for each brand in a div container.

    At the moment, I have figured out how to echo the brand name with only 1 div. However there are 2 products foreach brand - Nike and Addidas! How do I echo out the two products for each brand into the div cotainer? So there will be the <h2> and two divs below for the two products for that brand?

    Here is the php I used so far:

    PHP Code:
    $deals = array(
            
    "Nike"=> array(
                array(
    'productTitle' => "Nike Trainers 1",'productImage' => "deals/nike1.jpg",'productDescription' => " Fantastic Savings on Nike Trainers",'rrp' => "&pound;69.99",'salePrice' => "&pound;60.00"),
                array(
    'productTitle' => "Nike Trainers 2",'productImage' => "deals/nike2.jpg",'productDescription' => " Fantastic Savings on Nike Trainers",'rrp' => "&pound;69.99",'salePrice' => "&pound;60.00")),

    "Addidas"=> array(
                array(
    'productTitle' => "Addidas Boots 1",'productImage' => "deals/addidas1.jpg",'productDescription' => " Fantastic Savings on Addidas Boots",'rrp' => "&pound;69.99",'salePrice' => "&pound;60.00"),
                array(
    'productTitle' => "Addidas Boots 2",'productImage' => "deals/addidas2.jpg",'productDescription' => " Fantastic Savings on Addidas Boots",'rrp' => "&pound;69.99",'salePrice' => "&pound;60.00")),
                
             
    );
    //close $deals array

    foreach ($deals as $brand =>$items)
    {
        echo 
    "<div class=\"header\"><h2>$brand:</h2><br>";
        echo 
    "<div id=\"dealwrapper\">";
        foreach (
    $deal as $items)
        {

        }
    echo 
    "</div>";
    }
    ?> 
    Help much appreciated.

    Volterony
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    The inner loop is wrong. Have a look at my code: You're looping over the $deals to get each $brand with the corresponding $items. For a particular $brand, you wanna loop over the $items to get each $item.

    Also the HTML is somehow messed up. You have
    Code:
    <div><h2>...</h2><br></div>
    There's a closing </div> missing, and that <br> makes no sense.

    And you need to escape strings before putting them into the HTML markup to make sure they don't contain any "bad" characters:

    PHP Code:
    function html_escape($raw_input)
    {
        return 
    htmlspecialchars($raw_inputENT_QUOTES ENT_HTML401'UTF-8');        // change this if you have a different character encoding than UTF-8
    }


    foreach (
    $deals as $brand => $items)
    {
        echo
            
    '<h2>' html_escape($brand) . ':</h2>'
            
    '<div id="dealwrapper">'
        
    ;
        foreach (
    $items as $item)
        {
            echo 
    'Title: ' html_escape($item['productTitle']) . '<br>';    // show title (or whatever you want)
        
    }
        echo 
    '</div>';

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by Jacques1
    The inner loop is wrong. Have a look at my code: You're looping over the $deals to get each $brand with the corresponding $items. For a particular $brand, you wanna loop over the $items to get each $item.

    Also the HTML is somehow messed up. You have
    Code:
    <div><h2>...</h2><br></div>
    There's a closing </div> missing, and that <br> makes no sense.

    And you need to escape strings before putting them into the HTML markup to make sure they don't contain any "bad" characters:

    PHP Code:
    function html_escape($raw_input)
    {
        return 
    htmlspecialchars($raw_inputENT_QUOTES ENT_HTML401'UTF-8');        // change this if you have a different character encoding than UTF-8
    }


    foreach (
    $deals as $brand => $items)
    {
        echo
            
    '<h2>' html_escape($brand) . ':</h2>'
            
    '<div id="dealwrapper">'
        
    ;
        foreach (
    $items as $item)
        {
            echo 
    'Title: ' html_escape($item['productTitle']) . '<br>';    // show title (or whatever you want)
        
    }
        echo 
    '</div>';

    Awesome Jacques1 !! Much appreciated.

    Volterony
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Resource


    Basically arrays are a tad different than other variables to output. You can either loop through the array, select a particular part of the array, or print the whole array.

IMN logo majestic logo threadwatch logo seochat tools logo