PHP - Remove a child with a specific attribute selected - php

I’m trying to figure out how to remove a child with a specific attribute selected (filename). I have PHP displaying an XML file on a page that loads images. If someone where to click on any image or hit the delete button -
<img src="' . $child['src'] . '" alt="gallery image" />Delete me
it would know where to find in the XML file and delete it.
Here what loads and displays my XML file:
<?php
$xml = simplexml_load_file('myPhotos.xml');
//echo $xml->getName() . "<br />";
foreach ($xml->children() as $child)
{
echo '<img src="' . $child['src'] . '" alt="gallery image" />';
}
?>
Here is my XML structure:
<slideshow>
<image src="myPhotosResized/image1.jpg" desc="This is the 1st image!" />
<image src="myPhotosResized/image2.jpg" desc="This is the 2nd image!" />
<image src="myPhotosResized/image3.jpg" desc="This is the 3rd image!" />
<image src="myPhotosResized/image4.jpg" desc="This is the 4th image!" />
</slideshow>

Assuming that you want to actually delete the file (and not just remove it from the DOM), either use Ajax or wrap your images with a link that is targeting a script to delete the file:
With Ajax:
<?
$xml = simplexml_load_file('myPhotos.xml');
//echo $xml->getName() . "<br />";
foreach ($xml->children() as $child)
{
echo '<img src="' . $child['src'] . '" alt="gallery image" onclick="deleteFile('.$child["src"].')" />';
}
?>
<script>
function deleteFile(filename) {
var t = this;
$.ajax({
url: 'deleteFile.php?filename='+filename,
type: 'post',
success: function(data) {
t.style.display = 'none';//hide the link
alert('File deleted!');
}
});
}
</script>
And on your deleteFile.php you will need to use:
$filename = $_GET['filename'];//gets the file name from the URL
unlink($filename);//deletes the file
Second option:
If you must keep everything with PHP and not use jQuery or Ajax, you can simply wrap the images with a link and set its href to:
echo '<img src="' . $child['src'] . '" alt="gallery image" />';
Hope this helps

The key is to write your HTML properly and then write your jQuery to do the magic
Assuming your HTML is like this, give your elements class names
<img src="' . $child['src'] . '" class="deleteme" alt="gallery image" />Delete me
This jQuery code will do what you want. place it at the end of your file
<script>
$('.clicktodelete').click(function () {
$(this).prev('.deleteme').hide();
});
</script>

Related

Correct PHP Syntax

I need to add this HTML tag:
<img src = "<?php echo esc_url( get_avatar_url( $post->post_author ) ); ?>" />
Inside PHP:
echo '<h3 class="nome-vendor-cat">',
$current_cat->name,
" di ",
'<a href="' . $vendor_link->get_shop_url() . '" class="nome-vendor">',
get_usermeta( $post->post_author, 'dokan_store_name' ),
'</span>',
'</h3>';
I have to insert that image in PHP, but it always generates syntax errors.
Based on:
<img src = "<?php echo esc_url(get_avatar_url($post->post_author));?>" />
Detect the PHP part:
esc_url(get_avatar_url($post->post_author))
Detect the HTML part:
<img src = "
//and
" />
Write it in PHP:
echo '<img src = "'.esc_url(get_avatar_url($post->post_author)).'" />';
And you should better use . for concating things.
Its ok to use , when you echo stuff.
But if you replace echo with an assignment $var = , you have to replace all , with . to get it work again.
So always use ..

issue when i try to show image on php

i have only a small question on php also i want show an image on slider also i get error on data-thumb" " because the cod on it it's not good writen , can you check it please
echo '<img data-thumb="<img src="'.JURI::root().$image->file_url .'" />"
src="'.JURI::root().$image->file_url .'"
alt="'. $image->file_title .'" >';
also i get this result :
<img data-thumb="<img src=" https:="" website.com="" images=""
stories="" virtuemart="" product="" product-03.jpg"="">"
src="https://www.website.com/images/stories/virtuemart/product/product-03.jpg"
alt="VM Cart Logo " /> </div>
how can i fix it please ?
Your output isn't producing valid HTML. Try this.
echo '<img
src="'.JURI::root().$image->file_url.'"
alt="'.$image->file_title.'"
data-thumb="'.JURI::root().$image->file_url.'">';
Update
thanks also there is only a small issue that it must be an img on the
data-thumb to print like this result : ' src="sitecom.com/pic.png" alt="photo by
Barn Images"> how can i add my image source
src="'.JURI::root().$image->file_url.'" into the data-thumb like here
data-thumb='' ?
Can I ask why you want to embed an <img> tag in the data-thumb attribute? It can be done but usually, you'd just construct the <img> later when you actually use the contents of data-thumb. Anyhoo, I'll update my answer to match what you want.
This is valid HTML according to https://validator.w3.org/nu/.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My Title</title>
</head>
<body>
<?php
$url = 'https://via.placeholder.com/350x150';
$title = 'This is my image title';
echo '<img
src="' . $url . '"
alt="' . $title . '"
data-thumb=\'<img src="' . $url . '">\'>';
?>
</body>
</html>
Using your variables, it would be.
$url = JURI::root().$image->file_url;
$title = $image->file_title;
echo '<img
src="' . $url . '"
alt="' . $title . '"
data-thumb=\'<img src="' . $url . '">\'>';

How to add "../" into a PHP echo img src

In my code I show some images in this way:
...
<?php echo '<img src="' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="' .$imagetwo. '" alt="" /> '?></a></div>
...
where $imageone, $imagetwo have path from Mysql database
e.g.
$imagesone = "images/exposition/02-05-2017-11-28-00-foto 2_b.JPG";
This code is working in the same place where I have my images folder, but now I need to put the same code in a subfolder page and I'd like to use the same image.
So, I need to put dinamically "../" before my varible, something like this:
...
<?php echo '<img src="../' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="../' .$imagetwo. '" alt="" /> '?></a></div>
...
but it's not working.
Any suggestion?
EDIT This is my solution:
if ($images1 != ""){
$imageone = "../".$images1;
}
if ($images2 != ""){
$imagetwo = "../".$images2;
}
in this way I fixed my code and it's working!
Since you're using double quotation marks after src, you can just write down <img src="../$imageone" alt="" />
There's no need to use simple quotation marks to write the variable because the double quotation marks gets the variable number.

How to make a clickable image that carries a variable to another page in php?

My question is pretty simple. I tried doing some research into this but I never got it working with images.
I want to have a clickable image so when a user clicks on that image, it carries over some type of variable (ID) to another php page, so I can know which image that user clicked on and give the result.
My current code is this:
echo '<img src=' . $covers->src . 'height="300" width="190" value=' . $test . ' name="view" />';
I tried doing this on my second php page:
$var_value = $_GET['view];
echo $var_value;
but I get undefined error on line 1.
You try to fetch a parameter called view with $_GET. Simply add a parameter to your link called view and then its value.
echo '<img src="' . $covers->src . '" height="300" width="190" name="view" />';
In this example, using
echo $_GET['view'];
when at viewer.php will print "theValueIWantToBringAlong".
If your value is is stored in a variable, then do like this:
$myValue = 'theValueIWantToBringAlong';
echo '<img src="' . $covers->src . '" height="300" width="190" name="view" />';
Why not simply:
echo '<a href="viewer.php?view="' . $test . '">';
echo '<img src="' . $covers->src . '" height="300" width="190"/></a>';
Note also that you did not provide quotes around the src value of the image and
that you had no space before height.
echo "<a href=viewer.php?view={$test}><img src='{$covers->src}' height=300 width=190 name=view /></a>";
You want to put the values in the href of the <a> tag, not within the attributes of the <img>.
Change:
echo '<img src=' . $covers->src . 'height="300" width="190" value=' . $test . ' name="view" />';
to:
echo '<img src=' . $covers->src . 'height="300" width="190"/>';
An <a> isn't like a <form>. It can't pass values from elements inside of it to the link. The only way to do this with an <a> is to put the values within the href. The key/value pairs after the ? are then accessible in $_GET.
value and name are not valid attributes for img, see w3schools.com.

How to delete a particular XML child node based on their value

I have an XML structure like this:
<document>
<sliderimage>
<images>click.bmp</images>
<images>error_inotherpluginupload.JPG</images>
<images>dddd.jpg</images>
<images>Sunset123.jpg</images>
<images>Water lilies.jpg</images>
</sliderimage>
</document>
And I'm showing these images like this:
Code for fetching images you can see here :
<?php
$usernmeforxml = $_SESSION['username'];
$xmlpath = SITE_URL . "xml/" . $usernmeforxml . "/test.xml";
$xml = simplexml_load_string(file_get_contents($xmlpath));
$sliderimagesinner = $xml->sliderimage->images;
$imagenum = count($sliderimagesinner);
?>
</br>
<?php
for ($i = 0; $i < $imagenum; $i++)
{
?>
<a class="thumbnail" href="#thumb">
<img src="../slider_images/<?php
echo $sliderimagesinner[$i];
?>" width="120" height="70" />
<span>
<img src="../slider_images/<?php
echo $sliderimagesinner[$i];
?>" width="500" height="350" />
</span>
</a>
<?php
}
?>
Now i want to delete the image by adding a "delete" button on my images. When clicked, that image node should be deleted from the above XML file.
All things are dynamic, so it would be very helpful if anyone can help me regarding this.
$usernmeforxml=$_SESSION['username'];
$xmlpath=SITE_URL."xml/".$usernmeforxml."/test.xml";
$xml = simplexml_load_file($xmlpath);
if($_GET['remove'] == 'true') {
$myValue = $_GET['file'];
$xel_array = $xml->xpath("//images[text()='" . $myValue . "']");
//edited - forgot to double [0]
unset($xel_array[0][0]);
file_put_contents($xmlpath, $xml->asXML());
}
and a link like
remove
should do the trick.

Categories