I'm brand new to PHP and don't have the first clue about what I'm doing. I found the way to embed HTML in PHP by looking around this site.
However, I can't make this work. My code is:
<?php do { ?>
<?php if ($row_rsMore['contentID'] = 35) : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php elseif ($row_rsMore['contentID'] = 37) : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php elseif ($row_rsMore['contentID'] = 38) : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php else : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php endif ?>
<?php } while ($row_rsMore = mysql_fetch_assoc($rsMore)); ?>
I've tried surrounding the number (35, 37 and 38) with single quotes, double quotes, brackets and variations on these themes.
What happens is that the 'contentTitle' for each of the statements displays correctly but each href shows the same link (that of the first 'if' ie: 'aboutus.php').
What have I got wrong?
try
if ($row_rsMore['contentID'] == 35) :
instead of single =
change all single = to ==
Related
i need your help in this case; i have module in joomla and want to cutting title with specified a limit from joomla library string.php. i change this code :
<?php if ($params->get('show_title', 1)) : ?>
<h3 itemprop="name">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<?php echo $this->escape($displayData->title); ?>
<?php else : ?>
<?php echo $this->escape($displayData->title); ?>
<?php endif; ?>
</h3>
to this code :
<?php if ($params->get('show_title', 1)) : ?>
<h3 itemprop="name">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid)); ?>" itemprop="url">
<?php
$limit =100;
if (strlen($this->item->text) > $limit) {
echo (substr($this->item->text, 0, $limit)) . " ... ";
}
else {
echo $this->escape($displayData->title); } ?></a><?php else : ?>
<?php endif; ?>
</h3>
but not work.
thanks for your attention guys.
Joomla has a built-in JHtmlString/truncate method which you can use, I've had good success using it with some of our templates and overrides.
This method would let you simplify your code and you could replace you entire last php block with something like the following
<?php
$limit =100;
echo JHTML::_('string.truncate', ($this->item->text), $limit, false, false);
?>
More about JHtmlString/truncate: https://docs.joomla.org/API16:JHtmlString/truncate
Some example code which might be helpful:
https://gist.github.com/2dpi/a540527a64f9f0093392
https://hotexamples.com/examples/-/JHtmlString/truncateComplex/php-jhtmlstring-truncatecomplex-method-examples.html
Good luck!
I'm using Magento 1.9 and struggling on one thing.
I have a CMS block in my category page but I want it to show only on the first page! So if I scroll down and move to page 2 on the same category I don't want to see that CMS block again.
I tried to put this code in the CMS block... but it ignores me
(category-accordion.accordion is the main div of the CMS block)
<script>
if (window.location.href.indexOf("?p=") >-1)
{document.getElementsByClassName('category-accordion accordion')[0].display='none';}
// ]]></script>
Any idea?
EDIT:
tried the following code on the category page:
<?php if($this->isContentMode()): ?>
<?php echo $this->getCmsBlockHtml() ?>
<?php elseif($this->isMixedMode() && (strpos($_SERVER['REQUEST_URI'], '?=p') !== true)): ?>
<?php echo $this->getCmsBlockHtml() ?>
<?php echo $this->getProductListHtml() ?>
<?php elseif($this->isMixedMode() && (strpos($_SERVER['REQUEST_URI'], '?=p') !== false)): ?>
<?php echo $this->getProductListHtml() ?>
<?php else: ?>
<?php echo $this->getProductListHtml() ?>
<?php endif; ?>
you can set condition in Category View Template catalog\category\view.phtml.
<?php
$currentPage = (int) $this->getRequest()->getParam('p', 1);
if($currentPage <= 1) {
echo $this->getCmsBlockHtml()
} ?>
try this.
<script>
if (window.location.href.indexOf("?p=") >-1)
{
jQuery('.category-accordion.accordion').hide();
}
</script>
I have a problem with my php code.
This is my code:
<?php if($this->template->params->get("blast2", 1) == 1) : ?>
<a href="<?php echo $this->template->params->get("blast2_link", ''); ?>"
class="social_blast" id="social_blast2" target="_blank">Blast site</a><?php endif; ?>
What I need to do is get the URL of the current page along with the title and post this to another site.
I think this should work fine:
<?php if($this->template->params->get("blast2", 1) == 1) : ?>
<a href="http://blast-site.com/submit?Url=<?php echo $_SERVER["REQUEST_URI"]?>&title=<?=mainframe->getPageTitle()?>&no_mobile=1"
class="social_blast" id="social_blast2" target="_blank">Blast site</a><?php endif; ?>
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).
I've picked up a project from another developer and I'm trying to finish and clean up a bit. This part of the code renders products from a database after checking for a language cookie:
<?php if (get_cookie('spanish')) : ?>
<?php if ($product['details_spanish'] != '') : ?>
<?php $details = explode(':',$product['details_spanish']); ?>
<h3>Especificaciones</h3>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Especificaciones</h3>
<?php endif ?>
<?php endif; ?>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Specifications</h3>
<?php endif; ?>
<?php endif; ?>
<ul>
<?php if ($details) : ?>
<?php foreach ($details as $detail) : ?>
<?php $detail = split(',',$detail); ?>
<?php if ($detail[0] != '' && $detail[1] != '') : ?>
<li>
<strong><?=ucwords($detail[0])?></strong> : <?=$detail[1]?>
</li>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endif; ?>
Sorry, I know this is hard to read due to inconsistency and bad coding (not my project initially. What it's doing is checking for a spanish cookie on the users machine. If it finds it, it pulls the values from the spanish column of the table instead of the regular. It falls back on the english value if there is no spanish value in the table. Also, it creates a $details variable that contains one of the values of the database, and splits it where the : is.
The problem I'm having is on some products, it gives me an error saying it can't find the $details variable, I'm assuming it is because the variable doesn't live outside the scope of the first if statement?
I'm a php noob really but even I know this isn't good practice. is there a better way to clean this up and have my $detail variable available?
Quick workaround is to pre-initialize that variable at the very top:
<?php $details = ''; ?>