Jump to content

Checking array for a value (might be more PHP question than PW... sorry)


a-ok
 Share

Recommended Posts

Hi folks,

This may be a bit of a daft question as it may be more a PHP question than a PW question, but thought I would check.

I have some tags set up for each 'project' on my site. All good, and if I echo out

$project->project_tags

it returns 

1025|1074|1026|1027 

and obviously I can do a foreach loop on them etc. All good. However, I want to check if a tag exists then assign a colour value to it, for example:

data-color="<?php if ($project->project_tags == 1025) : ?>#ff0000<?php endif; ?>"

Is there a way to check an array for a value rather than running a foreach loop on it and doing it that way?

Thanks,

R

Link to comment
Share on other sites

I thought this might've worked:

data-color="<?php if (in_array(1025,$project->project_tags)) : ?>#ffff00<?php elseif (in_array(1029,$project->project_tags)) : ?>#6d8d24<?php elseif (in_array(1030,$project->project_tags)) : ?>#3b6e8f<?php elseif (in_array(1031,$project->project_tags)) : ?>#00bdff<?php else : ?>#00ff00<?php endif; ?>"

or even:

data-color="<?php if ($project->project_tags->has(1025)) : ?>#ffff00<?php elseif ($project->project_tags->has(1029)) : ?>#6d8d24<?php elseif ($project->project_tags->has(1030)) : ?>#3b6e8f<?php elseif ($project->project_tags->has(1031)) : ?>#00bdff<?php else : ?>#00ff00<?php endif; ?>"
Link to comment
Share on other sites

Not sure if I understodd you correctly. You are already iterating over project_tags and spit out one html tag for every element in project_tags, right?

Usualy you would store the color information somewhere to keep it dynamic, but if you want to keep it simple, create a mapping table. Something like

$colorMap = array(
  1025 => '#ffff00',
  1029 => '#6d8d24'
);

forach($project->project_tags as $tag) {
  echo '<span data-color="' . $colorMap[$tag] . '"></span>';
}
Link to comment
Share on other sites

Thanks for the help. No, I am not iterating over the project tags. I don't really want to. I just want to check them and if it contains an ID (1025, for example) then echo out a hex colour code.

Link to comment
Share on other sites

Why not iterate over them? If you need to check for all of them then it's the smartest way to do so.

$data-color = "";

$colorMap = array(
  1025 => '#ffff00',
  1029 => '#6d8d24'
);

forach($colorMap as $id => $color) {
  if($project->project_tags->has($id))
    $data-color .= $color;
}

// $data-color now consists of all the colors, where a tag was found.
  • Like 2
Link to comment
Share on other sites

Thanks for the help. So the best way is to do:

<?php $tag_colors = explode('|', $project->project_tags); ?>
  data-color="<?php if (in_array(1025,$tag_colors)) : ?>#ffff00<?php elseif (in_array(1029,$tag_colors)) : ?>#6d8d24<?php elseif (in_array(1030,$tag_colors)) : ?>#3b6e8f<?php elseif (in_array(1031,$tag_colors)) : ?>#00bdff<?php else : ?>#00ff00<?php endif; ?>"

This works, so maybe it is. Many thanks.

Link to comment
Share on other sites

"1025|1074|1026|1027" is only returned when you echo, because PW converts the object into a string only for this purpose. If you want to know if a page exists in a pageArray, you should use the pageArray methods, in this case:

if ( $project->project_tags->has(1025) )

Edit: LostKobrakai was faster :)

Link to comment
Share on other sites

  • 2 weeks later...

@diogo, I would just like to say, however, that this doesn't work:

<?php if ($page->parents->has(1025)) : ?>

But you never mentioned "parents" before now :)

Link to comment
Share on other sites

It's not directly related to parents, it's related to a pageArray, and @diogo said:

"1025|1074|1026|1027" is only returned when you echo, because PW converts the object into a string only for this purpose. If you want to know if a page exists in a pageArray, you should use the pageArray methods, in this case:

if ( $project->project_tags->has(1025) )

Edit: LostKobrakai was faster :)

So regardless I assumed parents would also work?

Link to comment
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
 Share

×
×
  • Create New...