View Full Version : simple If Else problem, can anyone help?

12-01-2010, 12:07 AM

Hopefully someone can help me. My knowledge of PHP doesnt go any further than looking at identifying areas of code and copy and paste - as basic as you get!

Basically i have a php script installed that produces an RSS feed for me to use with google base. There is a little problem were it does not pull the sale price(known as special) into googlebase and it will instead show the original price. I have identified what needs to be changed but i dont know the correct syntax.

I somehow need to get something similar to the following IF ELSE into the code below (this is taken from the same php script that shows the special price on the actual rss feed)

foreach ($product_data as $result) {
$special = $model_catalog_product->getProductSpecial($result['product_id']);
if ($special) {
$price = $special;
} else {
$price = $result['price'];
if ($useTax) {
$price = $currency->format($tax->calculate($price, $result['tax_class_id'], $config->get('config_tax')), $currency->getCode());
} else {
$price = $currency->format($price, $currency->getCode());

The code below is were i need the If Else inserted.

<?php foreach ($products as $product) { ?>
<title><?php echo htmlspecialchars($product['name'], ENT_QUOTES, 'utf-8'); ?></title>
<g:brand><?php echo htmlspecialchars($product['brand'], ENT_QUOTES, 'utf-8'); ?></g:brand>
<g:product_type><?php echo $taxonomy; ?></g:product_type>
<description><?php echo htmlspecialchars($product['desc'], ENT_QUOTES, 'utf-8'); ?></description>
<link><?php echo str_replace('&', '&amp;', $product['href']); ?></link>
<pubDate><?php echo $product['add_date']; ?></pubDate>
<!--guid isPermaLink="true"><?php echo $product['href']; ?></guid-->
<g:id><?php echo (int)$product['id']; ?></g:id>
<!--g:upc><?php echo sprintf("%012s", $product['id']); ?></g:upc-->
<g:image_link><?php echo $product['thumb']; ?></g:image_link>
<g:price><?php echo $product['price']; ?></g:price>
<g:mpn><?php echo htmlspecialchars($product['model'], ENT_QUOTES, 'utf-8'); ?></g:mpn>
<g:weight><?php echo $product['weight']; ?></g:weight>
<g:currency><?php echo $config->get('config_currency'); ?></g:currency>
<?php } ?>

The <g:price> is what googlebase reads to get the price of the product. The problem seems to be that there is no if else statement here to output the special price if there is one.

Any help would be greatly appreciated, i tired emailing the developer but i have been ignored and as you can see i'm pretty clueless

12-01-2010, 02:25 AM
Place your echo statement into an if/else statement:

echo $special;
} else {
echo $result['price'];

Or, you can determine the value of price with the if statement ahead of time and then you won't have to change anything in the below code. What an if statement does is determine if the parameters in the parenthesis are true or false (in this case, if $special is defined or undefined) and then execute the code in the {}s if it is true. If you place an else statement after an if statement, that code will be executed if the if statement returns false. an else if statement is like an if statement, except it won't execute unless the if or else if statement before it is false. If you know what parameter you want to check and what code you want to execute, an if statement is simple to use anywhere and can be a very handy resource.