Card Manipulation

Card Trick Picture

Card manipulation is the branch of magical illusion that deals with creating effects using sleight of hand techniques involving playing cards. Card manipulation is often used in magical performances, especially in close-up, parlor, and street magic. Some of the most recognized names in this field include Dai Vernon, Tony Slydini, Ed Marlo, S.W. Erdnase, Richard Turner, John Scarne and Ricky Jay. Before becoming world-famous for his escapes, Houdini billed himself as "The King of Cards". (Wikipedia)

For this example I used a card trick I learned when I was around 10 years old. I don't remember who taught it to me, but it's something I have never forgotten.

Follow the directions below to give it a try.

1) Choose a card in one of the three rows below and memorize it.
2) Select the row that contains your card.
3) Click the "GO" button.

Row One:
Row Two:
Row Three:
 

CODE FOLLOWS

<?php

    session_name
("xxxx"); // Set session name
    
session_start(); // Start session

    /**
     * Created by Casey Hargis
     * For CITW 185
     * PHP Web Development
     * Spring 2018
     * 21 Cards
     * Contains functions used in the program
     */

    
include 'includes/header.php';
    include 
'includes/functions.php';
    
error_reporting(E_ALL);

    
$suits = array("C""D""H""S");
    
$faces = array("A""2""3""4""5""6""7""8""9""10""J""Q""K");

    
// Create deck of cards by combining $suits and $faces array into $cards array
    
$cards = array();
    foreach (
$suits as $suit)
        {
        foreach (
$faces as $face)
            {
            
$cards[] = ($face $suit);
            }
        }

    
// Check to see if session variables have been created
    
$rowOne = isset($_SESSION['rowOne']) ? $_SESSION['rowOne'] : "";
    
$rowTwo = isset($_SESSION['rowTwo']) ? $_SESSION['rowTwo'] : "";
    
$rowThree = isset($_SESSION['rowThree']) ? $_SESSION['rowThree'] : "";

    
$step = isset($_POST['step']) ? $_POST['step'] : 1;
    
$step htmlentities($step);

    
// Check to see if a row has been selected
    
if (isset($_POST['row']))
        {
        
$row $_POST['row'];
        
$row htmlentities($row);
        }
    else
        {
        
$step 1;
        
$row '';
        }

    switch (
$step)
        {
        case 
'1':
            
?>
                <div>
                    <h1>Card Manipulation</h1>
                </div>
                <div id="picture">
                    <a><img src="images/cards/cardtrick2.jpg" alt="Card Trick Picture"></a>
                </div>
                <div>
                    <p>
                        Card manipulation is the branch of magical illusion that deals with creating effects using
                        sleight
                        of hand techniques involving playing cards. Card manipulation is often used in magical
                        performances,
                        especially in close-up, parlor, and street magic. Some of the most recognized names in this
                        field
                        include Dai Vernon, Tony Slydini, Ed Marlo, S.W. Erdnase, Richard Turner, John Scarne and Ricky
                        Jay. Before becoming world-famous for his escapes, Houdini billed himself as "The
                        King of Cards". (<a href="https://en.wikipedia.org/wiki/Card_manipulation" target="_blank">Wikipedia</a>)
                    </p>
                </div>
                <div>
                    <p>
                        For this example I used a card trick I learned when I was around 10 years old. I don't remember
                        who
                        taught
                        it to me, but it's something I have never forgotten.
                    </p>
                </div>

                <?php
                
// Call the splitCards function
                
list($rowOne$rowTwo$rowThree) = splitCards($cards);

                
// Directions for user
                
echo "<div><h2>Follow the directions below to give it a try.</h2></div>";
                echo 
"<p>1) Choose a card in one of the three rows below and memorize it.<br>  
             2) Select the row that contains your card.<br> 
             3) Click the \"GO\" button.</p>"
;

                
// Call the outputCards function
                
outputCards($rowOne$rowTwo$rowThree$step);
                break;

        case 
'2':
            
// Call the mergeCards function
                
$cards2 mergeCards($rowOne$rowTwo$rowThree$row);
                
// Call the dealCards function
                
list($rowOne$rowTwo$rowThree) = dealCards($cards2);

                echo 
"<div><h2>Follow the directions below for the next step.</h2></div>";
                echo 
"<p>1) Find the card you memorized in the previous step.<br> 
                 2) Select the row that contains your card.<br>  
                 3) Click the \"GO\" button.</p>"
;

                
// Call the outputCards function
                
outputCards($rowOne$rowTwo$rowThree$step);
                break;

        case 
'3':
            
// Call the mergeCards function
                
$cards2 mergeCards($rowOne$rowTwo$rowThree$row);
                
// Call the dealCards function
                
list($rowOne$rowTwo$rowThree) = dealCards($cards2);

                echo 
"<div><h2>Follow the directions below to reveal your card.</h2></div>";
                echo 
"<p>1) Find the card you memorized in the first step.<br> 
                 2) Select the row that contains your card.<br>  
                 3) Click the \"GO\" button.</p>"
;

                
// Call the outputCards function
                
outputCards($rowOne$rowTwo$rowThree$step);
                break;

        case 
'4':
            
// Call the mergeCards function
                
$cards2 mergeCards($rowOne$rowTwo$rowThree$row);
                echo 
"<h2>Here is your card:</h2>";
                
// Get the users card
                
$card $cards2[10];

                
// Display the users card
                
echo "<img src='images/cards/cards_large/$card.png' alt=\"Picture\">";
                echo 
'<br>';

                if (
$card == 'AH')
                    {
                    echo 
'Hi Mom!';
                    }
            
?>
                <form action="cards.php" method="POST">
                    <p>
                        <input type="hidden" name="step" value="1">
                        <input class="button" type="submit" name="submit" value="Try Another">
                    </p>
                </form>
                <?php
                
break;
        }

    include 
'includes/footer.php';


    
//------------------- functions --------------------------
    /**
     * @param $cards
     * @return array
     * Shuffle and split the $cards array into three separate arrays containing 7 cards each
     */

    
function splitCards($cards)
        {
        
shuffle($cards);// Shuffle $cards array
        
$rowOne array_slice($cards07); // $rowOne array = cards 0 through 6
        
$rowTwo array_slice($cards77); // $rowTwo array = cards 7 through 13
        
$rowThree array_slice($cards147); // $rowThree array = cards 14 through 20
        //Call saveCards function
        
saveCards($rowOne$rowTwo$rowThree);
        return array(
$rowOne$rowTwo$rowThree);
        }

    
/**
     * @param $rowOne
     * @param $rowTwo
     * @param $rowThree
     * @param $row
     * @return array
     * Merge row arrays together depending on user input
     */

    
function mergeCards($rowOne$rowTwo$rowThree$row)
        {
        
$cards2 = array();
        switch (
$row)
            {
            case 
'one'// If user selects row 1, row 1 goes to middle of the merged array
                    
$cards2 array_merge($rowTwo$rowOne$rowThree);
                    break;

            case 
'two':// If user selects row 2, row 2 goes to middle of the merged array
                    
$cards2 array_merge($rowThree$rowTwo$rowOne);
                    break;

            case 
'three':// If user selects row 3, row 3 goes to middle of the merged array
                    
$cards2 array_merge($rowTwo$rowThree$rowOne);
                    break;
            }
        return 
$cards2;
        }

    
/**
     * @param $cards2
     * @return array
     * Deal cards to 3 separate arrays containing 7 cards each
     */

    
function dealCards($cards2)
        {
        
$cards3 $cards2;
        
$cards4 $cards2;

        unset(
$cards2[1], $cards2[2], $cards2[4], $cards2[5], $cards2[7], $cards2[8], $cards2[10], $cards2[11], $cards2[13],
                
$cards2[14], $cards2[16], $cards2[17], $cards2[19], $cards2[20]);
        
$rowOne array_merge($cards2);
        unset(
$cards3[0], $cards3[2], $cards3[3], $cards3[5], $cards3[6], $cards3[8], $cards3[9], $cards3[11], $cards3[12],
                
$cards3[14], $cards3[15], $cards3[17], $cards3[18], $cards3[20]);
        
$rowTwo array_merge($cards3);
        unset(
$cards4[0], $cards4[1], $cards4[3], $cards4[4], $cards4[6], $cards4[7], $cards4[9], $cards4[10], $cards4[12],
                
$cards4[13], $cards4[15], $cards4[16], $cards4[18], $cards4[19]);
        
$rowThree array_merge($cards4);
        
//Call saveCards function
        
saveCards($rowOne$rowTwo$rowThree);
        return array(
$rowOne$rowTwo$rowThree);
        }

    
/**
     * @param $rowOne
     * @param $rowTwo
     * @param $rowThree
     * Saves row arrays to session
     */

    
function saveCards($rowOne$rowTwo$rowThree)
        {
        
$_SESSION["rowOne"] = $rowOne;
        
$_SESSION["rowTwo"] = $rowTwo;
        
$_SESSION["rowThree"] = $rowThree;
        }

    
/**
     * @param $rowOne
     * @param $rowTwo
     * @param $rowThree
     * @param $step
     * Displays the cards to the screen with radio buttons for user input
     */

    
function outputCards($rowOne$rowTwo$rowThree$step)
        {
        
?>
            <form id="formClass" method="post">
                <fieldset>
                    <legend>Row One:</legend>
                    <input id="rowOne" name="row" value="one" type="radio" required/>
                    <label for="rowOne">
                        <?php
                            
echo 'Row One<br>';
                            for (
$i 0$i <= 6$i++)
                                { 
// Display $rowOne cards array
                                
$card $rowOne[$i];
                                echo 
"<img src='images/cards/cards_small/$card.png' alt=\"$card Picture\">";
                                }
                        
?>
                    </label>
                </fieldset>
                <fieldset>
                    <legend>Row Two:</legend>
                    <input id="rowTwo" name="row" value="two" type="radio"/>
                    <label for="rowTwo">
                        <?php
                            
echo 'Row Two<br>';
                            for (
$i 0$i <= 6$i++)
                                { 
// Display $rowTwo cards array
                                
$card $rowTwo[$i];
                                echo 
"<img src='images/cards/cards_small/$card.png' alt=\"$card Picture\">";
                                }
                        
?>
                    </label>
                </fieldset>

                <fieldset>
                    <legend>Row Three:</legend>
                    <input id="rowThree" name="row" value="three" type="radio"/>
                    <label for="rowThree">
                        <?php
                            
echo 'Row Three<br>';
                            for (
$i 0$i <= 6$i++)
                                { 
// Display $rowThree cards array
                                
$card $rowThree[$i];
                                echo 
"<img src='images/cards/cards_small/$card.png' alt=\"$card Picture\">";
                                }
                        
?>
                    </label><br>
                </fieldset>

                <input type="hidden" name="step" value=<?php echo($step 1); ?>>
                <input class="button" type="submit" value="Go">

            </form>

            <?php
        
}

?>