Tracks You May Like

NameGenreIDComposerUnitPrice
For Those About To Rock (We Salute You)1Angus Young, Malcolm Young, Brian Johnson0.99
Balls to the Wall1U. Dirkschneider, W. Hoffmann, H. Frank, P. Baltes, S. Kaufmann, G. Hoffmann0.99
Fast As a Shark1F. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman0.99
Restless and Wild1F. Baltes, R.A. Smith-Diesel, S. Kaufman, U. Dirkscneider & W. Hoffman0.99
Princess of the Dawn1Deaffy & R.A. Smith-Diesel0.99
Put The Finger On You1Angus Young, Malcolm Young, Brian Johnson0.99
Let's Get It Up1Angus Young, Malcolm Young, Brian Johnson0.99
Inject The Venom1Angus Young, Malcolm Young, Brian Johnson0.99
Snowballed1Angus Young, Malcolm Young, Brian Johnson0.99
Evil Walks1Angus Young, Malcolm Young, Brian Johnson0.99
C.O.D.1Angus Young, Malcolm Young, Brian Johnson0.99
Breaking The Rules1Angus Young, Malcolm Young, Brian Johnson0.99
Night Of The Long Knives1Angus Young, Malcolm Young, Brian Johnson0.99
Spellbound1Angus Young, Malcolm Young, Brian Johnson0.99
Go Down1AC/DC0.99
Dog Eat Dog1AC/DC0.99
Let There Be Rock1AC/DC0.99
Bad Boy Boogie1AC/DC0.99
Problem Child1AC/DC0.99
Overdose1AC/DC0.99

<?php

$conn =  createConn();
session_start();


if (isset($_GET['add'])){

    $tracknum=$_GET['hiddenformid'];
	    
    if (isset($_SESSION['playlistcount'])){
	    $_SESSION['playlistcount']++;
	    $count = $_SESSION['playlistcount']; 
	}
	else{
	    $_SESSION['playlistcount']=1; 
	    $count=	$_SESSION['playlistcount']; 
	}

$_SESSION['playlistarray'][$count]=$tracknum; 
}


if (isset($_GET['delete'])){
    $trackidfromform=$_GET['trackidtodelete'];
    $linetodelete=$_GET['lineidtodelete'];
    $count=$_SESSION['playlistcount'];

        for ($i=1; $i<=$count; $i++){

            if (isset($_SESSION['playlistarray'][$i])){   

                if  ($linetodelete == $i){
                $idselected = $_SESSION['playlistarray'][$i];

                    if ($idselected == $trackidfromform){
                        $deletedindex=$i;
                        unset($_SESSION['playlistarray'][$i]);
                        $_SESSION['playlistcount'] = $_SESSION['playlistcount']-1;                
                        $count=$_SESSION['playlistcount'];
                    
                    
                        for ($x=$deletedindex; $x<=$count; $x++){
                        $_SESSION['playlistarray'][$x] = $_SESSION['playlistarray'][$x+1]; 					
                        //after deleting an array item's contents, move each subsequent array item's contents down by one space
                        //do not move anything in the preceeding array elements.... only start with blank element +1

                        }//endfor elements between deleted item and total of count
                        
                    }//endif id matches button

                }//endif line matches counter

            }//endif isset array element	

        }//endfor loop iterations = amount of $count

    if ($count<=0){//don't print playlist header if session is unset 
    unset($_SESSION);
    session_destroy();
    }
}// end if isset delete


if (isset($_SESSION['playlistcount'])){
?>
    <u><h2>Your Playlist</h2></u>
    <table>
    <tr><th>TrackName..</th><th>Composer..</th><th>Genre..</th><th></th></tr>

<?php

	for ($i=1; $i<=$count; $i++){	
	    if (isset($_SESSION['playlistarray'][$i])){        
            $line=$i;
            $chosenid = ($_SESSION['playlistarray'][$i]);
            $query = "Select Name, Composer, GenreId from Track where TrackId = $chosenid"; 
            $result = mysqli_query($conn,$query); 
            
        if (!$result){
            die(mysqli_error($conn));
        }
			
		if (mysqli_num_rows($result)> 0){
            $row = mysqli_fetch_assoc($result);
            echo "<tr><td>".$row['Name']."</td>";
            echo "<td>".$row['Composer']."</td>";
            echo "<td>".$row['GenreId']."</td>";
?>
            <td><form method="get" action="php-tracks.php">
            <input type='hidden' name='trackidtodelete' size="1" value="<?php echo $_SESSION['playlistarray'][$i]; ?>">
            <input type='hidden' name='lineidtodelete' size="1" value="<?php echo $line; ?>">
            <input type="submit" name="delete" value="remove"></form></td></tr>

<?php
	    }//end if sqli result
	
    }//end if isset array

}//end forloop

}//end if isset playlist count

echo "</table>";

//////////////////////////////////////////////////PROCESS COOKIES
//if name cookie set, say welcome
if(isset($_COOKIE['name'])){echo "Welcome, " . $_COOKIE['name'] . "!";}

//if discount cookie set, say discount
if(isset($_COOKIE['coupon'])){echo " Order today to receive 25% off your entire order!";}

//if genre1 cookie set, fill $genre1 variable 
//+ check for genre2 and genre3 cookies and fill vars if needed
//+ create query if genre cookies found
if(isset($_COOKIE['genre1']))
{
	$genre1=$_COOKIE['genre1'];
	$query = "select * from Track where GenreId = $genre1 Limit 20";

	if(isset($_COOKIE['genre2']))
		{$genre2=$_COOKIE['genre2'];
		$query = "select * from Track where GenreId = $genre2 or GenreId = $genre1 Limit 20";}

	if(isset($_COOKIE['genre3']))
		{$genre3=$_COOKIE['genre3'];
		$query = "select * from Track where GenreId = $genre1 or GenreId = $genre3 or GenreId = $genre2 Limit 20";}
}
else{
//if no genre cookie exists....
//NOTE to be here at all, you would have a NAME and at least ONE GENRE selected (unless direct via url)
$query = "select * from Track Limit 20";
};
?>

<u><h2>Tracks You May Like</h2></u>
<table>
<tr><th>Name</th><th>GenreID</th><th>Composer</th><th>UnitPrice</th><th></th></tr>

<?php
$result = mysqli_query($conn,$query);

if (!$result){
die(mysqli_error($conn));
}

if (mysqli_num_rows($result)> 0) {

    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr><td>".$row['Name']."</td>";
        echo "<td>".$row['GenreId']."</td>";
        echo "<td>".$row['Composer']."</td>";
        echo "<td>".$row['UnitPrice']."</td>";
        ?>
        <td>
        <form method='get' action='php-tracks.php'>
        <input type='hidden' name='hiddenformid' value="<?php echo $row['TrackId']; ?>">
        <input type='submit' name='add' value='Add to playlist'>
        </form>
        </td>
        </tr>

<?php
    }
} 
?>