0

I'm not sure if this is possible, but the concept is similar to using strtoupper. I am trying to set a color # value for each of the items in the array. Is there a valid PHP color function which can be applied here?

e.g. [0] = #ed7b7 [1] = #c13f0

These values are being called later on in the code using jQuery to display in a Javascript SweetAlert and I am trying to get each of the strings to appear as a different colour within the alert.

Segment of code where I am converting string to uppercase:

    // Return the product category count that belongs to the added item
if( has_term( $categories[0], 'product_cat', $product_id ) )
    echo json_encode(array(strtoupper($categories[0]) => $counts[0])); // Returned value to jQuery
if( has_term( $categories[1], 'product_cat', $product_id ) )
    echo json_encode(array(strtoupper($categories[1]) => $counts[1])); // Returned value to jQuery
}

And here is the Javascript alert where I am then trying to call the modified response with the colored variables:

    success: function (response) {
                    $.each( JSON.parse(response), function( category, count ) {
                        if( count == 4 ){                         
swal({
  type: 'success',
  title: "You've Added The 4 Minimum "+category+" Items!",
  allowOutsideClick: false,
  showCancelButton: false,
  showConfirmButton: false,
timer: 3000,
})  
Giuls
  • 570
  • 11
  • 33
  • Why not include a `'color' => 'whatever value'` per category? – Taplar Aug 29 '18 at 17:45
  • you mean just define a color outside the IF? So like... $categories[0] = $color = "#c13f0"; – Giuls Aug 29 '18 at 17:48
  • `json_encode(array(strtoupper($categories[0]) => $counts[0], 'color' => '#ED7B7'));` – Taplar Aug 29 '18 at 17:49
  • Hmm. Just tried adding that change, but didn't make any change to the displayed Javascript alert. – Giuls Aug 29 '18 at 17:53
  • That just puts the key value pair on the response object. Your javascript will have to read that off of the response and use it accordingly. – Taplar Aug 29 '18 at 17:54
  • I've tried adding that to read off the response, but doesn't seem like its being called: `success: function (response) { $.each( JSON.parse(response), function( category, count, 'color' ) {` – Giuls Aug 29 '18 at 18:00
  • Take off the `'color'` from the function, and in the function do `console.log(category, count)` and look at your console to see what you are getting, :) – Taplar Aug 29 '18 at 18:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179050/discussion-between-giuls-and-taplar). – Giuls Aug 29 '18 at 18:05

1 Answers1

0

You can generate any random color using the following function and assign to your array as color attribute.

function random_color_part() {
        return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
    }

    function random_color() {
        return random_color_part() . random_color_part() . random_color_part();
    }

if( has_term( $categories[0], 'product_cat', $product_id ) )
    echo json_encode(array(strtoupper($categories[0]) => $counts[0],'color' => random_color())); // Returned value to jQuery
if( has_term( $categories[1], 'product_cat', $product_id ) )
    echo json_encode(array(strtoupper($categories[1]) => $counts[1],'color' => random_color())); // Returned value to jQuery
}

Generating a random hex color code with PHP

akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24