PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 25th, 2013, 07:12 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
Alternating CSS Styles with Array Output

Hi There

Is there a way of choosing which CSS styles to execute when displaying items in an array?

Depending on the size and orientation of the image within the image key of the array, I need to be able to chose the appropriate div container to display the product/items?

So for example say I had an array of products:

PHP Code:
 $deals = array(
        
"products"=> array(
            array(
'productTitle' => "Title1",'productImage' => "images/img1.jpg",'productDescription' => " Desc1",'rrp' => "£69.99",'salePrice' => "£60.00"),
            array(
'productTitle' => "Tile2",'productImage' => "images/img2.jpg",'productDescription' => " Desc2",'rrp' => "£69.99",'salePrice' => "£60.00")); 


I have a stylesheet that can be applied to item one, and all other array items with image orientations similar to item 1. This is a container div that has a fixed height and width (css1.css). However, say item2 in the array needs to have specific elements positioned differently (however using the same main container) but with different css to position the image, title and description within the container div?

I know how to echo and apply the styles to item1 and similar items to the first produt in the array:

PHP Code:
foreach ($deals as $k => $v)
{
  echo 
"<div class=\"header\"><h2>$k:</h2><br>";
  if(
is_array($v))
  {
    foreach (
$v as $key => $val)
     {
         echo 
"<div class=\"wrapper\">";
          echo 
"<img class=\"logo\" src=\"".$val['logo']."\">";
           echo 
"<h2>".$val['productTitle']."</h2>";
          echo 
"<img class=\"container\" src=\"".$val['productImage']."\">";
         echo 
"<div class=\"description\">".$val['productDescription']; echo "</div>";
          echo 
"<div class=\"bottom\">RRP "." ".$val['rrp']."<br>"."NOW ONLY "."".$val['salePrice']; echo "</div>";
    }
       echo 
"</div>";
  }
     echo 
"</div>";



I was thinking of give each item in the array an key=>value of 'id' => '0' for the default items, and 'id' => '1' for items that need the different stylesheet/styles applied?

So when I loop through the items with foreach, if you encounter an id=1, apply the different styles.

Hope this makes sense and someone can advise?

Volterony

Reply With Quote
  #2  
Old January 25th, 2013, 08:13 AM
NotionCommotion NotionCommotion is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Sep 2006
Posts: 1,513 NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 32 m 46 sec
Reputation Power: 526
Add a class name to your $deals['products'] array, and then add the class to some parent element of your HTML. Use only one cssfile.css, but use classes to apply different CSS to the different DIVs.

PHP Code:
foreach ($v as $key => $val)
{
  echo 
'<div class="wrapper '.$val['class'].'">';
  ....



Note how I used single quotes instead of escaped outer quotes as you used? Your way also works, but is harder to read and easier to make mistakes.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Alternating CSS Styles with Array Output

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap