Jump to content

Adding a second conditional statement


TheTasteofAussie

Recommended Posts

I have been told to add    <?php if ( isset( $product->currency ) ) : ?>   into the below code to check if the "currency" field has been set.

If it hasn't been set (ie left empty) then I want to show the Aussie dollaras default   <img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Australia.jpg" /> $AUD 

I've tried putting it in different places and all I get is a blank page. I think there might need to be an "else" somewhere so i put in all sorts of different elseif's and still ended up with a blank page. I am wondering if anybody could put me on the right track please.

Thanks Rowan

    <?php if ($product->saleprice > 0 &&  $product->saleprice < $product->price) { ?>
  <div class="prc">
    <small>
      On Sale: <span style="font-weight: bold; color:red;"><?php if ($product->currency == 'USD') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_States.jpg" /> $USD 
<?php elseif ($product->currency == 'USD$') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_States.jpg" /> $USD 
<?php elseif ($product->currency == 'GBP') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_Kingdom.jpg" /> £ 
<?php elseif ($product->currency == '£') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_Kingdom.jpg" /> £ 
<?php elseif ($product->currency == 'AUD') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Australia.jpg" /> $AUD 
<?php elseif ($product->currency == 'AUD$') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Australia.jpg" /> $AUD 
<?php elseif ($product->currency == 'CDN$') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Canada.jpg" /> $CDN 
<?php elseif ($product->currency == 'RM') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Malaysia.jpg" />
<?php elseif ($product->currency == 'CDN') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Canada.jpg" /> $CDN 
<?php elseif ($product->currency == 'NZD') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_New_Zealand.jpg" /> $NZD 
<?php elseif ($product->currency == 'NZD$') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_New_Zealand.jpg" /> $NZD 
<?php endif; ?> [product.saleprice]</span>
    </small>
  </div>
<?php } else { ?>
  <div class="prc">
    <small>
     <?php if ($product->currency == 'USD') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_States.jpg" /> $USD 
<?php elseif ($product->currency == 'USD$') : ?>
      <img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_States.jpg" /> $USD 
<?php elseif ($product->currency == 'GBP') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_Kingdom.jpg" /> £ 
<?php elseif ($product->currency == '£') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_the_United_Kingdom.jpg" /> £ 
<?php elseif ($product->currency == 'AUD') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Australia.jpg" /> $AUD 
<?php elseif ($product->currency == 'AUD$') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Australia.jpg" /> $AUD 
<?php elseif ($product->currency == 'CDN$') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Canada.jpg" /> $CDN 
<?php elseif ($product->currency == 'RM') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Malaysia.jpg" />
<?php elseif ($product->currency == 'CDN') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_Canada.jpg" /> $CDN 
<?php elseif ($product->currency == 'NZD') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_New_Zealand.jpg" /> $NZD 
<?php elseif ($product->currency == 'NZD$') : ?>
<img src="http://aussietaste.recipes/wp-content/uploads/flags/Flag_of_New_Zealand.jpg" /> $NZD 
<?php endif; ?> [product.price]
    </small>
  </div>    
<?php } ?>
 

Link to comment
Share on other sites

two things when programming -

 

1) Don't Repeat Yourself (DRY). you have two blocks of code that only differs in the On Sale: <span ...></span> text/markup. you should only write out program logic when you have different things you need to do. the common block of code, containing all the mapping of currency to flag image and display symbol, should not be repeated.

 

2) when all you are doing is mapping one value to other values, doesn't write out program logic (if/else/switch/case.) use a simple lookup array to do the mapping.

 

by simplifying the code, it will be easy to see where and how you should ad the logic to default to the AUD value.

 

i'll post some code showing how simply this can be done ....

Edited by mac_gyver
Link to comment
Share on other sites

And don't use multiple identifiers to denote the same currency. You have “GBP”, but you also have “£”, which effectively requires you to do every check twice.

 

Use a single unique identifier. The ISO code is a good start.

The problem I have is that when the data is imported from different sources some use the currency symbol and others use the text. What I am trying to achieve is a consitent pricing look. I have no control over the input data. And the problem I now have is for those entries that have no currency annotation. They are defaulting to $USD instead of $AUD.

Edited by TheTasteofAussie
Link to comment
Share on other sites

two things when programming -

 

1) Don't Repeat Yourself (DRY). you have two blocks of code that only differs in the On Sale: <span ...></span> text/markup. you should only write out program logic when you have different things you need to do. the common block of code, containing all the mapping of currency to flag image and display symbol, should not be repeated.

 

2) when all you are doing is mapping one value to other values, doesn't write out program logic (if/else/switch/case.) use a simple lookup array to do the mapping.

 

by simplifying the code, it will be easy to see where and how you should ad the logic to default to the AUD value.

 

i'll post some code showing how simply this can be done ....

I'm guessing DRY is a novice indicator, which is definitely my realm. Certainlylooking forward to an alternative. Thank you

Link to comment
Share on other sites

The problem I have is that when the data is imported from different sources some use the currency symbol and others use the text.

 

Then fix the input data as soon as it reaches your application. DRY also means that you don't repeat the same validation everywhere in your code.

Link to comment
Share on other sites

Then fix the input data as soon as it reaches your application. DRY also means that you don't repeat the same validation everywhere in your code.

I am using a third party to aggregate the datafeeds and the use a plugin to update my product database. There only place I can achieve the change is manually before I import to my website. That amounts to some 2000+ products. i think that the over head of the vaidation would be better than the time require tomanually edit each entry.

Link to comment
Share on other sites

If it hasn't been set (ie left empty) 

 

 

in php, empty is not the same thing that isset() tests. i suspect you mean a non-empty string?

 

see the following example code -  


$flag_path = "http://aussietaste.recipes/wp-content/uploads/flags/";

// note: currency SYM and SYM$ map to just SYM due to trimming of any trailing $ by the code
// define an array that maps input values to output values
$map['USD'] = array('flag'=>'Flag_of_the_United_States.jpg','symbol'=>'$USD');
$map['GBP'] = array('flag'=>'Flag_of_the_United_Kingdom.jpg"','symbol'=>'£');
$map['£'] = array('flag'=>'Flag_of_the_United_Kingdom.jpg"','symbol'=>'£');
$map['AUD'] = array('flag'=>'Flag_of_Australia.jpg','symbol'=>'$AUD');
// add other mappings here...

$currency = $product->currency != '' ? rtrim($product->currency,'$') : 'AUD'; // use value (less any trailing $) or default to 'AUD'

if(!isset($map[$currency]))
{
    // no mapping exists
    echo "The currency value {$product->currency} isn't defined.";
} else {
    // the currency is defined, use it
    $arr = $map[$currency];
    $cur = "<img src='$flag_path{$arr['flag']}' alt='' /> {$arr['symbol']} ";
    ?>
    <div class='prc'>
        <small>
        <?php
        if($product->saleprice > 0 &&  $product->saleprice < $product->price){
            echo 'On Sale: <span style="font-weight: bold; color:red;">';
            echo $cur;
            echo " [product.saleprice]</span>";
        } else {
            echo $cur;
            echo " [product.price]";
        }
        ?>
        </small>
    </div>
<?php
}
?>
Link to comment
Share on other sites

I am using a third party to aggregate the datafeeds and the use a plugin to update my product database. There only place I can achieve the change is manually before I import to my website. That amounts to some 2000+ products. i think that the over head of the vaidation would be better than the time require tomanually edit each entry.

 

Who said anything about manual checks? Wordpress plug-ins can be extended, so the import procedure can be fixed simply by hooking into it.

 

This is not just about the currency chaos. The problem is that you appearently accept any input without even the most basic validation and normalization. Never do that, especially when you're dealing with actual money (or is this just one of those “school projects”?).

Link to comment
Share on other sites

Thank you, I'm now a lot closer but the page is still awry . Here is the whole template with the code added as a test.

<table style="border: thin solid #E6E6E6; width: 100%; background-color: #cecece;">
<tr style="border: none;">
  <td align="left" style="width: 50%; border: none; text-align: left;"><form method="get">
      <div style="color: Black;">Products per page:</div><?php echo datafeedr_tools()->form_select("ppp",
        array(
        "12" => "12",
        "24" => "24",
        "36" => "36",
        "48" => "48",
        )); ?><input type="submit" value="Change" class="dfbuttoncat dfblue dfmedium dfrounded" style="background-color: #008dc2; width: 50%; margin-left: 30px;"></form></td>
  <td align="left" style="width: 50%; border: none; text-align: left;"><form method="get">
    <div style="color: Black;">Sort by:</div><?php echo datafeedr_tools()->form_select("sort",
        array(
        "price"   => "Price (low to high)",
        "-price"   => "Price (high to low)",
        "name"     => "Product Name (a - z)",
        "-name"   => "Product Name (z - a)",
        )); ?><input type="submit" value="Sort" class="dfbuttoncat dfblue dfmedium dfrounded" style="background-color: #008dc2; width: 50%; margin-left: 30px;"></form></td>
</tr></table>   
<div class="pl">
  [product.each]
  <div class="t1 col3">
  <div class="bdr">

    <div class="thmb"><a href="[product.link]" title="[product.name]">[product.image tag='1']</a></div>
        <h2><a href="[product.link]" title="[product.name]">[product.name]</a></h2>
    <p class="desc">[product.description cut='20 words']</p>
    <?php if ($product->saleprice > 0 &&  $product->saleprice < $product->price) { ?>
  <div class="prc">
    <small>
$flag_path = "http://aussietaste.recipes/wp-content/uploads/flags/";

// note: currency SYM and SYM$ map to just SYM due to trimming of any trailing $ by the code
// define an array that maps input values to output values
$map['USD'] = array('flag'=>'Flag_of_the_United_States.jpg','symbol'=>'$USD');
$map['GBP'] = array('flag'=>'Flag_of_the_United_Kingdom.jpg"','symbol'=>'£');
$map['£'] = array('flag'=>'Flag_of_the_United_Kingdom.jpg"','symbol'=>'£');
$map['AUD'] = array('flag'=>'Flag_of_Australia.jpg','symbol'=>'$AUD');
// add other mappings here...

$currency = $product->currency != '' ? rtrim($product->currency,'$') : 'AUD'; // use value (less any trailing $) or default to 'AUD'

if(!isset($map[$currency]))
{
    // no mapping exists
    echo "The currency value {$product->currency} isn't defined.";
} else {
    // the currency is defined, use it
    $arr = $map[$currency];
    $cur = "<img src='$flag_path{$arr['flag']}' alt='' /> {$arr['symbol']} ";
    ?>
    <div class='prc'>
        <small>
        <?php
        if($product->saleprice > 0 &&  $product->saleprice < $product->price){
            echo 'On Sale: <span style="font-weight: bold; color:red;">';
            echo $cur;
            echo " [product.saleprice]</span>";
        } else {
            echo $cur;
            echo " [product.price]";
        }
        ?>
        </small>
    </div>
<?php
}
?>
    </small>
  </div>    
<?php ?>

    <div class="btns">          [product.merchant_logo]<br><br>
<a href="[product.link]" title="View details [product.name]" class="dfbutton dfblue dflarge dfrounded">More Details</a>
    </div>
    <div class="clearingdiv"> </div>
  </div>
  </div>
  [/product.each]
  <div class="clearingdiv"> </div>
</div>

<div class="pgr">
  Pages:
  [pager.first text='<img src="[server.plugin_url]/first.gif" alt="first page" />' alt='<img src="[server.plugin_url]/first_off.gif" alt="first page" />']
  [pager.prev text='<img src="[server.plugin_url]/prev.gif" alt="prev page" />' alt='<img src="[server.plugin_url]/prev_off.gif" alt="prev page" />']
  [pager.pages]
  [pager.next text='<img src="[server.plugin_url]/next.gif" alt="next page" />' alt='<img src="[server.plugin_url]/next_off.gif" alt="next page" />']
  [pager.last text='<img src="[server.plugin_url]/last.gif" alt="last page" />' alt='<img src="[server.plugin_url]/last_off.gif" alt="last page" />']
</div>

I think I've missed something along the way

Link to comment
Share on other sites

This is not just about the currency chaos. The problem is that you appearently accept any input without even the most basic validation and normalization. Never do that, especially when you're dealing with actual money (or is this just one of those “school projects”?).

To me it is just about the currencies. I can check the inputs from the merchants quite quickly however I need to manually make any changes. Some merchants don't put anything into the currency field and the default is USD. If I could change the default I would but that is another story again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.