the problem & context

the problem: sorting cards into different categories is a pain and takes a lot of time

the solution: have a computer do it for you!

caveats: this is a LARGE mod, and can be very much customized. this is very much based on Idolise.

this is based off of Caitlin's mod to the show_needed mod, in combination with my own log to cards function to cross reference against your etcg's collecting and keeping sections.

the mod

add this to any page you want to put the card sorter within your normal header and footers. you must be on a page that calls to your func.php

thank you to Caitlin for helping me clean up some parts of this code!

<?php

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

			$result = $database->get_assoc("SELECT `id` FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
			$tcgid = $result['id'];
				$cards = '';
				$result = $database->get_assoc("SELECT `cards` FROM `cards` WHERE `tcg`='$tcgid' AND `category`='$category' LIMIT 1");
				return $result['cards'];
		}
		function trim_value(&$value) { $value = trim($value); }
	?>

<?php

function array_flatten($array) {

  $return = array();
  foreach ($array as $key => $value) {
    if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
    else {$return[$key] = $value;}
  }
  return $return;

}

function taeyong_loml($tcg, $category, $count, $pend = 0, $low = 0)
{
  $total = array();
  if (strtolower($category) === 'collecting')
  {
    $database = new Database;
    $sanitize = new Sanitize;
    $tcg = $sanitize->for_db($tcg);
    $tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
    $tcgid = $tcginfo['id'];
    $worth = intval($count);
    $result = $database->query("SELECT * FROM `collecting` WHERE `tcg` = '$tcgid' AND `mastered` = '0' AND `worth` = '$worth' ORDER BY `sort`, `deck`");
    $cards = '';
    while ($row = mysqli_fetch_assoc($result))
    {
      if($row[cards] !== ''){
        $current = explode(', ', $row[cards]);
        if($row['count'] == count($current)){

        }
        else {
          $cards .= $row['cards'] . ', ';
          $total[$row['deck']] = $row['count'];
        }
      }
    }
    $cards = substr($cards, 0, -2);
  }
  else
  {
    $cards = get_category($tcg, $category);
  }
  $cards = explode(', ', $cards);
  $cards = array_unique($cards);
  array_walk($cards, 'trim_value');
  $deck = array();
  //Get decks
  foreach ($cards as $card)
  {
    $deck[] = substr($card, 0, -2);
  }
  $group = array_combine($cards, $deck);
  $deck = array_unique($deck);
  //Results
  $counter = 0;
  $deck_count = count($deck);
  $tyong = array();

  foreach ($deck as $check)
  {
    echo '';
    if (isset($total[$check]))
    {
      $all = $total[$check];
    }
    else
    {
      $all = $count;
    }
    $mine = array();
    $got = array_keys($group, $check);
    foreach ($got as $num)
    {
      $mine[] = $check . substr($num, -2);
    }

    $def = range(1, $all);
    $default = array();
    foreach ($def as $no)
    {
      if ($no < 10)
      {
        $default[] = $check . '0' . $no;
      }
      else
      {
        $default[] = $check . $no;
      }
    }
    $diff = array_diff($default, $mine);

    $diff = array_filter($diff, 'strlen');

    array_push($tyong, $diff);
  }

  if (!is_array($tyong)) {
    return false;
  }
  $tyong = array_flatten($tyong);
  return $tyong;
}

$hahaveryfunny = "";
function test_input($hahaveryfunny) {
  $hahaveryfunny = trim($hahaveryfunny);
  $hahaveryfunny = stripslashes($hahaveryfunny);
  $hahaveryfunny = htmlspecialchars($hahaveryfunny);
  return $hahaveryfunny;
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $hahaveryfunny = test_input($_POST["hahaveryfunny"]);
  if ($hahaveryfunny === "") {echo "gimme some logs or cards";}
  else {
    $thecards = array();
    $losers = array();
    $currency = array(); // idolise currency
    $slips = array(); // idolise coupons
    $deckvoteslips = array(); // idolise deck vote coupons
    $ncts = array(); // custom category for myself - all nct decks
    $jaewin = array(); // custom category for myself - specific decks
    $hahaveryfunny = preg_split('/\\r\\n|\\r|\\n/', $hahaveryfunny);
    foreach ($hahaveryfunny as $markhatesjohnny) {
      $losers = $markhatesjohnny;
      $checkone = strpos($markhatesjohnny, ': ');
      if ($checkone !== false) {
        $markhatesjohnny +- strlen(': ');
        $losers = substr($markhatesjohnny, $checkone);
        $losers = ltrim($losers, ': ');
      }
      $checkthree = strpos($losers, 'gem'); // searches for gems
      if ($checkthree !== false) {
        $gemcount = substr_count($losers, 'gem');
        $losers = str_replace(', gem', '', $losers);
        $losers = str_replace('gem', '', $losers);
        for ($x = 0; $x <$gemcount; $x++) {
          array_push($currency, 'gem');
        }
      }
      $checkfour = strpos($losers, 'gold'); // searches for gold
      if ($checkfour !== false) {
        $goldcount = substr_count($losers, 'gold');
        $losers = str_replace(', gold', '', $losers);
        $losers = str_replace('gold', '', $losers);
        for ($x = 0; $x <$goldcount; $x++) {
          array_push($currency, 'gold');
        }
      }

      //   }
      $losers = explode(', ', $losers);
      $thecards = array_merge_recursive($thecards, $losers);
    }

    //YA THINK YA BIG BOY THROWING THREE STACKS

    $taeyongcollect = array();
    $taeyongkeep = array();
    $taeyongcollect = taeyong_loml( 'idolise', 'collecting', '1', '1');
    $taeyongkeep = taeyong_loml( 'idolise', 'keeping', '20', '1');
    $tyongcollects = array_intersect($thecards, $taeyongcollect);
    $thecards = array_diff($thecards, $taeyongcollect);
    $tyongkeeps = array_intersect($thecards, $taeyongkeep);
    $thecards = array_diff($thecards, $taeyongkeep);

    //my array of decks containing these words to push to the Jaewin section
    $dannisBoys = array(
      'taeyong',
      'winwin',
      'hitchhiker-',
      'longflight',
      'highwaytoheaven'
    );

    foreach($thecards as $key => $value){
      if (strpos($thecards[$key], 'coupon-choice(')  !== false) { // looks for deck vote coupons
        $thecards[$key] = $value;
        array_push($deckvoteslips, $value);
        unset($thecards[$key]);
      }
      if (strpos($thecards[$key], 'coupon')  !== false) { // any other coupon
        $thecards[$key] = $value;
        array_push($slips, $value);
        unset($thecards[$key]);
      }

  foreach ($dannisBoys as $keep) {
    if(strpos($thecards[$key], $keep) !== false){ // looks for custom categories
$thecards[$key] = $value;
      array_push($jaewin, $value);
      unset($thecards[$key]);
    }
  }
      if (strpos($thecards[$key], 'nct-')  !== false) { // looks for anything with nct
        $thecards[$key] = $value;
        array_push($ncts, $value);
        unset($thecards[$key]);
      }
    }

    sort($thecards);
    sort($tyongcollects);
    sort($tyongkeeps);
    sort($currency);
    sort($slips);
    sort($deckvoteslips);
    sort($jaewin);
    sort($ncts);
    $thecards = implode(', ',$thecards);
    $tyongcollects = implode(', ',$tyongcollects);
    $tyongkeeps = implode(', ',$tyongkeeps);
    $currency = implode(', ',$currency);
    $slips = implode(', ',$slips);
    $deckvoteslips = implode(', ',$deckvoteslips);
    $jaewin = implode(', ',$jaewin);
    $ncts = implode(', ',$ncts);
    echo "<br><br>here are your trading cards: <br>";
    print_r($thecards);
    echo "<br><br>here are your collecting cards: <br>";
    print_r($tyongcollects);
    echo "<br><br>here are your keeping cards: <br>";
    print_r($tyongkeeps);
    echo "<br><br>here are your jaewin cards: <br>";
    print_r($jaewin);
    echo "<br><br>here are your nct cards: <br>";
    print_r($ncts);
    echo "<br><br>here are your coupons: <br>";
    print_r($slips);
    echo "<br><br>here are your deck vote coupons: <br>";
    print_r($deckvoteslips);
    echo "<br><br>currency: <br>";
    print_r($currency);

  } // else

} // larger if

?>

<form action="PAGENAMEHERE.php" method="post">
		Input Logs or Card List: <br><textarea class="logbox" cols="40" rows="4" name="hahaveryfunny" id="hahaveryfunny"></textarea><br>
		<input type = "submit" name = "submit" value = "Submit">
	</form><br><br>
	</div>

change the pagenamehere.php to the actual page's name!

you can enter either a list of cards OR a list of logs (minus the dates) to the sorter, and it will sort for you.

example output (i have my card sorter on multiple pages in my etcg, including on its own page):

Untitled

Untitled