Hokay, so. Here's my embedded template, chilling:
<? $i=0; ?>
{exp:channel:entries
channel="products"
dynamic="no"
entry_id="{embed:ids}"
}
<? $i++; ?>
{exp:playa:parents
field_id="25"
limit="1"
}
<!-- product -->
{if no_parents}
<? $i--; ?>
{/if}
{/exp:playa:parents}
{if no_results}
No results!
{/if}
{/exp:channel:entries}
<? if ($i === 0 ) { echo 'No products found!'; } ?>
The logic I had for this $i variable was to get an accurate reading of whether any results have been output. "Result" in this sense refers to what gets output by Playa.
The exp_channel_entries's no_results test only gets triggered if {embed:ids} is empty or the embedded ids don't correspond to entries in the channel. If the entries method returns entries but none of the results have a parent entry, nothing gets output -- and I needed a way to determine this, and I thought "Hmm, PHP should be able to do this, right?"
The desired outcome is that 'No products found!' gets output when $i = 0 but for some reason, $i is always 0 regardless of what entries get spit out.
Oh, and before you ask: YES, PHP is indeed enabled. Example: Below, {embed:ids} = 41|78|79|80|81|87|106. When set to OUTPUT, the PHP tags just get printed in the source:
<? $i=0; ?>
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<? $i--; ?>
<? $i++; ?>
<!-- product -->
<? if ($i === 0 ) { echo 'No products found!'; } ?>
If I switch PHP parsing to INPUT the tags get processed, but $i = 0 every time.
I added an echo $i; after $i=0, $i++, and $i--. With PHP set to OUTPUT, as before, the statements just get output in the page source. With PHP set to INPUT, I get this string of values: 0 1 1 1 1 1 01
So my questions to you, StackOverflow community, is:
1) Why does PHP in OUTPUT mode just output the PHP tags without processing them?
2) How can I keep count of the number of product parents being output?
I couldn't tell you why your PHP isn't being parsed when switched to output (never seen that before), but I do think there's a simpler way to do this:
{exp:query sql="SELECT child_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}
{exp:playa:parents field_id="25" entry_id="{child_entry_id}" limit="1"}
<!-- product -->
{/exp:playa:parents}
{if no_results}<p>No products found!</p>{/if}
{/exp:query}
This query will only return IDs of entries which do indeed have parents. The only thing you'll have to do is change your passed ids embed from using pipes to commas.
You could also try this:
{exp:channel:entries channel="products" entry_id="0{exp:query sql="SELECT parent_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}|{parent_entry_id}{/exp:query}" dynamic="no"}
<!-- product -->
{if no_results}<p>No products found!</p>{/if}
{/exp:channel:entries}
I realize that this thread is old but I'm posting just in case someone else has similar problems and can't follow Derek's awesome solution due to architectural issues.
My issues were solved with -
Adding full php start tags (with php)
Remove all braces and going with the alternative syntax
foreach(items as item):
...
endforeach;
Changing permissions on the file to 755.
Changing the group owner of the file to the default web user
Deleting all template manager entries (if that doesn't work, just edit the file on template manager itself)
Hope this helps.
Related
I am trying to set a condition for php- mysql pagination so that it can change the current page "li" to "li class="active" " to mark the current selected page. I am new to php and searching for such pagination tutorial but no luck.
I have done so far what is working but not able to mark selected page. Here $id is for detecting the current page id. How can I set if condition ( or other) so that I can mark the current page item in the pagination? Thousands thanks for helping me.
<ul class="pagination">
<?php if($id > 1) {?> <li>Previous</li><?php }?>
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
<?php if($id!=$page)
//3!=4
{?>
<li>Next</li>
<?php }?>
</ul>
You could change your for loop from
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
to:
<?php
for($i=1;$i <= $page;$i++){
$class=($i==$id)? ' class="active"' : '';
echo '<li'.$class.'>'.$i.'</li>';
}
?>
If I've understood your code properly, $page represents total pages and $id represents the current page, this will set the current page number as the active class and leave the other pages without the class
Assuming that $id is the current page number, and that $page is the total number of pages, you need to highlight only one by doing the following in your loop:
if($i==$id) // highlight
else // don’t highlight
Your main errors are that you didn’t test whether $i==$id, and you didn’t have an alternative unhighlighted version.
I really think that you should simplify your code by separating your logic from the HTML. It becomes very unreadable otherwise, and very hard to manage.
I have taken the liberty of doing just that. This way you can see where the logic does the hard work, an you only need to print the results in the HTML.
<?php
$id=3; // test value
$page=20; // test value
$li=array(); // temporary array for convenience
$template='<li%s>%s</li>';
if($id>1) $li[]=sprintf($template,'',$id-1,'Previous');
for($i=1;$i<=$page;$i++) {
if($i==$id) $li[]=sprintf($template,' class="active"',$i,$i); // Current
else $li[]=sprintf($template,'',$i,$i);
}
if($id<$page) $li[]=sprintf($template,'',$id+1,'Next');
$li=implode('',$li); // convert to string for printing
?>
<ul class="pagination">
<?php print $li; ?>
</ul>
You will also see two techniques to make things easier:
I use an array as a temporary container. It is easier to add items this way and then to implode it into a string afterwards
sprintf makes it easier to define a template string, so you can manage the HTML component more easily, and fill in the gaps when the time comes.
I would like to make a custom module with a list of tags. When a tag is clicked the visitor would be navigated to a category page that would show articles with that tag.
I am not a joomla expert, I was thinking about a solution like a hyperlink like this that I would add to the tags inside the module:
href="http://mywebsite.com/index.php/itemlist/tag/tokio%20city?category=places"
Is this possible? Or how could I achieve this result?
Thanks!
This is a bit more complicated than just a query string in the URL as you also need to tweak a template.
If you want to keep it as simple as possible, I'd would recommend creating a new K2 template using template overrides and editing the category template so that it would read the query string parameters and show only articles already filtered by the category and furthermore by the tag via a query string.
That's just a brief how-to, now with a lil bit more details:
1) Create a new K2 template using template overrides.
In your template, if it doesn't exist already, create a folder structure /templates/your_template/html/com_k2/templates/default. The "default" can be replaced with any name if you want to have more K2 templates, but you have to set the new template to each category you have manually.
Now take the content from "/components/com_k2/templates/default" and copy it to the new folder in your template. Now, K2 is using the templates from your /templates/your_template/html/com_k2/ folder. Feel free to google more details if you don't understand template overrides, it's pretty important thing when customizing a template.
2) Edit your category view file to accommodate the list to your query strings
The file that interests you now is in /templates/your_template/html/com_k2/templates/default/category.php. Open this file and try to understand what's important there:
Line 141
<?php foreach($this->leading as $key=>$item): ?>
Line 169
<?php foreach($this->primary as $key=>$item): ?>
Line 197
<?php foreach($this->secondary as $key=>$item): ?>
Line 226
<?php foreach($this->links as $key=>$item): ?>
This is what matters. In these four foreach loops, there are all the items. Then, you can wrap the content of each of these loops into an if-condition to check whether it has the desired tag that is specified in the URL.
To show you an example, this is the code for <div id="itemListPrimary">. You can replace this whole div in the category.php file with the following code and it will work flawlessly. I've just written and tested it.
<div id="itemListPrimary">
<?php foreach ($this->primary as $key=>$item): ?>
<?php
# Get the value of the "tag" query string
$jInput = JFactory::getApplication()->input;
$myTag = $jInput->get('tag', null, 'STRING'); // Joomla 1.6+
//$myTag = JRequest::getVar('tag'); // for Joomla 1.5
# If the tag is empty, the query string is not specified and we'll go standard way without any tag filter
if (empty($myTag)) {
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
# Otherwise the tag is set so we'll filter the articles by the tag
} else {
# Get an array of all the tags that the current article in the loop has
$articleTags = array();
foreach ($item->tags as $tag) {
$articleTags[] = $tag->name;
}
# Check if the article has the tag specified in the URL as a query string
if (in_array($myTag, $articleTags)) {
# Now the default content of the foreach loop comes as written in the default K2 category.php template
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
}
} ?>
<?php endforeach; ?>
</div>
3) Understand how the URLs will work
My typical category URL is:
http://mywebsite.com/category-name
To show only articles with a specified tag, use:
http://mywebsite.com/category-name?tag=your-tag
For instance, if you want to show only articles with the "Tokio City" tag, use:
http://mywebsite.com/category-name?tag=Tokio City
Done.
That's the basics of what you needs. It's all you need if you use primary articles only (no leading and secondary or links). Of course there are plenty more things you might want to take care of:
a notice if there is no article with the specified tag
no redundant code, I've written it like this for the sake of simplicity and readability
SEO - spaces and special characters in URLs
making sure no empty div will be printed
But that would be way more code and I wanted to keep it simple & readable for you. I think I gave you more than enough for a start, so go ahead and get it done, good luck :)
statmentI am currently working on a project. I am having issues with making a variable ++ in the loop while nested in if statements. I really do not understand why it's doing what it's doing. Here is my code.
<?php $i=0;
while ($loop->have_posts() ) : $loop->the_post(); ?>
<?php if(get_field('featured_game') && valid_release(get_field('release_date'))):?>
<div class="item <?php if($i == 0){echo 'active';}?> row carousel-<?=$i;?>">
// do generated html
<style>
.carousel-<?=$i;?>{
// do generated css
}
</style>
</div>
<?php $i++;?>
<?php endif?>
<?php endwhile;?>
</div>
My results have
carousel-
these spots all having returned 0 despite the loop running. meaning that incrment is not happening.
if I remove the if statement the loop works, as expected. The if statement are just to verify criteria to be true. I do can place the ++ outside of the if statement I get the same results.
Could some please explain what I am missing here? This seems like such a basic problem. I really do not understand why $i will not increment when does meet the criteria and posts the correct info. Thanks in advance to anyone who takes the effort.
I ran this code:
<?php $i = 0;
while ($i < 10) : ?>
<?php if (true): ?>
<?php if ($i == 0) {
echo 'active';
} ?>
<?= $i; ?>
<?php $i++; ?>
<?php endif ?>
<?php endwhile; ?>
and the result was:
active 0 1 2 3 4 5 6 7 8 9
so either
get_field('featured_game')
or
valid_release(get_field('release_date'))
must be false.
Try
var_dump(get_field('featured_game'));
var_dump(valid_release(get_field('release_date')));
inside the loop.
Turns out, it was increment properly all along.
My issue turns out to be that bootstraps carousel cycles the the content within the the parent div not the actual div it seems. If I disable it, or look directly at the source, everything is what it is supposed to be.
I program in other languages but my job involves reading PHP and I'm trying to understand part of this view page (we use MVC).
<?
foreach ($slides as $i => $slide) {
?>
<li class="yui3-carousel-element<?=$i > 0 ? ' hidden-node' : ''?>">
<?
Why is the loop surrounded by <? and ?> ? I thought those go on either end of the entire PHP script, but instead I'm seeing them scattered throughout the whole thing.
That ternary expression seems to just be floating....it's not being echo'ed or print'ed or concatenated to anything....not to mention it weirdly seems to be comparing the number 0 to something I can't make out....it's not a string...it's an li element with no closing tag?? In php?? I'm very confused.
<? ?> are called short tags in PHP. They indicate the start and end of PHP code. The ternary is also wrapped with the short tags. That ternary is actually appending something to the class depending on whether or not the current index of $slide is greater than 0. The li is HTML and it should be closed.
<? is used as an opening tag for php-code and ?> as a closing tag. PHP can be mixed with other languages like HTML as seen in your example.
Actually it is being echo'ed, that's what the =right after the <? does. It is nothing else than a short command for echo. The other thing with the ? and : is another short form. Written out the whole thing equals if($i > 0) echo ' hidden-node'; else echo '';.
So the code adds <li>-Elements for every slide and for every slide except the first one it adds the class 'hidden-node', which most likely hides all other elements except the first one when the code gets loaded.
The <? and ?> tags are short tags saying that PHP code is inside of them. When they are closed, it goes back to simply outputting HTML.
The <?=that you see on the line is simply a shorthand for <?php echo
Personally I would have probably left the tag open at the start, then do echo on the line in the loop. What is there is the equivalent to the following:
<?php
foreach ($slides as $i => $slide) {
$hiddenElement = $i > 0 ? ' hidden-node' : '';
echo '<li class="yui3-carousel-element'.$hiddenElement.'">';
The loop is surrounded by those because it's exiting PHP code and entering HTML code. For example, you can do: <?php /*code here blah blah*/ if (...) { ?> <div>ha</div> <?php } /*more code*/ ?>. In PHP, if you don't wrap stuff with <?php ?>, then it's executed as HTML code.
That statement with <?= is a shortcut to echo'ing. It literally says <?php echo (($i > 0) ? ' hidden-node' : '' ?>.
the <? and ?> is shorthand for the php tags.
after that is clear you can see that the
<li class="yui3-carousel-element<?=$i > 0 ? ' hidden-node' : ''?>">
part is checking if $i is greater than 0 and displaying the hidden-node when it is
I have my data being output to a span currently... this is how it looks:
Now, when i remove the span and place a div there i am given this output:
This is desired, but I want to set a height to my page and have the data show up in as little as 3 columns. How would I do this? I have searched everywhere online but can't seem to find anything that shows a solution.
I did read that some use javascript for the format but i am still clueless on even this option.
My desired output would look like this:
If you know how many items you want in a column then you can seperate them out into individual divs and then float those divs to the left to get them to be next to each other.
<div style='float:left'>
//Items go here
</div>
<div style='float:left'>
//Items go here
</div>
etc.
If you figure out how many items your query returned, say using mysql_num_rows() and divide by 3 you can tell how many to put in each column.
Also be sure to clear the floats afterwards, so like this:
<div style="clear:both"></div>
Sometimes this is necessary as there will be random issues if this is not put there.
What you are describing can be solved with styling only. You have several divs that must be displayed in columns. The easiest way is floating them to the left, and setting the width for 1/3 of the parent. If you want 4 columns, set the with to 1/4 of the parent, and so on.
<div class='sqlResult' style="float:left;width:33%;">
<a href='#'>$key</a>
</div>
Also as other answers mentioned, don't use duplicated ids. Always use classes. If you need to target each div individually, give it a unique id, such as "category_1", "category_2", and so on.
This should work
<table><tr>
<?php $count=0; $total=mysqli_stmt_num_rows($sql)-1; $idxcount=0; $limit=10; while($row = mysqli_fetch_array($sql)): $key = $row['Keyword_Name']; ?>
<?php if($count == 0){ echo '<td>';} ?>
<span>
<?php echo $key; ?>
</span>
<?php if($total == $idxcount): ?>
</td>
<?php elseif($count == $limit): ?>
</td>
<?php $count=0; else: $count++; ?>
<?php endif; $idxcount++; ?>
<?php endwhile; ?>
</tr></table>