Notification with alert - php

I am trying to notify the user for their expired products. This works well. but i am not being able to display the product name in alert which is the variable $product->name.
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
?><script>alert('Product expired!');</script><?php
}
?>
<?php endforeach;?>

Just add the product name to the string:
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
?><script>alert('Product <?php echo $product->name; ?> expired!');</script><?php
}
?>
<?php endforeach;?>

Use echo
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
echo"<script>alert('Product expired!');</script>";
}
?>
<?php endforeach;?>

Related

using and multiple else if statement- output unexpected end of file

i have been trying to echo the codes and it not working properly. after reading up ifelse statement, i still cant figure what wrong with this statement in php outputting unexpected end of file.
<?php if($current_type){?>
<?php echo " Manage Product Type ";
echo "Menu Name:". $current_type["product_type"]."<br>";?>
Edit Subject;
<?php}elseif($current_category){?>
<h1> Manage Product category </h1>
<?php echo "Menu Name:". $current_category["category_name"];?>
<?php}elseif ($current_product){?>
<h1> Manage Products </h1>
<?php echo "Menu Name:". $current_product["product_names"];?>
<?php }else{?>
Please select a Product, category, or type
<?php};?>
This is a huge improvement on the readability of your code. It uses PHP alternate syntax for control structures.
<?php if($current_type): ?>
<h1>Manage Product Type</h1>
Menu Name: <?php echo $current_type["product_type"] ?><br>
Edit Subject
<?php elseif ($current_category): ?>
<h1>Manage Product category</h1>
Menu Name: <?php echo $current_category["category_name"] ?>
<?php elseif ($current_product): ?>
<h1>Manage Products</h1>
Menu Name <?php echo $current_product["product_names"] ?>
<?php else: ?>
Please select a Product, category, or type
<?php endif ?>
try this,
<?php if($current_type){ ?>
<?php echo " Manage Product Type ";
echo "Menu Name:". $current_type["product_type"]."<br>";?>
Edit Subject;
<?php } else if($current_category){ ?>
<h1> Manage Product category </h1>
<?php echo "Menu Name:". $current_category["category_name"];?>
<?php } else if ($current_product){?>
<h1> Manage Products </h1>
<?php echo "Menu Name:". $current_product["product_names"];?>
<?php } else {?>
Please select a Product, category, or type
<?php }?>
Use this rationale
<?php if(1 == 1) { ?>
<?php echo "xxxxx"; ?>
<?php } elseif(1 == 2) { ?>
<?php echo "xxxxx"; ?>
<?php } elseif (1 == 3){ ?>
<?php echo "xxxxx"?>
<?php } else { ?>
<?php echo "xxxxx"?>
<?php } ?>

PHP echo as condition inside PHP if statement

I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).

Magento static Blocks is not displaying properly.

I have created few static blocks just based on the category Id trying to display different blocks .
The problem being some times the block is displayed while some other times it is not .
I guess there is prob with code ? or the way magneto displays static blocks not sure ?
CODE:
<?php
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==14): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==15): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==16): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==18): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==19): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==86): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==25): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==13): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==98): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_fbyz')->toHtml();?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_others')->toHtml();?>
<?php endif; ?>
</div>
<?php endif; ?>
please resolve my problem.
If you put this code in product view page then it it not properly way to get current category in product page..
Used Mage::registry("current_category") instead of Mage::getModel('catalog/layer')->getCurrentCategory();
If you come to category directly then you can get current category id product page.then you used to fetch current product categories ids.
$categoryIds = Mage::registry('current_product')->getCategoryIds();
if(Mage::registry("current_category"))
{
$category = Mage::getModel('catalog/category')
->load(Mage::registry("current_category")->getId());
}elseif(is_array($categoryIds )){
//multiple categories id of product. that way i was taken on list category
$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
}
elseif(!is_null($categoryIds)){
$category = Mage::getModel('catalog/category')->load($categoryIds);
}
else{
//no categoies
}

Pulling Default WordPress Category Name in Widget

I am trying to add the default category name on my site to a WordPress widget. Is there a way to pull this string (not the ID) automatically? I only have one category so i thought it would be easy, but none of the following are working:
<?php bloginfo('category'); ?>
<?php get_category(); ?>
<?php get_the_category(); ?>
<?php get_category([0]); ?>
<?php get_the_category([0]); ?>
<?php get_category([0]->cat_name); ?>
<?php get_the_category([0]->cat_name); ?>
<?php echo get_category(); ?>
<?php echo get_the_category(); ?>
<?php echo get_category([0]); ?>
<?php echo get_the_category([0]); ?>
<?php echo get_category([0]->cat_name); ?>
<?php echo get_the_category([0]->cat_name); ?>
Any help is appreciated...
UPDATE: 08/01/13 - Amal pushed me onto the right path. I ended up using the following and it worked perfectly...
<?php echo get_cat_name(get_option('default_category'));?>
Try this:
$category = get_category(get_option('default_category'));

php multiple if's but not related

I have a WP site but this is a PHP question, I have the following code:
f(!empty($c_dates1_new)) { ?>
<p class="text">Dates: From <?php echo $c_dates1_new; ?> to <?php echo $c_datee1_new; ?></p>
<?php } if(!empty($c_dates2_new)) { ?>
<p class="text">Dates 2: From <?php echo $c_dates2_new; ?> to <?php echo $c_datee2_new; ?></p>
<?php } if(!empty($c_dates3_new)) { ?>
<p class="text">Dates 3: From <?php echo $c_dates3_new; ?> to <?php echo $c_datee3_new; ?></p>
<?php } if(!empty($c_dates4_new)) { ?>
<p class="text">Dates 4: From <?php echo $c_dates4_new; ?> to <?php echo $c_datee4_new; ?></p>
<?php }
Now this only shows the content from $c_dates_1 where it should display the content from all of them.
I cannot workout what I need to put between them e.g. it's not an else, it's not an OR and it's not an AND as they are all separate checks, not related to each other.
any input is appreciated

Categories