I create a link in my store to compare list, but i want to display this button only if have products in compare list.
this is the following code i'm using to show the link:
Abrir Compadores
Probably you can do is:
<a <?php if(false) echo "style='display:none'"; ?> href=<?php echo $this->getUrl('catalog/product_compare/index'); ?>">Click</a>
in if condition of above code you can use you validation whatever condition you want to use...
use below code to solve your problem.
<a class="top-compare" style="color: #003d66;" href="<?php echo $this->getUrl('catalog/product_compare/index') ?>"><?php echo $this->__("Compare") ?></a>
<?php
$compare = Mage::helper('catalog/product_compare')->getItemCount();
if($compare > 0){
//do somthing
}else{
//do anything
}
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 am trying to implement pagination functionality to a page. I have custom post types and I have looped through and gathered all the posts for this page. I display the first post and hide the rest and I have next/previous buttons that will show the next post and hide the current post. It seems to be working, however I am not able to view all the posts - only 2 of them.
Here is my code:
<?php
$currProj = 1;
$countposts = 3;
?>
<?php if ($countposts>1) {
$prevID = $currProj - 1;
if ($prevID <= 0) {
$prevID = $countposts;
?>
<a class="btn-previous" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $prevID; ?>')" onclick="<?php $currProj=$prevID; ?>"> < Previous </a>
<?php } else {
?>
<a class="btn-previous" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $prevID; ?>')" onclick="<?php $currProj=$prevID; ?>"> < Previous </a>
<?php
//$currProj = $prevID;
}
echo $currProj; //This outputs 3 where it should be 1
$nextID = $currProj + 1;
if ($nextID > $countposts) {
$nextID = 1;
?>
<a class="btn-next" id="nextlink" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $nextID; ?>')" onclick="<?php $currProj=$nextID; ?>"> Next > </a>
<?php } else {
?>
<a class="btn-next" id="nextlink" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $nextID; ?>')" onclick="<?php $currProj=$nextID; ?>"> Next > </a>
<?php
//$currProj = $nextID;
}
} ?>
The javascript function is working correctly, the issue seems to be the $currProj variable. I think the issue is my onClick attribute in the tag - is there a better way of having an onClick event that will give my variable a different value? Or a way of checking if this link has been clicked then give the currProj the value of prevID/nextID ?
I have been stuck on this for a while now and I don't seem to be getting anywhere. Any help would be greatly appreciated.
Your code above seems to be conflating what's happening on the server side and what's happening on the client side. Anything wrapped in <?php ... ?> is going to be executed on the server and then sent to the client as HTML -- for example, this line:
<a class="btn-previous" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $prevID; ?>')" onclick="<?php $currProj=$prevID; ?>"> < Previous </a>
will end up being sent to the client with all of the PHP interpreted:
<a class="btn-previous" href="javascript:showProject(1,3)" onclick=""> < Previous </a>
The key thing here is this you're not re-running the PHP when you click -- the client is completely agnostic to the PHP ever having been there. Every time you click the Previous button, its href attribute is still javascript:showProject(1,3).
You have two options; namely, you can go back to the server and have it re-render the page whenever you click the next / previous button by including those variables as parameters to your page, e.g., get the current project from the URL and link like so:
$currProj = ( $_REQUEST['currProj'] ? $_REQUEST['currProj'] : 1 );
...
<a class="btn-previous" href="<?php echo '[your_url_here]?currProj=' . $prevID ?>">
However, it looks like you're interested in doing this without ever pinging the server again. In that case, you'll need to store, reference, and update these variables in javascript. There are probably a thousand ways to do this; the closes to what you seem to want to do would be to have your showProject function take no arguments and instead figure out what it needs to do based on the value of the current project, something like
var currProj = <?php echo $currProj; ?>; // this initializes the JS variable currProj from whatever it is in PHP when the server sends the page contents
var countposts = <?php echo $countposts; ?>; // initialize this too
var showPrevProject = function showPrevProject() {
// hide the current project using jQuery; assumes the DOM element you want to hide is given ID #project-[currProj]
$('#project-' + currProj).hide();
// Update the current project variable in JS; scope should update globally
currProj = currProj - 1;
if (currProj === 0) { currProj = countposts; }
// Now show that element
$('#project-' + currProj).show();
}
Then in your link you can use showNextProject or showPrevProject as appropriate.
More generally, though, it's probably better to include your javascript as a separate file and register event handlers to deal with this sort of thing. I would also recommend checking out jQuery, which is a powerful library that greatly simplifies accessing and manipulating DOM elements.
What about creating a HTML hidden input for storing those values?
<input type="hidden" value="<?php echo $currProj ?>" id="currProj">
So you can access it and modify it in client side with javascript or jQuery. If you want a solution server-side you can do it with ajax and POST or GET requests.
Maybe this isn't what you are looking for (modify the php attributes) and I misunderstood you. Let me know if this is your case.
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"
My WordPress options panel has a section where the user can paste their logo's URL to show up in the header. If the input is blank, I want the Blog's title to show up instead on my header. The ID of the input is "nl_logo", so I added an if statement in my header.
<?php if ("nl_logo") { ?>
<img src="<?php echo get_option('nl_logo'); ?>">
<?php } else { ?>
<h1><?php bloginfo('name'); ?></h1>
<?php } ?>
The first part of the if statement works. However, anything below else doesn't work when I have no URL saved in my input. So, if the input is empty, how do I display something else with PHP? Or is there a different and better way to do this? For example, creating a function and calling the results to display with a simple line of PHP?
Try this... also try to understand it.
Keeping with the established coding style:
<?php $nlLogo = get_option('nl_logo'); ?>
<?php if (empty($nlLogo)) { ?>
<h1><?php echo(bloginfo('name')); ?></h1>
<?php } else { ?>
<img src="<?php echo($nlLogo); ?>">
<?php } ?>
That should atleast be valid PHP now. I don't know if the functions you are using are correct, but if they are this should work.
Here is a cleaner way to do it...
<?php
$nlLogo = get_option('nl_logo');
if (empty($nlLogo)) {
echo('<h1>'.bloginfo('name').'</h1>');
} else {
echo('<img src="'.$nlLogo.'">');
}
?>
Option three because I'm feeling "teachy" using a ternary. Probably a little long for this to be the best choice, but it is another option.
<?php
$nlLogo = get_option('nl_logo');
echo(empty($nlLogo) ? '<h1>'.bloginfo('name').'</h1>' : '<img src="'.$nlLogo.'">');
?>
Note I switched the if / else because I'm using empty and it just feels cleaner to do it this way instead of using !empty()
<h1><?php bloginfo('name'); ?></h1>
Should be
<h1><?php echo bloginfo('name'); ?></h1>
You could try the empty method to test if a string is empty. In context:
if(!empty($nl_logo)) {
// stuff
} else {
// other stuff
}
This does not make sense:
if ("nl_logo")
You're basically saying if the string exists then proceed, which it does, of course.
It makes more sense to take the input string:
$input = $_POST["postedInput"];
As an example, doesn't really matter as long as you know how you got the value into $input.
Now, you can use the ternary operator to determine whether you want to use the user's input or if you want to use the default title:
$img = $input == "" ? bloginfo($input) : get_option($input);
Depending on what your functions do.. if get_options returns a string then this will work.
Anyway, don't mis PHP and HTML, it will make things complicated and you'll be locked into bad design.
Note:
Make sure to check if the input is actually received, using isset.
You are testing for string "nl_logo" to be true. That returns always true.
try changing your code like this:
<?php
$nl_logo = get_option('nl_logo');
if (!empty($nl_logo)):
?>
<img src="<?php echo $nl_logo; ?>">
<?php else: ?>
<h1><?php bloginfo('name'); ?></h1>
<?php endif; ?>
Don't close the php tags.
Whatever html you need to write, just write it as echo statements within one big php block.
This should fix your problem
I would like to set values on an url like this:
<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'¬e='$id_note'&user='$username'>
Then be able to grab then from the recieving page.
Right now all im getting is this when clicked:
http://localhost/readnotes/viewyournote.php?chapter=
I don't how you embed your link in your code, but if it is outside of <?php ?> tags, then you have to do:
<a href="http://<?php echo $data_url ?>/viewyournote.php?chapter=<?php echo $name_of_chapter ?>¬e=<?php echo $id_note ?>&user=<?php echo $username?>" >
if it is inside these tags, you can also do:
echo "<a href='http://$data_url/viewyournote.php?chapter=$name_of_chapter¬e=$id_note&user=$username?' >";
You can get these values on the recieveing page with $_GET['variable_name_here'], e.g. $_GET['chapter'].
Use Query string
$val = "yourvalue" ;
$url "http://localhost/readnotes/viewyournote.php?chapter=$val";
Now $val is passed to specified url .
There you could get it by using $_GET['chapter'] , It will give you "yourvalue"
<a href='http://$data_url/viewyournote.php?chapter=<?php echo $name_of_chapter; ?>¬e=<?php echo $id_note; ?>&user=<?php echo $username; ?>>
Replace your line with
<?php
echo "<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'¬e='$id_note' user='$username'>";
?>
On the receiving end use
<?php
$name_of_chapter = $_GET['chapter'];
...
?>