开发者

Displaying an image in php using two different variables?

开发者 https://www.devze.com 2023-03-31 18:08 出处:网络
I have my root folder, and within my root folder is a folder named images. Within the images folder is 4 subfolders, each named after a Suit of cards.

I have my root folder, and within my root folder is a folder named images. Within the images folder is 4 subfolders, each named after a Suit of cards. Within each Suit folder, I have 13 pictures named after cards. Ace.jpg, Two.jpg, etc. Within my Code, I declare each suit and card as a variable.

/*Array used to select a random number*/
$CardNumber = array();
$CardNumber[0]="Ace";
$CardNumber[1]="Two";
$CardNumber[2]="Three";
$CardNumb开发者_如何学Pythoner[3]="Four";
$CardNumber[4]="Five";
$CardNumber[5]="Six";
$CardNumber[6]="Seven";
$CardNumber[7]="Eight";
$CardNumber[8]="Nine";
$CardNumber[9]="Ten";
$CardNumber[10]="Jack";
$CardNumber[11]="Queen";
$CardNumber[12]="King";

/*Array used to select a random suit.*/
$CardSuit = array();
$CardSuit[0]="Clubs";
$CardSuit[1]="Diamonds";
$CardSuit[2]="Hearts";
$CardSuit[3]="Spades";

After a player picks a Card and a Suit, is there anyway to display the card he chose?

E.G. If You picked the 5 of Clubs, it would display the picture named Five.jpg from the Clubs folder?


As far as I understand you just need to create an image tag with the src pointing to the right file in the right directory...

$suit = 'Hearts';
$card = 'Queen';
printf('<img src="%s/%s.jpg">', $suit, $card);


I guess you could do the following if you want to pick a random card

$CNkey = array_rand($CardNumber);
$CSKey = array_rand($CardSuit);
$randomCardNumber = $CardNumber[$CNKey];
$randomCardSuit = $CardSuit[$CSKey];   
$image = '/images/' . $randomCardSuit . '/' . $randomCardNumber . '.jpg'; // or any other image extensions

echo '<img src="' . $image . '">';
0

精彩评论

暂无评论...
验证码 换一张
取 消