From a50f4c0a4b8b87c36d02d0636e58aa60a6b92b39 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 21 Jul 2024 11:35:54 +0200 Subject: optimize error messages and use name for sql select --- mtg-pdf.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'mtg-pdf.php') diff --git a/mtg-pdf.php b/mtg-pdf.php index b5e9fb2..068c91e 100644 --- a/mtg-pdf.php +++ b/mtg-pdf.php @@ -25,6 +25,10 @@ for ($idx = 0; $idx < count($matches[0]); $idx++) { ]; } +if (empty($cards)) { + die("Couldn't find any cards matching the pattern."); +} + /* * Select data from DB */ @@ -33,21 +37,32 @@ $db = new \PDO('sqlite:AllPrintings.sqlite'); $images = []; foreach ($cards as $card) { - $query = 'SELECT scryfallId,layout from cardIdentifiers JOIN cards on cardIdentifiers.uuid=cards.uuid WHERE cards.setCode=:setCode AND cards.number=:number'; + $query = <<prepare($query); - $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]); + $statement->execute(['name' => $card['name'], 'setCode' => $card['set'], 'number' => $card['number']]); $result = $statement->fetch(); $id = $result['scryfallId'] ?? ''; if (empty($id)) { - $query = 'SELECT scryfallId,layout from tokenIdentifiers JOIN tokens on tokenIdentifiers.uuid=tokens.uuid WHERE tokens.setCode=:setCode AND tokens.number=:number'; + $query = <<prepare($query); - $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]); + $statement->execute(['name' => $card['name'], 'setCode' => $card['set'], 'number' => $card['number']]); $result = $statement->fetch(); $id = $result['scryfallId'] ?? ''; if (empty($id)) { echo "$card[name] ($card[set]) $card[number] not found."; + echo php_sapi_name() == 'cli' ? "\n" : '
'; continue; } } @@ -67,6 +82,10 @@ foreach ($cards as $card) { } } +if (empty($images)) { + die('No cards to print.'); +} + /* * Build HTML */ -- cgit v1.2.3