Draknyte1
Draknyte1

Reputation: 24

Displaying an image in php using two different variables?

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";
$CardNumber[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?

Upvotes: 0

Views: 192

Answers (2)

Fad
Fad

Reputation: 9858

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 . '">';

Upvotes: 0

deceze
deceze

Reputation: 522081

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);

Upvotes: 4

Related Questions