1

我正在创建一个纸牌游戏,一个页面创建游戏并将每个玩家的所有纸牌插入 MySQL 数据库。c1,c2,c3... 由为您选择的随机卡存储。

因此,card1 将作为整数存储在数据库中,如 c1,其余 12 张卡片以此类推。

卡片从数据库返回到游戏页面后,会经过一个数组来组织卡片图片。然后例如通过两个数组显示它们。

            echo '<form input="index.php" method="post">';
            echo $cards[$fetch['c1']].'<input class="check" type="checkbox" name="c1c">';
            echo $cards[$fetch['c2']].'<input class="check" type="checkbox" name="c2c">';
            echo $cards[$fetch['c3']].'<input class="check" type="checkbox" name="c3c">';
            echo $cards[$fetch['c4']].'<input class="check" type="checkbox" name="c4c">';
            echo $cards[$fetch['c5']].'<input class="check" type="checkbox" name="c5c">';
            echo $cards[$fetch['c6']].'<input class="check" type="checkbox" name="c6c">';
            echo $cards[$fetch['c7']].'<input class="check" type="checkbox" name="c7c">';
            echo $cards[$fetch['c8']].'<input class="check" type="checkbox" name="c8c">';
            echo $cards[$fetch['c9']].'<input class="check" type="checkbox" name="c9c">';
            echo $cards[$fetch['c10']].'<input class="check" type="checkbox" name="c10c">';
            echo $cards[$fetch['c11']].'<input class="check" type="checkbox" name="c11c">';
            echo $cards[$fetch['c12']].'<input class="check" type="checkbox" name="c12c">';
            echo $cards[$fetch['c13']].'<input class="check" type="checkbox" name="c13c">';
            print_r($fetch);

$cards作为图片分类器。

$fetch是从数据库返回的数组。

            $query = "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13 FROM games WHERE comp = 0";
            $stmt = $db->prepare($query);
            $stmt->execute();

            $fetch = $stmt->fetch(PDO::FETCH_ASSOC);

            asort($fetch);

现在这段代码为我提供了我所需要的,print_r回报

Array ( [c9] => 3 [c5] => 6 [c10] => 8 [c11] => 13 [c13] => 15 [c3] => 16 [c2] => 18 [c1] => 19 [c4] => 24 [c8] => 40 [c12] => 44 [c7] => 45 [c6] => 47 ) 有组织的升序,但是当我显示卡片时,我无法弄清楚如何让它们按该顺序运行,因为键是数据库中的列名,而我无法在显示中要求的值。

所以我的基本问题是,我如何对卡片进行排序,以便它们通过我当前的数组将图片排序到卡片升序?

如果需要;

            $cards = array(
                    "0" => "<img src=\"cards/dead.png\"/>",
                    "1" => "<img src=\"cards/2_of_spades.png\"/>",
                    "2" => "<img src=\"cards/3_of_spades.png\"/>",
                    "3" => "<img src=\"cards/4_of_spades.png\"/>",
                    "4" => "<img src=\"cards/5_of_spades.png\"/>",
                    "5" => "<img src=\"cards/6_of_spades.png\"/>",
                    "6" => "<img src=\"cards/7_of_spades.png\"/>",
                    "7" => "<img src=\"cards/8_of_spades.png\"/>",
                    "8" => "<img src=\"cards/9_of_spades.png\"/>",
                    "9" => "<img src=\"cards/10_of_spades.png\"/>",
                    "10" => "<img src=\"cards/jack_of_spades2.png\"/>",
                    "11" => "<img src=\"cards/queen_of_spades2.png\"/>",
                    "12" => "<img src=\"cards/king_of_spades2.png\"/>",
                    "13" => "<img src=\"cards/ace_of_spades.png\"/>",

                    "14" => "<img src=\"cards/2_of_clubs.png\"/>",
                    "15" => "<img src=\"cards/3_of_clubs.png\"/>",
                    "16" => "<img src=\"cards/4_of_clubs.png\"/>",
                    "17" => "<img src=\"cards/5_of_clubs.png\"/>",
                    "18" => "<img src=\"cards/6_of_clubs.png\"/>",
                    "19" => "<img src=\"cards/7_of_clubs.png\"/>",
                    "20" => "<img src=\"cards/8_of_clubs.png\"/>",
                    "21" => "<img src=\"cards/9_of_clubs.png\"/>",
                    "22" => "<img src=\"cards/10_of_clubs.png\"/>",
                    "23" => "<img src=\"cards/jack_of_clubs2.png\"/>",
                    "24" => "<img src=\"cards/queen_of_clubs2.png\"/>",
                    "25" => "<img src=\"cards/king_of_clubs2.png\"/>",
                    "26" => "<img src=\"cards/ace_of_clubs.png\"/>",

                    "27" => "<img src=\"cards/2_of_hearts.png\"/>",
                    "28" => "<img src=\"cards/3_of_hearts.png\"/>",
                    "29" => "<img src=\"cards/4_of_hearts.png\"/>",
                    "30" => "<img src=\"cards/5_of_hearts.png\"/>",
                    "31" => "<img src=\"cards/6_of_hearts.png\"/>",
                    "32" => "<img src=\"cards/7_of_hearts.png\"/>",
                    "33" => "<img src=\"cards/8_of_hearts.png\"/>",
                    "34" => "<img src=\"cards/9_of_hearts.png\"/>",
                    "35" => "<img src=\"cards/10_of_hearts.png\"/>",
                    "36" => "<img src=\"cards/jack_of_hearts2.png\"/>",
                    "37" => "<img src=\"cards/queen_of_hearts2.png\"/>",
                    "38" => "<img src=\"cards/king_of_hearts2.png\"/>",
                    "39" => "<img src=\"cards/ace_of_hearts.png\"/>",

                    "40" => "<img src=\"cards/2_of_diamonds.png\"/>",
                    "41" => "<img src=\"cards/3_of_diamonds.png\"/>",
                    "42" => "<img src=\"cards/4_of_diamonds.png\"/>",
                    "43" => "<img src=\"cards/5_of_diamonds.png\"/>",
                    "44" => "<img src=\"cards/6_of_diamonds.png\"/>",
                    "45" => "<img src=\"cards/7_of_diamonds.png\"/>",
                    "46" => "<img src=\"cards/8_of_diamonds.png\"/>",
                    "47" => "<img src=\"cards/9_of_diamonds.png\"/>",
                    "48" => "<img src=\"cards/10_of_diamonds.png\"/>",
                    "49" => "<img src=\"cards/jack_of_diamonds2.png\"/>",
                    "50" => "<img src=\"cards/queen_of_diamonds2.png\"/>",
                    "51" => "<img src=\"cards/king_of_diamonds2.png\"/>",
                    "52" => "<img src=\"cards/ace_of_diamonds.png\"/>"
            );
4

1 回答 1

0

使用foreach( doco here ) 应该以正确的顺序返回它们。

foreach($fetch as $cardId => $card) {
    echo 'CardID: ' . $cardId . '<br>';
    echo 'Card: ' . $cards[$card] . '<br>';
}

哪里$cardId是密钥(例如'c1')和$card卡的ID 值(例如3)。

PHPFiddle 示例

于 2013-08-22T02:16:53.203 回答