Update PHP variable realtime in IF ELSE Statement(change image) - php

I have a Woocommerce website with a plugin so the costumer can add a product to
his/her wishlist by pressing a button. This button has an image with a white heart. After clicking the button the white heart has to be changed in a red heart. This works, but only after the page is reloaded. So I want to let change the image real-time. I know something like AJAX is needed. The name of the image is used by the variable $cls . This one is used in the src of the image.
I have the following code:
<?php
if(is_user_logged_in()) {
$hlink=get_permalink()."?add_to_wishlist=".$post_ID;
$a=1;
} else { $hlink='/login';$a=0; }
$is_in_wishlist = YITH_WCWL()->is_product_in_wishlist( $post_ID );
if($is_in_wishlist==1) {
$cls='red';
} else {
$cls='white';
}
?>
<a href="<?php echo $hlink;?>" <?php if($a==1){ ?> rel='nofollow' <?php } ?> data-product-id="<?php echo $post_ID ?>" data-product-type="simple" class='add_to_wishlist'><img class="botButton" id="changeheartcolor" src="//voice-overs.online/wp-content/themes/Impreza-child/images/voice-overs_online_heart-<?php echo $cls; ?>.svg">
How can I make it work so the heart (image) will change directly change without refreshing the page?

You need to use javascript to reaload the image, not a PHP question just change the src value of image by javascript, also preload it before change.

Related

custom code for how to upload product image from backend to front end like ecommerce in php

I am new to PHP. I want to know how to be able to upload a product image from the back end to the front end of a static website using PHP. Please help me. Thank you in advance.
Select image :
<?php
if(isset($_POST['Submit1']))
{
$filepath = "images/" . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath))
{
echo "<img src=".$filepath." height=400 width=300 />";
}
else
{
echo "Error !!";
}
}
?>
I have tried the code above. It is running, but the image is displaying only on the same page. However, I need the image to display on another page when I click on submit button.
You can redirect this page when image is uploaded;
header( 'Location:http://anypage.com/?filepath='.$file_path );
and you can use get method;
$filepath=$_GET['filepath']
echo "<img src=".$filepath." height=400 width=300 />";
You can NOT pass the variable from a page to another until you use Session in PHP. But that is not a good practice for product image.
You should store your $filepath in the database and fetch it every where in your application when you need that.

PHP if image name with this extension doesn't exist in the directory, then show XXX.png

I'm pretty new in PHP and what I would like to do is for PHP to check if a certain image file with a specific name exists in a specific directory, then echo the name of the file, but if it doesn't exist, then just show XXX.png.
I currently have a page (http://powerplantv2.jehzlau.net/ppm-deals) that echoes all product names from a certain attribute in Magento.
This page calls all images based on the attribute name. For example in my page there's an attribute name called "cool haan". So it automatically calls the image named "coolhaan.png". If there's an attribute name called "levis" then it will show an image named levis.png.
But I don't know how to add a condition if levis.png doesn't exist in the directory, I just want to call XXX.png.
How can I let PHP check first if the image exists that matches the certain attribute in the directory, then show attributename.png, if now, XXX.png.
Currently, below is my code:
<?php
$name ='deals';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem(); $attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); $ppmtenants =
$attribute ->getSource()->getAllOptions(false); ?>
<?php foreach ($ppmtenants as $ppmtenant): ?>
<img src="<?php echo $this->getUrl() ?>media/wysiwyg/Deals/<?php echo strtolower($ppmtenantclean); ?>.png">
<?php endforeach; ?>
The code above gets all the options from a certain attribute then displays all the images with the attribute name. What I want to do is to check for the image name first before showing it.
To simplify my question, I just want to add a placeholder image with the name XXX.png for attributes with no images yet. :D
You can use file_exists() http://php.net/manual/en/function.file-exists.php to check if the file exists and then display the image, otherwise output the placeholder image.
<?php if(file_exists(path_to_your_file)) {
// Image does exist, fetch the image
} else {
// Image doesn't exist, output xxx.png
}
?>
Both the answers posted so far are correct, but to make things easier for you, you can do something like this:
<?php foreach ($ppmtenants as $ppmtenant):
if(file_exists($this->getUrl()."media/wysiwyg/Deals/".strtolower($ppmtenantclean).".png"))
{ ?>
<img src="<?php echo $this->getUrl() ?>media/wysiwyg/Deals/<?php echo strtolower($ppmtenantclean); ?>.png">
<?php
}
else
{
echo '<img src="xxx.png" alt="No image" />';
}
?>
<?php endforeach; ?>
<?php
if (!file_exists("PATH_TO_IMAGE") {
//display xxx.png
} else {
//load the image
}

disable a href links using PHP

I have a page with a menu on for logged in users
i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links
How can I disable all the links on that page if a PHP variable = 'no'
i know i can use
if($php_var == 'no') {
//do something here
}
but I'm not sure how to disable the links?
Is there any way using CSS or Javascript to disable links?
try this
if($php_var == "no")
{
echo 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.
If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?
if($php_var === "no") {
echo "This is the text of your link.";
} else
{
echo "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work
<?php
if($php_var === "no"){
echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
}
?>
You would need to do the processing pre-output, PHP will not dynamically disable the href of an already created DOM element.
If you are producing the output of the links via PHP, you could do something like:
echo 'Link';
Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
echo 'LINK';
}
that will keep showing the link but will lead nowhere if the user is not logged in.
Or you can do :
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
//do nothing here or put a link to the login page
}
that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it.
Note that code in this answer is just a guess of your real code
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">

WordPress Conditional if_page not working

my Conditional PHP is not working and i dont know why. Can you help me?
Here the Code:
<?php
if( is_page(array('5279','4945') ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif( is_page(array('5656','5668','5672','5677','5682','5690','5735','5738','5741','5744','5749','5752')))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
But i need it in this way... (letters are the Pages ids)
For pages a,b,c,d i need a own logo
For pages e,f,g,h i need a own logo
For pages j,k,l,m i need a own logo
and for Page n,o,p,q i need a own logo
Try this code, literally just tidied it slightly and removed the quotation marks round the page numbers.
<?php
if(is_page(array(5279,4945) ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif(is_page(array(5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
Just make sure you have the right pages. To do this go to pages in the dashboard and hover over the link to the page you want, you should see it shows the id number in the url, ie post=100. This is the id for that page and the one you will need to use. Using the id means that regardless of the page name, this code will always work for you.
An extreme test would also be using the below instead of the else
elseif(!is_page(array(5279,4945,5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}

$_GET not working when trying to view php image in a lightbox style

I have something that im currently working on, however it appears that the $_GET doesn't completely work.
I have a JavaScript light box that brings up an image in a little window, this works however i can only guess that it is using the same URL over and over again.
However when i view the source for the page (and even click one of the links in the source) it will display the correct data.
But the lightbox only seems to display the first image.
This is the JavaScript
<script>
//Checkes if any key pressed. If ESC key pressed it calls the lightbox_close() function.
window.document.onkeydown = function (e)
{
if (!e){
e = event;
}
if (e.keyCode == 27){
lightbox_close();
}
}
</script>
<script>
//This script makes light and fade divs visible by setting their display properties to block.
//Also it scrolls the browser to top of the page to make sure, the popup will be on middle of the screen.
function lightbox_open(){
window.scrollTo(0,0);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
</script>
<script>
//This makes light and fade divs invisible by setting their display properties to none.
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
</script>
I wont show the CSS i dont think thats relivant (If someone wants it then ask away)
The relevant part that creates the links is this, its part of a ForEach statement all PHP
$i = 0;
foreach ($nrows as $nrow)
{
$id = $nrow['id'];
$rid = $nrow['RaidID'];
$bid = $nrow['BossID'];
$normal = $nrow['NormalKills'];
$heroic = $nrow['HeroicKills'];
$boss = substr($nrow['BossName'], 0, 3);
$p1 = $id + $bid.".php";
$image = $boss . $p1;
#echo $image;
echo $bid;
if ($oid != $rid)
{
$i = 0;
}
if ($i == 0) {
?><td style="width: 176px;"><center><b><?php echo $nrow['raid']; ?> </b></center></td> </tr><?php
$i++;
}
?><tr><td style="width: 176px;"><div align="left"><?php echo $nrow['BossName']; ?><div id="light"><img src="bossdata/template.php?boss=<?php echo $bid;?>"></a></div><div id="fade" onClick="lightbox_close();"></div>
</div>
<?php
if ($heroic == 0)
{
if ($normal > 0)
{
echo '<img src="images/whiteskull.png" align="right" alt="Normal Kill">';
}
else
{
echo '<img src="images/redx.png" align="right" alt="Not Killed">';
}
}
else
{
echo '<img src="images/redskull.png" align="right" alt="Normal Kill">';
}
?>
</td></tr><?php
$oid = $id;
}
Now this all works, and it actually displays an image with data, however no matter what link i click the boss data is always from the first one on the list.
To me this means that the data is getting through, and reaching the the right parts on image so its "Working", but all the links do the same thing and show the same data :(
*Removed last code Bulk
You have multiple div with the same ID "light" since you create them in a foreach loop.
<div id="light">
Your function lightbox_open() opens all the divs that have id "light".
document.getElementById('light').style.display='block';
That's why you always see the first lightbox. Because the others are behind the first one.
you should try something like this :
function lightbox_open(elem){
window.scrollTo(0,0);
elem.getElementByClass('light').style.display='block';
elem.getElementByClass('fade').style.display='block';
}
And change this :
<a href="#" onclick="lightbox_open();">
By this :
<a href="#" onclick="lightbox_open(this);">
And replace id by class in your div definition :
<div class="light">
$_GET is working correctly in your code.
The issue is in the way you are combining JavaScript and PHP in the second code box. First, all of your divs have the same ID: "light" which is wrong because they all IDs are meant to be unique within the HTML document. You need to identify them uniquely, for example appending the BossID to them.
After identifying each div uniquely you'll have to edit lightbox_open and lightbox_close so they can receive the BossID of the divs that you want to show and hide.

Categories