2 pieces of similar PHP, one works one doesn't? - php

I've got a few fields on a property site, grabbing a value in English & depending on the value, translating it (if another language other than English is selected).
This piece of code works fine:
<?php if(get_post_meta($post->ID,'prop_parking',true) && $prop_parking):
$prop_parking_meta = get_post_meta($post->ID,'prop_parking',true);
if ($prop_parking_meta == 'Yes') {
$prop_parking_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
}
elseif ($prop_parking_meta == 'No') {
$prop_parking_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
} ?>
<li>
<p><?php echo PROP_PARK_CSTM;?>:</p><p> <?php _e( $prop_parking_meta ); ?></p>
</li>
<?php endif; ?>
I get back Yesin the set language, yet in this field I don't (I just see Yes or No):
<?php if(get_post_meta($post->ID,'prop_garage',true) && $prop_garage):
$prop_garage_meta = get_post_meta($post->ID,'prop_garage',true);
if ($prop_garage_meta == 'Yes') {
$prop_garage_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
}
elseif ($prop_garage_meta == 'No') {
$prop_garage_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
} ?>
<li>
<p><?php echo PROP_GARG_CSTM;?>:</p><p> <?php _e( $prop_garage_meta ); ?></p>
</li>
<?php endif; ?>
Is it something obvious I'm missing? :( Thanks!

I don't know why this issue happens sometimes in qTranslate, but there are two options to deal with it:
using the shortcode notation
$prop_garage_meta = '[:en]Yes[:es]Sí[:ru]да';
applying the_content filter
$prop_garage_meta = apply_filters(
'the_content',
'<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->'
);

Related

For loop is making 3600 database requests to load Products. PHP

Problem: One piece (That we can identify currently) is causing a clients product list to call the database over and over again (3600 times) at points when loading a longer list of products.
Code:
<?php foreach ($cats as $cat) :
if (in_array($cat->getId(), [68, 28, 27, 59, 79, 80, 119])) :
$cat_show = Mage::getModel('catalog/category')->load($cat->getId());
$children = $cat_show->getChildrenCategories($cat->getId());
$url1 = $cat_show->getURL();
$showAnyways = in_array(strtolower($cat_show->getName()), ["hats", "juniors", "accessories"]);
if ($cat_show->getShowSidebar() == 1 || $showAnyways) : ?>
<li class="current<?php if ($cat->getId() == $current_cat) { ?> active <?php } ?>">
<?php echo $cat->getName() ?>
<ul>
<?php if ($cat_show->getID() != 68 && $cat_show->getID() != 59) { ?>
<li class="current<?php if ($cat->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>"><a class="view_all" href="<?php echo $url1 ?>"><?php echo $this->__("View All"); ?></a></li>
<?php } ?>
<?php foreach ($children as $subcat) {
$subcat_show = Mage::getModel('catalog/category')->load($subcat->getId());
if ($subcat_show->getShowSidebar() == 1 || in_array($subcat_show->getID(), [84])) {
$grand_children = Mage::getModel('catalog/category')->getCategories($subcat->getId());
if ($grand_children) {
$cats_displayed = 0;
foreach ($grand_children as $grand_child) {
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$url = Mage::getModel('catalog/category')->load($grand_child_show->getId())->getURL();
?>
<li class="current<?php if ($grand_child->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>">
<?php echo $grand_child_show->getName() ?>
</li>
<?php $cats_displayed++;
}
}
}
if ($cats_displayed == 0 || !$grand_children) {
$url = Mage::getModel('catalog/category')->load($subcat->getId())->getURL();
?>
<li class="current<?php if ($subcat->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>">
<?php echo $subcat->getName() ?>
</li>
<?php }
}
} ?>
</ul>
</li>
<?php endif;?>
<?php endif;?>
<?php endforeach; ?>
Can anyone provide me with some pointers on how to make this FAR more efficient and not make so many DB calls.
Should note, I am not an amazing php developer by trade. Main language is python so I am trying to get some advice on the best way to go about fixing this given my less that great knowledge of php itself.
You should never have a database query call inside a for loop. You need to build a query at the start that will get all the data required before the for loop.
Some instant pointers I can see are:
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$url = Mage::getModel('catalog/category')->load($grand_child_show->getId())->getURL();
This is calling the database twice for no reason, you should be able to do this:
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$grand_child_show->getURL();
You should be able to drop all these 'GetModel' functions if at the start of the script you call something like:
$all_grand_children = Mage::getModel('catalog/category')->getAllCategories();
This would return a hash array which you would be able to access relevant items by doing the following inside the for loop:
$grand_children = $all_grand_children[$subcat->getId()];
This would replace
$grand_children = Mage::getModel('catalog/category')->getCategories($subcat->getId());
You should also do a initial call for all of the grand_child and cat_show objects. If you are skilled at SQL you can call just the relevant information by joining the tables in one SQL query.

Wordpress different headers on each pages

is it possible to have different headers on each page?
I'm currently using this code which works perfectly
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
<?php endif;?>
Now I tried to use this code and it doesnt work
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
else { ?> (is_page('Contact')){
echo '<img src="image5.jpg" />';
}
<?php endif;?>
Any ideas?
I know nothing about Wordpress - never used it but the above doesn't look right. Is there a reason whya more traditional style syntax is not adopted - more akin to this:
<?php
if( is_front_page() ){
echo do_shortcode('[myiamge1]');
} elseif( is_page('Contact') ){
echo '<img src="image5.jpg" />';
} else {
/*banana for scale - do something else*/
}
?>

Elseif in WordPress with three outputs (third one doesn't show up)

Can't get this to work, been trying different things - but it only shows the two first. The last one (the else) doesn't show up on the website.
Can anyone see what I'm doing wrong with this one?
Thank you! :)
<?php if ( get_post_meta($post->ID, 'ends', true) ) { ?>
<?php echo get_post_meta($post->ID, 'ends', true); ?>
<?php } elseif ( shortcode_exists( 'postexpirator' ) ) { ?>
<?php echo do_shortcode('[postexpirator]'); ?>
<?php } else { ?>
Continues
<?php } ?>
I think it's getting hung up on:
<?php } elseif ( shortcode_exists( 'postexpirator' ) ) { ?>
And checking if the shortcode exists in general? I'm not too sure how you have this setup because it's a shortcode but I think that is probably what it is.

WP Custom Fields, hide code if field is empty?

I'm using a custom field template plugin for WP. I want to hide
<li>Sqft: [squareft]</li>
if the field is empty. I've tried different codes, these are two I've tried based on suggestions:
<?php if ('squareft' !== '') { ?><li>Sqft: [squareft]</li>
<?php } ?>
And
<?PHP $squareft = ('squareft'); if ($squareft != '') { echo '<li>Sqft: [squareft]
</li>';} if (empty($squareft)) { echo " "; } ?>
I obviously don't have a clue what I'm doing, although I'm learning through trial and error. It uses shortcodes, so [squareft] is what to use to output the field data.
Any help is appreciated.
Update:
I think I've got it working, based on doing this method. Not yet gone live but it's working in my test post.
<?php
global $post;
$bathrooms = get_post_meta($post->ID, 'bathrooms', true);
if ( !empty($bathrooms) ) { echo '<li>Baths: [bathrooms]</li> | ' ; }
?>
Try this (replacement of the if statement in your first code sample):
<?php if (do_shortcode('[squareft]') != '') { ?>
<li>Sqft: [squareft]</li>
<?php } ?>
Or if you know the custom meta field's actual field name (generated by the plugin, probably some kind of prefix + squareft) you can do:
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { ?>
It would help to know the specific plugin you are using.
Using the get custom fields plugin I have used the following to hide content if the field it empty (in this case the 'info' field):
<?php $info = c2c_get_custom('Info');?>
<?php if ( $info == "" ) {echo "";} else {echo "<h2 id=comments>Notes</h2><div id=info>$info</div>" ;} ?>
Not sure if this will help but it might give someone else more ideas.
Are tring this :
<?php if ( (c2c_get_custom('image')) ) { ?>
blah blah blah php code etc...
<?php } ?>
Works perfectly. If field has something it shows, if not no show, it was leaving a broken image when empty, now its good.
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { echo "display output";?>

CakePHP: 3 levels deep associations not working properly

In my app a user has a profile and a user can post comments and posts.
When viewing a list of comments for a post I want to show the name of the person that posted the comment. I have tried the following:
<?php if ( ! empty($post['Comment']) ): ?>
<ul>
<?php foreach ($post['Comment'] as $comment): ?>
<li id="comment-<?php echo $comment['id']; ?>">
<h3><?php echo $this->Html->link($comment['User']['Profile']['firstname'] . ' ' . $comment['User']['Profile']['lastname'], array('controller'=>'profiles','action'=>'view','userName'=>$comment['User']['username'])); ?></h3>
<?php echo $comment['content']; ?>
<?php echo $comment['datetime']; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No comments...</p>
<?php endif; ?>
But I get the following error: Undefined index: User [APP/View/Posts/view.ctp, line 37]
Any ideas on how to fix the issue?
I have the following for the controller method:
function view ( $id = null, $slug = null )
{
$post = $this->Post->find('first',array('contain'=>array('Comment','User'=>array('Comment','Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));
if (!$post)
{
throw new NotFoundException('404');
}
else if($post['Post']['status'] == '0') // 0=draft 1=open 2=open
{
if($post['Post']['user_id'] == $this->Auth->user('id'))
{
$this->Session->setFlash('Your post has NOT been published yet');
}
else
{
throw new NotFoundException('404');
}
}
if (Inflector::slug($post['Post']['title']) != $slug || $slug = null)
{
$this->redirect(array('id'=>Tiny::toTiny($post['Post']['id']),'slug'=>Inflector::slug($post['Post']['title'])));
}
$this->set(compact('post'));
}
The model associations should all be correct as I can see the comments fine and see the profile info for the post itself, it's just the comments that don't show the profile info.
Thanks to all who can help.
You're setting $post['Comment'] as $comment in your foreach, whilst your user data isn't in $post['Comment']['User'] but in $post['User'], so your call with $comment['User'] won't work, since that index does not exist.
Use debug($var) in the future so you can see how your array structure looks like at any given moment.

Categories