Star Counting Code

If you're using an etcg category to hold your star fragments and stars, it can be difficult to actually maintain count of them. This code with count them automatically for you. It will also show stars you don't have any of right now.

Please note that this isn't tested with older versions of eTCG. This is coded for the version I use (1.1.0) but maaayyyy work on older versions?

The Code

Add this to your func.php OR if you have a file you include in your func.php file for custom add-ons, you can toss it in there! Just make sure it's outside of any pre-existing functions.

function show_stars( $tcg, $category ) {

  $database = new Database;
  $sanitize = new Sanitize;
  $tcg = $sanitize->for_db($tcg);
  $category = $sanitize->for_db($category);
  $altname = strtolower(str_replace(' ','',$tcg));

  $tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
  $tcgid = $tcginfo['id'];
  $cardsurl = $tcginfo['cardsurl'];
  $format = $tcginfo['format'];

  $cards = $database->get_assoc("SELECT `cards`, `format` FROM `cards` WHERE `tcg`='$tcgid' AND `category`='$category' LIMIT 1");
  if ( $cards['format'] != 'default' ) { $format = $cards['format']; }

  if ( $cards['cards'] === '' ) { $cards['cards'] = []; }

  $cards = explode(',',$cards['cards']);
  $cards = array_map(trim, $cards);


  $laststar = '';
  $currentstar = '';

  $starcount = 0;

  $couponstr = '';

  $juststarted = true;

  array_push($cards, '');

  $fragment_colors = ['bluefragment', 'orangefragment', 'pinkfragment', 'purplefragment'];
  $star_colors = ['bluestar', 'orangestar', 'pinkstar', 'purplestar', 'yellowstar'];

  $stars = [ 'fragments' => [], 'stars' => []];

  foreach($fragment_colors as $color) {
    $stars['fragments'][$color] = 0;
  }

  foreach($star_colors as $color) {
    $stars['stars'][$color] = 0;
  }


  foreach( $cards as $card ) {
    $card = trim($card);
    $currentstar = $card;
    
    if( $juststarted ) {
      $starcount = -1;
      $laststar = $card;
      $juststarted = false;
    }

    if( $laststar !== $currentstar ) {
      if(strpos($laststar, 'fragment') > -1) {
        $stars['fragments'][$laststar] = $starcount+1;
      } else {
        $stars['stars'][$laststar] = $starcount+1;
      }

      $laststar = $currentstar;
      $starcount = 0;
    } else {
      $starcount++;
    }
  }

  $fragmentstr = '';
  $starstr = '';

  foreach($stars['fragments'] as $color => $num) {
    $fragmentstr .= '<img src="'.$cardsurl.''.$color.'.'.$format.'" alt="" title="'.$color.'" /> x'.($num).' ';
  }

  foreach($stars['stars'] as $color => $num) {
    $starstr .= '<img src="'.$cardsurl.''.$color.'.'.$format.'" alt="" title="'.$color.'" /> x'.($num).' ';
  }

  echo $fragmentstr . '<br>' . $starstr;
}

Then, to call it, just add this wherever you want it to display:

<?php show_stars('Starlight', 'category name where your stars are '); ?>

And that's it!

But some of my images aren't loading >:(

As long as you have your fragments/star images wherever your cards are hosted, they should load! Just make sure you have all of the images in there, even for stars or fragments you haven't earned yet, so the snippet will display properly.