cb2004

Applying image descriptions to images from a text field with | as seperator

Recommended Posts

So I have my image field, and I also have my image descriptions in 1 text field seperated by |, these are in the correct order. I am trying to apply the images descriptions to the images but I am having a brain fart. My start is:

foreach ($pages->get(3780)->find("imagesMain.count>0") as $p) {
	$p->setOutputFormatting(false);

	$imagesCount = count($p->imagesMain);
	$descriptions = explode("|", $p->drupalAlt);
	$descriptionsFiltered = array_filter($descriptions);
	if($imagesCount === count($descriptionsFiltered)) {
	}

	$p->save();
	$p->setOutputFormatting(true);
}

In some places the images are not present yet but the descriptions are, hence the if($imagesCount === count($descriptionsFiltered)) check.

So, in there I want to foreach on the imagesMain and apply the descriptions in order.

Cheers for any help.

Share this post


Link to post
Share on other sites

Something like

for($i = 0; $i < $imagesCount; $i++) {
    $img = $p->imagesMain->eq($i);
    $descr = $descriptionsFiltered[$i];
    $img->description = $descr;
}

?

  • Thanks 1

Share this post


Link to post
Share on other sites

Perfect. Full code:

foreach ($pages->get(3780)->find("imagesMain!=, drupalAlt!=") as $p) {
	$p->setOutputFormatting(false);

	$imagesCount = count($p->imagesMain);
	$descriptions = array_filter(explode("|", $p->drupalAlt));

	if($imagesCount === count($descriptions)) {
		for($i = 0; $i < $imagesCount; $i++) {
			$p->imagesMain->eq($i)->description = $descriptions[$i];
		}
	}

	$p->save();
	$p->setOutputFormatting(true);
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.