Is there a way to check if a subArea contains blocks before outputting markup when using the Area Splitter add-on?
Have been trying stuff like this but don't understand how to make it work sorry:
<?php
defined('C5_EXECUTE') or die("Access Denied.");
$c = Page::getCurrentPage();
$this->controller->setArea($this->area);
?>
<div class="box">
<? if (($this->controller->subArea()->getTotalBlocksInArea($c) != 0) || ($c->isEditMode())) : ?>
<div class="box-header">
<?php $this->controller->subArea(); ?>
</div>
<? endif; ?>
<? if (($this->controller->subArea()->getTotalBlocksInArea($c) != 0) || ($c->isEditMode())) : ?>
<div class="box-footer">
<?php $this->controller->subArea(); ?>
</div>
<? endif; ?>
</div>
Any pointers in the right direction would be much appreciated.
Cheers
Ben
Not sure about the "area splitter" addon... I believe that's a really old thing that has been supplanted by the native "Area Layouts" in the core (which were added way back in 5.4 I believe).
If you're talking about the native "area layouts" functionality, though, then I have some code in the free Page List Teasers addon that does this:
$aHandle = 'Main'; //<--CHANGE THIS ACCORDINGLY
$c = Page::getCurrentPage();
//Get blocks inside "Area Layouts"...
$layout_blocks = array();
$area = new Area($aHandle);
$layouts = $area->getAreaLayouts($c); //returns empty array if no layouts
foreach ($layouts as $layout) {
$maxCell = $layout->getMaxCellNumber();
for ($i=1; $i<=$maxCell; $i++) {
$cellAreaHandle = $layout->getCellAreaHandle($i);
$cellBlocks = $c->getBlocks($cellAreaHandle);
$layout_blocks = array_merge($layout_blocks, $cellBlocks);
}
}
//Get non-area-layout blocks...
$nonlayout_blocks = $c->getBlocks($aHandle); //Returns blocks in the order they're on the page
//Combine the two sets of blocks
$blocks = array_merge($layout_blocks, $nonlayout_blocks);
Hopefully you can take that code and modify it to achieve what you want (e.g calling count() on the layout_blocks array or the combined array, depending on which you want).
Related
My php knowledge is limited so sorry if answer is obvious.
I'm using the .rand function to display random images from one of 3 pages when a link is clicked. The pages are all named the same and are in different folders (pages,media,text). The problem I'm having is that I want a combination of 3 different pages/images every time but instead it comes out as the same 3 page/image combination each time.
I want this
e.g.
Page1, Media2, Text1. next...
Page3, Media2, Text3. next...
Page1, Media3, Text2.
Instead it comes out
e.g.
Page1, Media1, Text1. next...
Page3, Media3, Text3. next...
Page2, Media2, Text2.
The solution is probably simple but I can't seem to figure it out.
here is my code :
<body>
<div id="wrapper">
<div id="click">CLICK HERE></div>
<div id="content">
<?php
if (!empty($_GET['p'])) {
$pages_dir = 'pages';
$pages = scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p = $_GET['p'];
if (in_array($p.'.inc.php',$pages)) {
include($pages_dir.'/'.$p.'.inc.php');
} else {
echo 'Sorry, page not found.';
}
}
?>
</div>
<div id="content2">
<?php
if (!empty($_GET['p'])) {
$media_dir = 'media';
$media = scandir($media_dir, 0);
unset($media[0], $media[1]);
$p = $_GET['p'];
if (in_array($p.'.inc.php',$media)) {
include($media_dir.'/'.$p.'.inc.php');
} else {
echo 'Sorry, page not found.';
}
}
?>
</div>
<div id="content3">
<?php
if (!empty($_GET['p'])) {
$text_dir = 'text';
$text = scandir($text_dir, 0);
unset($text[0], $text[1]);
$p = $_GET['p'];
if (in_array($p.'.inc.php',$text)) {
include($text_dir.'/'.$p.'.inc.php');
} else {
echo 'Sorry, page not found.';
}
}
?>
</div>
</div>
</body>
you have used the function "rand()" just once , so you will have just one random number. If you want more numbers , use the function multiple times. for example :
$rnd_1 = rand(0,3);
$rnd_2 = rand(0,3);
$rnd_3 = rand(0,3);
Each of them may produce a unique number ;)
You use rand only once here:
<a href="index.php?p=<?php echo 'include-'.rand(1,3); ?>">
There are two solutions:
Don't use rand as GET parameter, just generate random numbers when $_GET['p'] is set.
Create another two GET parameters, ex. $_GET['m'] for media, $_GET['c'] for content and replace $_GET['p'] with them.
Try using shuffle(glob('*.png'));, or just shuffle(); an array of your pictures.
See:
http://php.net/manual/en/function.glob.php
and
http://php.net/manual/en/function.shuffle.php
The string inside glob can be replaced with any file extension.
Your code might look like this:
$allPics = shuffle(glob('*png')); $pic = array_slice($allPics, 0, 3);
Now $pic[0], $pic[1], and $pic[2] hold your images.
Personally, I Think You Should Use JavaScript if you're just trying to return 3 images. That code would look something like this this:
First the External picloader.js file:
//+ Jonas Raoni Soares Silva - Google
//# http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
for(var j,x,i=o.length; i; j=parseInt(Math.random()*i),x=o[--i],o[i]=o[j],o[j]=x);
return o;
}
//everything else by Jason Raymond Buckley
function E(e){
return document.getElementById(e);
}
function picLoader(clickId, outIdsArray, imageArray){
E(clickId).onclick = function(){
var oa = outIdsArray, il = oa.length, sh = shuffle(imageArray), pic = sh.slice(0, il);
for(var i in oa){
E(oa[i]).innerHTML = "<img src='"+pic[i]+"' />";
}
}
}
The code on your page should now look like:
<body>
<div id="wrapper">
<input type='button' value='CLICK HERE' id='showPics' />
<div id="content"></div>
<div id="content2"></div>
<div id="content3"></div>
</div>
<script type='text/javascript' src='picloader.js'></script>
<script type='text/javascript'>
var imageArray = ['img1.png', 'img2.png', 'img3.png', 'img4.png', 'img5.png', 'img6.jpg', 'img7.jpg'];
picLoader('showPics', ['content', 'content1', 'content2'], imageArray);
</script>
</body>
</html>
This should work for any amount of output elements, as long as the imageArray is at least as long as your array that contains output content ids.
while ($row = mysql_fetch_array($result)) {
<h3> <?php echo $row['ideaTitle']; ?> </h3>
<blockquote>
<p>
<em>
<?php echo $row['feedback']; ?>
</em>
</p>
</blockquote>
<?php } ?>
Here's my working code. I want to attempt to find when $row['ideaTitle'] is new, but I'm not sure how to do it. I thought about setting a temp variable w/ the title name, but I feel like there needs to be a simpler solution.
Using a temporary variable is pretty much the way to go, I would say -- that's what I do in this kind of situation.
And it generally translates to this kind of code :
$previousTitle = null;
while ($row = mysql_fetch_array($result)) {
if ($row['ideaTitle'] != $previousTitle) {
// do something when the current line
// is the first with the current title
}
// work with $row
// Store the current title to the temporary variable,
// to be able to do the comparison on next iteration
$previousTitle = $row['ideaTitle'];
}
There is no other way than to use a temporary variable. But instead of using a single last value string, try a count map:
if (#$seen_titles[ $row["ideaTitle"] ]++ == 0) {
print "new title";
}
This trick works, because the counting up ++ is in effect done after the comparison. It needs an # error suppression however for the redundant notices here.
Imo this is quite a simple solution. However, you could also preprocess the data and create a title => feedbacks map:
$feedbacks = array();
while (($row = mysql_fetch_array($result))) {
if(!array_key_exists($row['ideaTitle'], $feedbacks) {
$feedbacks[$row['ideaTitle']] = array();
}
$feedbacks[$row['ideaTitle']][] = $row['feedback'];
}
And then create the output:
<?php foreach($feedbacks as $title => $fbs): ?>
<h3> <?php echo $title; ?> </h3>
<?php foreach($fbs as $fb): ?>
<blockquote>
<p>
<em><?php echo $fb ?></em>
</p>
</blockquote>
<?php endforeach; ?>
<?php endforeach; ?>
Keep a track of previously encountered titles in a key value array.
$prevTitles = array();
while ($row = mysql_fetch_array($result)) {
if($prevTitles[$row['ideaTitle']] == true)
continue;
$prevTitles[$row['ideaTitle']] = true;
<h3> <?php echo $row['ideaTitle']; ?> </h3>
<blockquote>
<p>
<em>
<?php echo $row['feedback']; ?>
</em>
</p>
</blockquote>
<?php } ?>
A complete beginners question.
I have a large number of divs (>80) on a page (page2.php) and what I would like to do is open page1.php, click on a link to open page2.php and show only one of these divs depending on which link was clicked.
I have a basic working version of this by adding an if else to the divs. I've only done this on 5 of the divs so far and it works but it also seems a fairly in-eloquent way of doing things.
Page 1:
this is a link
Page 2:
<?php
$divID = $_GET['id'];
?>
<div id="r0101" <? if($divID == r0101): ?>class="show"<? else: ?>class="hidden"<? endif; ?> >
This then applies a css class to hide or show the div.
Would it be possible to have a function or whatever at the top of the page that takes the id from the url, figures out that there is a div with that id also, show it and hide all the others? This is probably an easy thing to do but it has me stumped.
Any help greatly appreciated.
Thanks.
Let alone the divs and work on css (as you relay on that to hide/show the divs).
You can generate not only markup but css stylesheet too. Use a similar one (put it at
the end of your head section). And let the browser do the work for you ;)
<style type="text/css">
div {
display: none;
}
div#<?php echo $_GET['id']; ?>:{
display: block;
}
</style>
$divs = array('r0101', 'r0102', 'r0103');
$divID = $_GET['id'];
foreach($divs as $div)
{
echo '<div id="'.$div.'" class="';
if ($div == $divID)
{
echo 'show';
}
else
{
echo 'hidden';
}
echo '">';
}
Assuming I have read the question correctly, you have a set of divs (r0101, r0102, etc.) and wish to show only one of these depending on the page you are on. The code above creates an array of these divs, loops through and creates the div. The class of the div is 'show' if the div matches the div from the page url, and 'hidden' otherwise.
First of all, you should consider a way of making your divs to be printed dynamically. Something like:
<?php
for($i = 1; $i <= 80; $i++):
?>
<div id="r<?php print $i; ?>">div contents</div>
<?php
endfor;
?>
Also, if you find a way of doing what's stated above, you can also do something like:
<?php
for($i = 1; $i <= 80; $i++):
if($i == $_GET['id']){
$class = 'show';
} else {
$class = 'hidden';
}
?>
<div id="r<?php print $i; ?>" class="<?php print $class; ?>">div contents</div>
<?php
endfor;
?>
or
<?php
for($i = 1; $i <= 80; $i++):
$class = ($i == $_GET['id']) ? 'show' : 'hidden';
?>
<div id="r<?php print $i; ?>" class="<?php print $class; ?>">div contents</div>
<?php
endfor;
?>
which is exactly the same but (using the ternary operator) spares a few lines and (some people think) it decreases readability.
If you want to make your download faster, you should output only the div you want to show. You could do something like this:
$divs = array('r0101', 'r0102', 'r0103');
$divID = $_GET['id'];
foreach($divs as $div)
{
if($div == $divID){ echo '<div> /* CONTENT HERE */ </div> };
}
Hope this helps.
Here's a PHP example of mine. Can anyone find a shorter/easier way to do this?
<? foreach($posts as $post){?>
<div class="<?=($c++%2==1)?‘odd’:NULL?>">
<?=$post?>
</div>
<? }?>
<style>
.odd{background-color:red;}
</style>
Examples in other languages would be fun to see as well.
Fundamentally - no. That's about as easy as it gets. You might rewrite it a bit shorter/cleaner, but the idea will be the same. This is how I would write it:
$c = true; // Let's not forget to initialize our variables, shall we?
foreach($posts as $post)
echo '<div'.(($c = !$c)?' class="odd"':'').">$post</div>";
If you'd like to have less in-line PHP, a great way of doing it is via JavaScript.
Using jQuery, it's simply:
<script type="text/javascript">
$('div:odd').css('background-color', 'red');
</script>
Using CSS3 you can do something like this:
div:nth-child(odd)
{
background-color: red
}
But better not use that for a few years if you actually want your users to see the color...
Smarty has it inbuilt:
{section name=rows loop=$data}
<tr class="{cycle values="odd,even"}">
<td>{$data[rows]}</td>
</tr>
{/section}
So does Django:
{% for o in some_list %}
<tr class="{% cycle 'row1' 'row2' %}">
...
</tr>
{% endfor %}
i always name my zebra rows "row0" and "row1" - this makes the code a bit simpler.
<?php // you should always use the full opening tag for compatibility
$i = 0;
foreach ($rows as $row) {
echo '<tr class="row' . ($i++ % 2) . '">...</tr>';
}
?>
Maybe a function with a static variable?
<?php
function alternate_row_color($css_class) {
static $show = true;
$show = !$show;
if ($show) {
return $css_class;
} else {
return NULL;
}
}
?>
Then to use it (using your example):
<?php foreach($posts as $post) { ?>
<div class="<?=alternate_row_color('odd')?>">
<?=$post?>
</div>
<?php } ?>
<?php $alt = true; foreach ($posts as $post): $alt = !$alt; ?>
<div<?php echo $alt ? ' class="odd"' : ''; ?>>
<!-- Content -->
</div>
<?php endforeach ?>
Would be the simplest and clearest way to do it.
You can encapsulate the logic as follows:
<?php
class ListCycler {
private $cols, $offs, $len;
// expects two or more string parameters
public function __construct() {
$this->offs = -1;
$this->len = func_num_args();
$this->cols = func_get_args();
foreach($this->cols as &$c)
$c = trim(strval($c));
}
// the object auto-increments every time it is read
public function __toString() {
$this->offs = ($this->offs+1) % $this->len;
return $this->cols[ $this->offs ];
}
}
?>
<html>
<head>
<style>
ul#posts li.odd { background-color:red; }
ul#posts li.even { background-color:white; }
</style>
</head>
<body>
<div>
<h3>Posts:</h3>
<ul id="posts"><?php
$rc = new ListCycler('odd','even');
foreach($posts as $p)
echo "<li class='$rc'>$p</li>";
?></ul>
</div>
</body>
</html>
Just for fun
Assuming you can use CSS3 selectors you can do something like
<div class="posts">
<? foreach($posts as $post){?>
<div>
<?=$post?>
</div>
<? }?>
</div>
<style>
div.posts div:odd{background-color:red;}
</style>
Even with CSS2 support and mootools (javascript library) you can substitute the style with this javascript
<script type="text/javascript">
// obviously this script line should go in a js file in a onload (or onDomReady) function
$$('div.posts div:odd').setStyle('background-color','red');
</script>
If you don't have anything but php a it you can simplify a bit yous code using an array
<? $isodd=array('','odd');
$c=0;
foreach($posts as $post){?>
<div class="<?=$isodd[$c++%2]?>">
<?=$post?>
</div>
<? }?>
It's short enough as it is, but I would probably wrap it into some helper function with a clear name. That way it's more obvious what's going on and you won't have to repeat that logic in all templates where you need it.
If you want to do it on the display end and are comfortable with or otherwise already using javascript, libraries like jQuery will often have :odd and :even selectors, which you can then hook up to adding specific style properties or hooking into CSS more generally by adding classes.
On a side noe, to alternate between two values a and b, a nice way of doing it in a loop is this:
x = a;
while ( true ) {
x = a + b - x;
}
You can also do this without addition and subtraction:
x = a ^ b ^ x;
where ^ is the XOR operation.
If you just want to alternate between 0 and 1, you can do this:
x = 0;
while ( true ) {
x = !x;
}
You could of course use x as an index of colors, CSS style classes and so on.
function row_color($cnt,$even,$odd) {
echo ($cnt%2) ? "<tr bgcolor=\"$odd\">" : "<tr bgcolor=\"$even\">";
}
How to use:
$cnt=0;
while ($row = mysql_fetch_array ($result)) {
row_color($cnt++,"e0e0e0","FFFFFF");
}
You can abuse the $GLOBAL scope to store the current selected class state, see below table_row_toggle() function. Yes, I know its dirty to abuse the $GLOBAL scope, but hey, we're here to fix problems ain't we? :)
Calling the table row toggle function in HTML:
<tr <? table_row_toggle(); ?>>
The function in PHP:
/* function to toggle row colors in tables */
function table_row_toggle() {
/* check if $trclass is defined in caller */
if(array_key_exists('trclass', $GLOBALS)) {
$trclass = $GLOBALS['trclass'];
}
/* toggle between row1 and row2 */
if(!isset($trclass) || $trclass == 'row2') {
$trclass = 'row1';
} else {
$trclass = 'row2';
}
/* set $trclass in caller */
$GLOBALS['trclass'] = $trclass;
/* write the desired class to the caller */
echo ' class="' . $trclass . '"';
}
<?php ($i%2==1) ? $bgc='#999999' : $bgc='#FFFFFF'; ?>
'<div bgcolor=" bgcolor='.$bgc.'">';
Spot on Vilx but, always go minimal for speed (page weight)
<tr class="'.(($c = !$c)?'odd':'even').'">
Been using something like this:
<?php
function cycle(&$arr) {
$arr[] = array_shift($arr);
return end($arr);
}
$oddEven = array('odd', 'even');
echo cycle($oddEven)."\n";
echo cycle($oddEven)."\n";
echo cycle($oddEven)."\n";
A simple little function that works well for me.
<?php
class alternating_rows()
{
private $cycler = true;
//------------------------------------------------------------------------------
function rowclass($row0,$row1)
{
$this->cycler = !$this->cycler;//toggle the cycler
$class=($this->cycler)?$row0:$row1;
return $class;
}// end function rowclass
//------------------------------------------------------------------------------
}//end class alternating rows
?>
<?php $tablerows= new alternating_rows();?>
<table>
<tr>
<th scope="col">Heading 1</th>
<th scope="col">Heading 2</th>
</tr>
<?php foreach ($dataset as $row){?>
<tr class="<?php echo $tablerows->rowclass("oddrow","evenrow"); ?>">
<td>some data</td>
<td>some more data</td>
</tr>
<?php } //end foreach?>
</table>
In PHP I am using this code:
function alternate($sEven = "even", $sOdd = "odd")
{
static $iCount;
return ($iCount++ & 1) ? $sOdd :$sEven;
}
for($i = 0; $i< 5; $i++)
echo alternate();
/*output:
even
odd
even
odd
even
*/
Source: http://sklueh.de/2013/11/einfache-alternierung-mit-php/
I am relatively new to The Zend framework having only been working with it probably two months at the most. I am now trying to get a list of results from a query I run, send it through the zend paginator class and display 4 results per page, however it does not seem to be recognising when it has got 4 results, I believe there is an error in my code but I can not for the life of me see it, I was hoping someone here will be able to pick up on it and help me and out and probably tell me it is something stupid that I have missed. Thanks here is the code I have written.
This the query in my model
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
$query = $db->query($sql);
while ($row = $query->fetchAll()) {
$result[] = $row;
}
return $result;
}
This is code in my controller
function preDispatch()
{
$this->_helper->layout->setLayout('latestnews');
Zend_Loader::loadClass('newsArticles');
$allNews = new newsArticles();
$this->view->allNews = $allNews->getAllnews();
//die (var_dump($this->view->allNews));
$data = $this->view->allNews;
// Instantiate the Zend Paginator and give it the Zend_Db_Select instance Argument ($selection)
$paginator = Zend_Paginator::factory($data);
// Set parameters for paginator
$paginator->setCurrentPageNumber($this->_getParam("page")); // Note: For this to work of course, your URL must be something like this: http://localhost:8888/index/index/page/1 <- meaning we are currently on page one, and pass that value into the "setCurrentPageNumber"
$paginator->setItemCountPerPage(1);
$paginator->setPageRange(4);
// Make paginator available in your views
$this->view->paginator = $paginator;
//die(var_dump($data));
}
Below is the view that builds the paginator,
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
< Previous |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?= $page; ?> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
Next >
<?php else: ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</div>
<?php endif; ?>
And finally the code the makes up my view
<div id="rightColumn">
<h3>Latest News</h3>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<?php
foreach ($item as $k => $v)
{
echo "<li>" . $v['title'] . "</li>";
}
?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?= $this->paginationControl($this->paginator, 'Sliding', '/latestnews/partial_pagination_control.phtml'); ?>
</div>
I will be greatful for any help you can give me.
Thanks
Sico
$paginator should be set to 4 as you want to show only 4 records at a time :
$paginator->setItemCountPerPage(4);
So I think the root of your problem is in your model class with the getAllNews function. The Zend_Db fetchAll function retrieves an associative array of all the records matching your query. You are returning an array of one element that is an array of all the rows retrieved by your select statement.
Try this:
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
return $db->fetchAll($sql);
}
You sould not pass values to paginator if you want it to limit your query.
There is a code will work for you
$usersTable = $this->getTable();
$registerSelect = $usersTable->select()->where('status = ?', 'OK')->order('lastLogin DESC');
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($registerSelect));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($limit);