I'm trying to put together a code to display links of subcategories.
I want the current active category to change style. I've put together the following code but can't seem to get it to work properly.
$object = new Mage_Catalog_Block_Navigation();
$actualCategoryId = $object->getCurrentCategory()->getId();
$actualCategory = Mage::getModel('catalog/category')->load($parentid);
$subCategories = explode(',', $actualCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$category = Mage::getModel('catalog/category')->load($subCategoryId);
if ( $category->getIsActive() )
{
{
echo '<li><a href="'.$category->getURL().'" style="text-decoration: none;'.($magentoCurrentUrl == $category->getURL() ? 'color:#fff;' : '').'" >'.$category->getName().'</a> </li>';
}
}
}
I want the above code to change the link to white if it is active. But it fails to include the color style. However, if I change $magentoCurrentUrl == $category->getURL() to $magentoCurrentUrl = $category->getURL() It will change the color style to #fff but also apply it to all links and not just the active one.
Can anyone point me in the right direction with this?
I think you'll want to use 3 equals signs:
(($magentoCurrentUrl === $category->getURL()) ? 'color:#fff;' : '')
The following test code produces the effect you are looking for. Here is the code I tested:
<!doctype html>
<head>
</head>
<body>
<?php
$var1 = "http://location.php";
$magentoCurrentUrl = "http://location.php";
$categoryName = "Testing";
echo '<li><a href="'.$var1.'" style="text-decoration: none;'.(($magentoCurrentUrl === $var1) ? 'color:#fff;' : '').'" >'.$categoryName.'</a> </li>';
?>
</body>
</html>
And here is the resulting HTML:
<!doctype html>
<head>
</head>
<body>
<li><a href="http://location.php" style="text-decoration: none;color:#fff;" >Testing</a> </li>
</body>
</html>
All I have done is insert my own variables in place of your variables and function calls. That is why I suggested you use var_dump() to make sure the returned values were actually identical for the link you are trying to highlight.
Related
These are my problems:
Note: the single queotes (') are not actually displayed
echo $variable; returns nothing
echo "$variable" returns '$variable'
returns 'stuff"; code ?'
this problem appears both in a portable version of Chrome and in Firefox
here is my code for reference:
<!DOCTYPE HTML>
<html lang = "it" >
<head>
<meta charset="utf-8">
<meta name = "keywords" content = "catcher, gioco, top, 10">
<meta name="author" content="Luca Ballarati">
<?php require 'connessione'; ?>
<title> TOP 10 </title>
</head>
<body style = "background-color: white;">
<h1>PROVA:</h1>
<?php
apri_conn();
mysqli_select_db($con,"catcher");
$sql="SELECT Username, Record FROM utenti ORDER BY Record LIMIT 10";
$result = mysqli_query($con,$sql);
chiudi_conn();
echo '<b><center><h2>TOP 10 GIOCATORI e RECORD</h2></center></b><br><br>.';
?>
<?php
$i=0;
while ($i < 11) {
$i++;
$NomeUtente = mysql_result($result,$i,"NomeUtente");
$Record = mysql_result($result,$i,"Record");
$str = "AAA"
echo "<p>AAA</p>";
}
?>
</body>
I think I'm using the correct syntax, from the comparisons I made with other posts on this site and others. Am I wrong, or is there something else at play?
Apart from you numerous errors contained within this code, you shouldn't use echo on a HTML document, you can simply close the PHP tag. For example
if( 1 == 1 ) { //True every time
?>
<p>AAA</p>
<?php } ?>
is almost the same as you would write :
if( 1 == 1 ) { //True every time
echo "<p>AAA</p>";
}
Also if you want to mix HTML with PHP code, I would advise you to use endif and endwhile for your loops.
Below is the code to display the post. That loads every 10 posts. And I want to display banner after 3 post. Can someone tell me, code to display banner after 3 post? By completing the code below. Thank you so much for your help.
<?php
$stories = Wo_GetPosts(array('limit' => 10));
if (count($stories) <= 0) {
echo Wo_LoadPage('story/no-stories');
} else {
foreach ($stories as $wo['story']) {
echo Wo_LoadPage('story/content');
}
}
?>
You are trying to use count() in a way that this function is not intended. Per the PHP documentation:
Counts all elements in an array, or something in an object.
http://php.net/manual/en/function.count.php
What you need to do is create a counter variable that increments within your foreach loop, and when it hits 3 outputs the banner. Your code might look something like this:
<?php
$stories = Wo_GetPosts(array('limit' => 10));
// No stories; output error
if (count($stories) <= 0)
echo Wo_LoadPage('story/no-stories');
// Stories exist; show them!
} else {
$count = 0;
// Loop through $stories
foreach ($stories as $wo['story']) {
// Increment the value of $count by +1
$count++;
if ($count == 3) {
// Output my Banner here
}
echo Wo_LoadPage('story/content');
}
}
?>
One thing I would note is that the above code only outputs a banner one time; when the value of $count is 3. You could adjust this to run ever 3rd story by changing the match from if ($count ==3) to if ($count % 3 == 0) which essentially reads as If the value of $count is divisible by 3.
I guess you create some HTML Code with this? If true, I'd use some Javascript for this purpose. Something like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="stories">
<div>story 1</div>
<div>story 2</div>
<div>story 3</div>
<div>story 4</div>
</div>
<script type="text/javascript">
$("#stories>div:nth-child(3)").after('<div class="banner">your bannery</div> ');
</script>
</body>
</html>
So, I'm using a mix of PHP and HTML to display the results of a text file search query. On the previous page, there's a set of forms for a license plate number, start date, and end date. In theory, if the text file contains the license plate within the listed dates, it will show what time, date, and location all of them are shown in an unordered list. The code create the list without items initially, regardless of whether or not there's any matches. If there's matches, it creates a list item for each match. If not, it creates a single list item that says no matches were found.
Currently, however, it's trying to do both if there's no matches. If there's no number matching in the database, it'll create a mostly blank line for the "match," then display the message that there was no matches.
Here's an example of the current, wrong output:
Query results for :
, : .
There are no plates found matching the query.
Here's the code that's creating this. I've looked it over for an hour now and can't figure out why it's doing that.
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8" />
<title>Query</title>
<link rel="Stylesheet" media="all" href="styles.css" />
</head>
<body>
<div class="match">
<h1>Query results for <?= $wantedplate ?>: </h1>
<ul>
<?php
$wantedplate = $_GET["plate"];
$data = fopen("stored.txt", "r");
$cline = fgets($data);
$match_check = 0;
list($cplate,$cdate,$ctime,$place) = explode(",",$cline);
while ( !feof($data) ) {
list($cplate,$csubject,$cdate,$ctime,$cplace) = explode(",",$cline);
if ($cplate == $wantedplate) {
$match_check += 1;
?>
<li> <?= $cdate ?>, <?= $ctime ?>: <?= $cplace ?>.</li>
<?php
;
$cline = fgets($data); }
else {$cline = fgets($data); }
}
if ( $match_check == 0 ) { ?>
<li> There are no plates found matching the query. </li>
<?php ; } ?>
</ul>
</div>
</body>
</html>
Any help that could be provided would be greatly appreciated. I've been staring at this for an hour now and still can't figured out why it's doing that. Thanks in advance for the assistance.
I have a sidebar menu that is created if a specific tag is found in a category. For example
Apples
Oranges
When clicked, the URL pattern would be as below:
/category/?tag=apples
The code that renders the menu is as below:
<?php if ( $term->slug == 'apples' ) {?>
<section class="item">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=apples"> Apples</a>
</h4></section>
<?php } if ( $term->slug == 'oranges') {?>
<section class="item">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=oranges"> Oranges</a>
</h4></section>
<? } ?>
<?php } } ?>
I'm having a difficult time to highlight "Apples" with a red background when that is the active URL pattern, and the same with Oranges having an orange background.
I have tried various Jquery fiddle examples that works perfectly on fiddle, but not when i implement them. Most are for static html pages which is easier to do.
This is probably something you should put in a loop, but an example for your apples section:
<section class="item <?php echo ($term->slug === $_GET('tag')) ? 'active' : '';">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=apples"> Apples</a>
</h4></section>
Note that I have added an active class to the section element so that you can style that (or its children) differently.
Why not verify the $_GET variable and then highlight the active URL ?
if ($_GET['tag'] == 'apples')
echo ''; //Highlight : ON, with CSS
else
echo ''; //Highlight : OFF
Same goes for "oranges".
Since you tagged it with jQuery, you can solve it on the client side:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(function(){
var tag = getParameterByName('tag');
if(tag){
$('h4').filter(function(){
return $(this).text().toLowerCase() === tag;
}).css({ color: 'red' });
}
});
Allthough I suggest you solve it directly on the server using PHP. Just an alternative.
I have same php to generate HTML in 2 ocasions:
Generate a link with target="_new";
Generate a link without target property.
The only way that I have to differentiate both of them is to create the parent div as different ID (eg: <div id="new"> for the 1st, '' for the 2nd.
Is there any way to check if has some #new in html and them set target?
Here's the way that I've tried so far:
<?php $new = $html->find("#new"); ?>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" <?php if (is_object($new)): ?> target="_new" <?php else : ?> <?php endif; ?> >
If you are following HTMLDOMPARSER then you can follow:
$html = file_get_html('http://www.google.com/');
$is_new_exist=false;
if($html->find('div#new'))
$is_new_exist=true;
And now you can use that flag for your checking
For further query please checkout HTMLDOMPARSER
I would presume you would be able to use something like
$new = $html->find("#new");
if ($new) {
echo something
} else {
echo something else
}
Based on the assumption you are using domdocument or a html parser.
You would not use target however but rather do something like <a href="#new" or if it were on another page <a href="somepage.php#new"