Web_Start Your Day

start

Start Your Day

Tell you what’s the weather today and pick a song for you to start your day!

(codes below)

page_1

<?php
/* 
-----
Start your day with setting up goal, knowing the weather, and enjoying music picked up for you.
-----
*/
?>
<!DOCTYPE HTML> 
<html> 
	<head>
		<title>Start Your Day</title>
	</head>
	
	<body>
	<h1 align="center">Start Your Day</h1>
	
	<p align="center"><img src="start.png" alt="Start" height="150" width="160"/></p>

	<p align="center">Good morning stranger.<br/>What's your mood today?</b></p>
		
	<form align="center" action="/web_05/Start_Your_Day/myDay.php" method="post">
		<div class="selec_box">
  			<label for="mood">mood:</label>
        	<!-- The second value will be selected initially -->
			<select name="mood">
				<option selected>-----</option>
			    <option value="workaholic">workaholic</option> 
			    <option value="relaxed">holiday</option>
				<option value="lost">lost</option>
			</select>
	    </div>
	    <div>
        	<label for="city">city:</label>
	        <input type="text" name="city" />
    	</div>
    	<div>
        	<label for="country">country:</label>
	        <input type="text" name="country" />
    	</div>
    	<div class="button">
        	<button type="submit"> :) </button>
	    </div>
	</form>
	
	

</body> 
</html>

page_2

 <?php

 	// mood
	//session_start();

	if ( $_POST['mood'] == "-----" ) {
		echo "It'd be lovely if you tell me your mood.";
	} else if ( empty($_POST['city']) || empty($_POST['country']) ) {
		echo "It'd be lovely if you tell me your location.";
	} else if ( empty($_POST['mood']) && empty($_POST['city']) && empty($_POST['country']) ) {
		echo "It'd be lovely if you tell me your mood and location.";
	} else {
		$mood = $_POST['mood'];
		$city = $_POST['city'];
		$country = $_POST['country'];
	

	if ($mood == "workaholic") {
		$imgMood = "work.png";
	} else if ($mood == "relaxed") {
		$imgMood = "holidays.png";
	} else if ($mood == "lost") {
		$imgMood = "lost.png";
	}
 	// weather
 	$data = file_get_contents(
	"http://api.openweathermap.org/data/2.5/weather?q=" . $city . "," . $country . "&units=metric");
	// turn the JSON data into a PHP object
	$myday = json_decode($data);

	//var_dump($myday); //for debugging
	$today = date('M d',$myday->dt); // format the timestamp as a human-readable date
	$tempMinMax = $myday->main->temp .' C';
	$myweather = $myday->weather[0]->description;
	
	
?>

<!DOCTYPE HTML> 
<html> 
	<head>
		<title>In this way.</title>
	</head>
	<body>
	<h1 align="center"><font size="5">It will be a good day!</font></h1>
	<p align="center">
		<?php
			echo "Today is " . $today . ".<br/> Temperature is around " . $tempMinMax ."
				; " . $myweather . ".<br/>";	
			echo " <br/>";
			echo "I woke up in " . $city . ", feeling " . $mood . ".<br/>";
			echo " <br/>";
			echo '<img src="'. $imgMood . '"alt="Start" height="150" width="160"/>';
			echo " <br/>";
			echo "Today will be a good day,<br/> because I start it with this song.<br/>";
			echo " <br/>";

			require('google-api-php-client/src/Google_Client.php');
			require('google-api-php-client/src/contrib/Google_YouTubeService.php');

			/* Set $developer_key to the "API key" value from the "Access" tab of the
			Google APIs Console <http://code.google.com/apis/console#access>
			Go to your app, then open "Server Key" and copy the "API Key" value
			Please ensure that you have enabled the YouTube Data API for your project. */
			$developer_key = 'AIzaSyBq4Y_-39wzoHM1c5xd19SXORtjA31As7A';


			// create a new Google Client object
			$client = new Google_Client();
			$client->setDeveloperKey($developer_key);

			$youtube = new Google_YoutubeService($client);

			$search_term = $mood . " music " . $myweather . " " . $city;
			//echo $search_term;

			// You could make the search term dynamic by sending it in the query string with GET
			// ex: yoursite.com/thispage.php?search=puppies
			// You could also make a form to allow someone to fill out a search field
			if (isset($_GET['search'])){
				$search_term = $_GET['search'];
			}

			// see more about the available search parameters here: https://developers.google.com/youtube/v3/docs/search/list
			$searchResponse = $youtube->search->listSearch('id,snippet', array(
			  'q' => $search_term,
			  'maxResults' => 20,
			  'type'=>'video'
			));

			//var_dump($searchResponse); // for debugging
			//echo "Search: <em>$search_term</em><br/>";

			/*
			foreach ($searchResponse['items'] as $searchResult) {
				$title = $searchResult['snippet']['title'];
				$thumbnail_src = $searchResult['snippet']['thumbnails']['default']['url'];
				$video_id = $searchResult['id']['videoId'];
				//YouTube video URL format: http://www.youtube.com/watch?v=mmf0KAHWlnk
				$video_url = 'http://www.youtube.com/watch?v='.$video_id;
				echo '<a href="'.$video_url.'">';
				echo '<img src="'.$thumbnail_src.'" height="250" width="400"/>';
				echo '</a><br/>';
			}*/

			
			$length = count($searchResponse['items']);
			$num = rand(1, $length) - 1;
			$searchResult = $searchResponse['items'][$num];

			$title = $searchResult['snippet']['title'];
			$thumbnail_src = $searchResult['snippet']['thumbnails']['default']['url'];
			$video_id = $searchResult['id']['videoId'];
			//YouTube video URL format: http://www.youtube.com/watch?v=mmf0KAHWlnk
			$video_url = 'http://www.youtube.com/watch?v='.$video_id;
			//echo $num;
			echo " <br/>";
			//echo '<a href="'.$video_url.'">';
			//echo '<img src="'.$thumbnail_src.'" height="250" width="400"/>';
			echo '<iframe width="420" height="315" src="//www.youtube.com/embed/'. $video_id .'"" frameborder="0" allowfullscreen></iframe>';
			// echo " <br/>";
			// echo $thumbnail_src;
			//echo '</a><br/>';
			echo '<br/>';
			

		}
		?>
	</p>

	<p align="center">
	<a href="start.php"><font size="2">somehow... my mood changes!</font></a></li>
	</p>
	
	
	
</body> 
</html>

Leave a Comment.