I'm listing all my tables (that I see in phpMyAdmin) names using HTML.
I would simply like to "hide" (just in the HTML) the ones which contain "XYZ" in the name of the table.
I made an attempt- but I have not been successful.
<?php foreach($tables as $table):?>
<li <?php if($table['XYZ'] == $theTable):?>class="active"<?php endif;?>>
<span class="fui-list-small-thumbnails"></span> <?php echo $table['table'];?>
</li>
<?php endforeach;?>
Sure it is possible, either include the list item in the HTML source, but hidden...
<?php
foreach($tables as $table) {
$pos = strpos($table['table'],'XYZ');
$class = $pos === false ? 'style="display:none"' : '';
$href = site_url('db/'.$theDB."/".$table['table']);
echo "<li {$class}><span class=\"fui-list-small-thumbnails\"></span> ".$table['table']."</li>";
}
?>
Or do not even include the list item in the HTML source
<?php
foreach($tables as $table) {
$pos = strpos($table['table'],'XYZ');
if ($pos === false) {
$href = site_url('db/'.$theDB."/".$table['table']);
echo "<li><span class=\"fui-list-small-thumbnails\"></span> ".$table['table']."</li>";
}
}
?>
Key function to test if a string contains another string is strpos. Also I have refactored your code so there is less switching between "view" and "controller" (where view is the HTML being echoed out and the controller is the logic) as this makes code much more readable.
http://php.net/manual/en/function.strpos.php
https://en.wikipedia.org/wiki/Model-view-controller
You can simply make the table visibility hidden like this
<li <?php if($table['XYZ'] == "XYZ"):?>style="visibility: hidden;"<?php endif;?>>
or you can change the visibility: hidden; to display:none;
I think you can achieve like this way.
<?php foreach($tables as $table):?>
$hideTable = "";
$hideTable = ($table['XYZ'] == $theTable)? "style='display:none'":'';
<li <?php echo $hideTable;?> >
<span class="fui-list-small-thumbnails"></span> <?php echo $table['table'];?>
</li>
<?php endforeach;?>
Try above code may helps you.
<?php foreach($tables as $table):?>
<li <?= (($table['XYZ'] == $theTable) ? 'style="display:none"' : "");?>
<span class="fui-list-small-thumbnails"></span> <?php echo $table['table'];?>
</li>
<?php endforeach;?>
Try to make the entry invisible, but it would be more performant if you
a) filter in the query or
b) filter before printing the HTML
Related
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).
I have a menu generated in php this way.
<?php
while($rowMenu = mysql_fetch_array($rsMenu)){
$link="category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
?>
<li><?php echo $name; ?></li>
<?php
}
?>
and now I want to add a background-color to the item of the present page.The background-color is defined in css ( .productActive )
I search for add a css class in php like I make with javascript but didnt find any solution, so I made this way
<?php
$cat=$_GET['cat']; /gets the id from the URL
while($rowMenu = mysql_fetch_array($rsMenu)){
$link="category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
?>
<li><?php echo $name; ?></li>
<?php
if($cat == $rowMenu['MenuItemID']) {
echo"<li class='productActive'>".$nome."</li>";
}
}//end of while
?>
But this way add one more item to the menu. it repeats the present li item. Is there any other way??
Thanks
while($rowMenu = mysql_fetch_array($rsMenu)){
$link="category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
if($cat == $rowMenu['MenuItemID']) {
echo"<li class='productActive'>".$name."</li>";
} else {
echo"<li>".$name."</li>";
}
}
That should work, I think
while($rowMenu = mysql_fetch_array($rsMenu)){
$link = "category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
// add active class?
$class = $cat == $rowMenu['MenuItemID']
? ' class="productActive"'
: '';
?>
<li<?= $class; ?>><?= $name; ?></li>
<?php
}//end of while
Then if the item matches, it'll add the class to the tag, otherwise it won't add anything.
Although I would prefer this:
printf('<li%s>%s</li>', $class, $link, $name);
p.s. Anchors should always be inside the <li>, not outside.
So prior to being introduced to CakePHP, I'd highlight the appropriate navigation tab according to the url with the following (rather sloppy) code I wrote (fyi absolute_url was a function I wrote to get the absolute path) :
$page = $_SERVER['PHP_SELF'];
// add all possible states for the navigation to an array
$checkNav = array(
"index" => "index",
"new" => "new",
"random" => "random",
"submit" => "submit"
);
$compareAgainst = strpos($page, $checkNav['index']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Popular</span></li>\n";
} else {
echo "<li>Popular</li>\n";
}
$compareAgainst = strpos($page, $checkNav['new']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">New</span></li>\n";
} else {
echo "<li>New</li>\n";
}
$compareAgainst = strpos($page, $checkNav['random']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Random</span></li>\n";
} else {
echo "<li>Random</li>\n";
}
$compareAgainst = strpos($page, $checkNav['submit']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">+ Submit a Link</span></li>\n";
} else {
echo "<li>+ Submit a Link</li>\n";
}
Now, I've noticed that in Cake, to determine the relative path, I can just go:
<?= $this->here; ?>
Is there a better way to do this, or should I just implement this (new) method with the old code?
You can do the following
Add this to app_helper.php if you need it in multiple pages. You feed this function with the controller and the action you want to check you want to compare against. The function compares it with the current page and return true if they match.
function isActive($controller, $actions = array())
{
foreach ($actions as $action)
{
if ($controller == $this->params['controller'] && $action == $this->params['action'])
{
return true;
}
}
return false;
}
And then generate your links like so:
<ul class="left">
<li <?php if($html->isActive('controller_name', array('index'))) { echo 'class="active"'; } ?>><?php echo $html->link('Index', '/index'); ?></li>
<li <?php if($html->isActive('controller_name', array('new'))) { echo 'class="active"'; } ?>><?php echo $html->link('New', '/new'); ?></li>
<li <?php if($html->isActive('controller_name', array('random'))) { echo 'class="active"'; } ?>><?php echo $html->link('Random', '/random'); ?></li>
<li <?php if($html->isActive('controller_name', array('submit'))) { echo 'class="active"'; } ?>><?php echo $html->link('Submit', '/submit'); ?></li>
</ul>
If the function returns true, the link will have class="active". Adapt it to your needs.
The way I've always done this is to give your body tag an id, and use css to target it. If your views are all separate then you can hard code the body id. If you are using some sort of template that adds in the header, content, footer etc., then just pass the id as a variable to the header view or wherever the body tag is (really any outer container/div that will be on every view and contain your navigation tabs). Also you will need to give your navigation tab id's to target each one.
Then just some css like this:
#homepage a#hometab,
#aboutpage a#abouttab,
#productpage a#productstab,
#contactpage a#contacttab
{
special active styling here
}
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.