0

I am having an issue with the images.

I try to add an image id when user clicks the image and the id will be saved to DB.

When the page reload, the images that has id attribute will show the id in img tag.

The way I identify the image that has id attribute is based on the image src. However, I just found out that there are many duplicated images and my codes will add an id attribute to all the duplicated images.

I wish only to add the id attribute the the image that the user clicks.

My codes

this.id is the new id attribute for the image and it generated from DB.

//click image codes....
//assign the id to the clicked image (not all duplicated images)
this.img.attr('id',this.id);

when page reload...

 $doc = new DOMDocument();
 $doc->loadHTML($myHtml);

          $imageTags = $doc->getElementsByTagName('img');

          //get the images that has id attribute from DB
          $imgSource=$this->imgSource;    //imgSource is an array
          $imgID=$this->imgID;            //imgID is an array

          //search the htmlstring and add the id attribute to the images
          foreach($imageTags as $tag) {
            $source=$tag->getAttribute('src');

           //if the html contains the image that has id attribute..
              if(in_array($source, $imgSource)){

               $ids=array_keys($imgSource,$source);
               foreach($ids as $id){
                  $tag->setAttribute('id',$imgID[$id]);
                  $myHtml=$doc->saveHTML();
                }
              }
            }
          }

My codes above will assign the id to the images that has id stored in DB. However, it will also assign the id to all the duplicated images. I need to distinquish those duplicated images and I can ONLY do it in php in my case. This problem has driven me crazy! I would really appreciate if anyone can help me about it. Thanks so much.

1 Answer 1

1

If the problem is to distinguish duplicates, then the appropiate part to avoid them is to change the code where you add the id to the duplicate images, isn't it?

I don't completely understand how the PHP code you posted works with the ids, but I suppose $this->imgSource and $this->imgID are something like this:

$this->imgSource = array(
  [0] => 'src/image/a',
  [1] => 'src/image/b',
  [2] => 'src/image/c',
  [3] => 'src/image/a'
);
$this->imgID = array(
  [0] => 111,
  [1] => 222,
  [2] => 333,
  [3] => 444
);

So when $source is 'src/image/a' it will do something like:

$tag->setAttribute('id', 111);
$tag->setAttribute('id', 444);

If that's what you want to avoid, I'd suggest to remove the id value to prevent using it again.

$ids = array_keys($imgSource, $source);
foreach($ids as $id) {
    if(isset($imgID[$id])) {
        $tag->setAttribute('id', $imgID[$id]);
        $myHtml = $doc->saveHTML();
        unset($imgID[$id]);
        break;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much A Rodas. You do understand my question very well. I appreciate it. unset works only if the user clicks the first duplicated image. If user clicks the second or third image, it won't assign the id to them because the codes found the image source for the first image. +1
Then does this only work when the user clicks on an image which is not duplicated? I think I'm probably missing something in this sentence: "it won't assign the id to them because the codes found the image source for the first image".
yes it only works only when the image is not duplicated. My bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.