How do I retrieve youtube videos from a channel without timing out? - php

I have this program that retrieves videos from a channel and shuffles them, as you can see, the program gets the channel's name from the variable $playlistId
<?php
session_start();
class PlayList {
public function __construct($id) {
$this->id = $id;
$this->videos = [];
$this->currentVideo = 0;
$this->addVideos('https://gdata.youtube.com/feeds/api/users/' . $this->id . '/uploads?v=2&alt=json');
shuffle($this->videos);
}
public function addVideos($url) {
$cont = json_decode(file_get_contents($url));
foreach ($cont->feed->entry as $entry) {
$video = [];
$video['title'] = $entry->title->{'$t'};
$video['desc'] = $entry->{'media$group'}->{'media$description'}->{'$t'};
$video['id'] = $entry->{'media$group'}->{'yt$videoid'}->{'$t'};
$this->videos[] = $video;
}
foreach ($cont->feed->link as $link) {
if ($link->rel === 'next') {
$this->addVideos($link->href);
break;
}
}
}
public function getId() {
return $this->id;
}
public function nextVideo() {
if ($this->currentVideo < count($this->videos) - 1) {
$this->currentVideo += 1;
} else {
$this->currentVideo = 0;
}
}
public function getVideoTitle() {
return $this->videos[$this->currentVideo]['title'];
}
public function getVideoDesc() {
return $this->videos[$this->currentVideo]['desc'];
}
public function getVideoSrc() {
return "http://www.youtube.com/embed/" . $this->videos[$this->currentVideo]['id'];
}
public function printList() {
foreach ($this->videos as $video) {
echo $video['title'] . '<br>';
}
}
}
?>
<?php
$playlistId = "NAKCollection";
if (!isset($_SESSION['playlist']) || $_SESSION['playlist']->getId() != $playlistId) {
$_SESSION['playlist'] = new PlayList($playlistId);
} else {
$_SESSION['playlist']->nextVideo();
}
?>
<html>
<head>
<title>Video Shuffle</title>
</head>
<body>
<div style="float: right;">
<?php
$_SESSION['playlist']->printList();
?>
</div>
<div>
<?php echo $_SESSION['playlist']->getVideoTitle(); ?>
</div>
<div>
<iframe type="text/html" src="<?php echo $_SESSION['playlist']->getVideoSrc(); ?>" allowfullscreen></iframe>
</div>
</body>
</html>
If the channel has like 300 videos the page times out, how can I retrieve 50 videos at a time like a batch and then another 50 when the first group is done?
I know I can alter php.ini but I don't want to do that now

This is using the deprecated GData API, if you use the new Data API v3, while retrieving channel's uploaded videos, you can set maxresults and get your results with paging, so you can iterate through pages.

Related

How to save favorite images after logout and still see them after login?

Problem:
So when I login, a user can go to the browse.php to add images to the favorites.php, and the photos will appear on that page.
When I log out and log back in, the images are gone (not saved).
Question:
How do I save it after I logout so on next login I get to see the favorite'd images from that particular user using cookies or other methods (can't use database because I'm not allowed to)?
My favorites page
<?php
session_start();
require_once 'favoritesfunction.php';
if (isset($_GET["fileName"])) {
$fileName = $_GET["fileName"];
addToFavorites($fileName);
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
$title = "Travel Photos - View Favorites";
?>
<link rel="stylesheet" href="viewfavorites.css" />
</head>
<body>
<main class="container">
<h1 class="favheader">Favorites</h1>
<div class="content">
<?php
if (isset($_SESSION['favorites']) && isset($_SESSION['email'])) {
$favorites = $_SESSION['favorites'];
if (count($favorites) > 0) {
?> <ul class="ul-favorite"> <?php
foreach ($favorites as $f) {
echo '<img class="displayPic" src="https://storage.googleapis.com/assignment1_web2/square150/' . $f . '">';
}
?>
</ul>
<p id="removeall"><button class="button"><span><a target="" href="services/removefavorites.php?remove=all">Remove All</a></span></button></p>
<?php
}
} else {
?>
<div class="nofavorites">
<p>No favorites found.</p>
</div>
<?php
}
?>
</div>
</main>
</body>
<script type="text/javascript" src="main.js"></script>
</html>
My add to favorites function php
<?php
function addToFavorites($fileName)
{
//Checks if the session favorites exists
if (isset($_SESSION['favorites'])) {
$favorites = $_SESSION['favorites'];
} else {
$favorites = array();
$_SESSION['favorites'] = $favorites;
}
// add item to favourites
$item = $fileName;
$match = false;
//Loop below checks for duplicates
$i = 0;
while ($i < count($favorites)) {
if ($favorites[$i] == $item) {
$favorites[$i] = $item;
$match = true;
}
$i++;
}
//if match equals false, that means its not in the favorites list of the user
//so it is added to the user's favorites array
if ($match == false) {
$favorites[] = $item;
}
$_SESSION['favorites'] = $favorites;
$_SESSION['favorites'] = $favorites;
}
My approach:
I tried doing something like setting $name = $_SESSION['email'] and $value="<img src=...>" then setcookie($name, $value) and then if(isset($_COOKIE[$value])) echo $value
I know this is totally wrong, I tried looking everywhere online to try to solve this, if I could get some help that would be great thanks!

include loading webpages in slideshow

I am totally new to stackoverflow. I'm trying to adjust an excisting script in which an folder containing photo and video is getting loaded and displayed like a slideshow.
I'd like to add the option to load webpages also. Is there any easy way to do this?
Thank you so much.
This is my code:
<?php
include "class.getFiles.php";
$images = new getFiles();
// list of all files in the images folder (includes videos)
$imageArray = $images->getImageArray();
$sortedImages = new sortFiles();
$sortedImages->sortImageArray($imageArray);
// remove files not in the correct time period
$imageArray = $sortedImages->getImageArray();
$randImage = $sortedImages->randomImageNum();
$fileName = $imageArray[$randImage];
$info = new SplFileInfo($fileName);
?>
<!DOCTYPE html>
<html>
<head>
<title>Fiction Slideshow</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<?php
if($info->getExtension() == "mp4")
{
echo '<video id="vid" class="videoDisplay" autoplay>
<source src="images/'.$fileName.'" type="video/mp4">
Your browser does not support the video tag.
</video>';
echo '<script type="text/javascript">
var vid = document.getElementById("vid");
vid.addEventListener("ended", function(){
window.location.reload();
});
</script>';
}
else
{
echo '<img class="imageDisplay" src="images/'.$fileName.'" />';
echo '<script type="text/javascript">
setTimeout(function(){
window.location.reload();
}, 30000);
</script>';
}
?>
</body>
</html>
This is the class.getFiles.php file that the other script calls.
<?php
class getFiles{
protected $dir;
protected $imageArray;
function __construct()
{
$this->get_dir();
$this->get_images();
}
protected function get_dir()
{
$this->dir = getcwd();
}
protected function get_images()
{
if(count(scandir($this->dir."/images")) != 2)
{
$this->imageArray = scandir($this->dir."/images");
}
else
{
die("There are no files in the directory");
}
}
public function getImageArray()
{
return $this->imageArray;
}
}
class sortFiles{
protected $sortedImageArray = [];
public function sortImageArray($imageArray)
{
foreach ($imageArray as $imageFile )
{
if($imageFile !== ".." && $imageFile !== ".")
{
$imagePath = $imageFile;
$imageFile = (substr($imageFile, 0, -4));
$BeginningPos = strpos($imageFile, '_');
$beginningDate = (substr($imageFile, 0, $BeginningPos));
$beginningDateformatted = str_replace("-","/", $beginningDate);
$stringToStartTime = strtotime($beginningDateformatted);
$EndingPos = strpos($imageFile, '_', $BeginningPos + strlen('_'));
$EndingPos = $EndingPos + 1;
$EndingDate = (substr($imageFile, $EndingPos));
$EndingDateformatted = str_replace("-","/", $EndingDate);
$stringToEndTime = strtotime($EndingDateformatted);
$time = time();
if($time <= $stringToStartTime && $time >= $stringToEndTime)
{
array_push($this->sortedImageArray, $imagePath);
}
}
}
}
public function getImageArray()
{
if(count($this->sortedImageArray) != 0)
{
return $this->sortedImageArray;
}
else
{
die("There are no files in the time range");
}
}
public function randomImageNum()
{
$imageArrayLength = count($this->sortedImageArray);
$imageRand = rand(0, $imageArrayLength-1);
return $imageRand;
}
}
?>
Well you can save the web pages as links within your media folder. For example if you want to display the web page http://www.example.com, then you can create a file webpage1.txt inside the media folder. This file can have the link http://www.example.com.
In your code you can add a new condition that checks if the file type is txt. If the file type is txt, then your code can read the link inside the file and display the link using an iframe. The following code should work:
else if(strpos($fileName, "weblink") !== false)
{
/** The path to the web page link */
$file_path = (getcwd() . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR . $fileName);
/** Read contents of file */
$web_page_link = file_get_contents($file_path);
/** Display the link in iframe */
echo "<iframe src='".$web_page_link."' width='100%' height='100%'></iframe>";
}
The above code should go above the else statement

How to generate link for the data fetched from MYSQL on webpage

For a quiz i am Fetching some random questions from a table and displaying the questions (one question at a time) on the webpage.
Now by taking count of the questions fetched i am displaying the numbers starting from 1 as a tab.
i.e. suppose i have fetched 10 questions then it will display tabs as 1 2 3 4 5 6 7 8 9 10. and currently what ever question is displaying on webpage that tab will be highlighted.
my question is how can i make these tabs as link so that when i click on that tab it will show that question on the webpage.
View:
<div id="container">
<?php echo "<br />" ?>
<?php
$attributes=array('id'=>'testForm');
echo form_open('attemptTest/getquestion/',$attributes);
?>
<div id="body">
<?php
$totq=explode(",",$test['random_question_no']);
$totq=array_sum($totq);
$totalQuestions=array_sum(explode(",",$test['random_question_no']));
$selected_answers=explode(',',$result['selected_answers']);
$selected_answers=$selected_answers[$qno-1];
?>
<input type="hidden" name="qno" value="<?=$qno?>" id="qno">
<input type="hidden" name="direction" value="N" id="direction">
<input type="hidden" name="submitTest" value="0" id="submitTest">
<input type="hidden" name="time1" value="<?php $time_taken=explode(',',$result['time_taken']); echo (array_sum($time_taken)+$result['iniTime']);?>" >
<code><table><tr><td valign="top" >Q<?=$qno?>) </td><td style="word-break: break-word;"><?=$question['question']?></td></tr></table></code>
<?php $option=explode(',',$question['options']);
foreach($option as $option){
$ans="ans".$qno;
?>
<div id="qoption">
<table><tr><td style="width:18px;" valign=top ><input type="radio" value="<?=$option?>" name="answer" <?php if($selected_answers==$option){ echo "checked";} ?> > </td><td><?=$option?></td></tr></table>
</div>
<?php
}
?>
<br/>
<table><tr><td><?php if($qno>="2"){ ?><div class="big_button" style="width:70px;"><center><a href="javascript:document.getElementById('direction').value='B';document.getElementById('testForm').submit();" id="big_button" >Back</a></center></div><?php } ?></td><td> </td><td>
<?php if($qno<$totalQuestions){ ?><div class="big_button" style="width:70px;"><center>Next</center></div> <?php } ?>
</td><td> </td>
<td>
<div class="big_button" style="width:120px;"><center>
Submit Test
</center></div>
</td></tr></table>
<br/>
</div></div>
<table id="all_questions">
<tr>
<?php $i=0; foreach($questions_all as $key=>$value){$i++;?>
<td class="<?php if($qno==$key+1){echo "active ";}?> <?php if(isset($_COOKIE['answered_'.$value])){if($_COOKIE['answered_'.$value]!=0){ echo "green";}} ?>" ><?=$key+1?></td><?php if($i==4){$i=0; echo "</tr><tr>";}?>
<?php }?>
</tr>
</table>
<style>
#all_questions td{
width:40px;
height:40px;
border-radius:3px;
margin:5px;
text-align:center;
vertical-align:middle;
}
.active {
background-color:red;
}
.green {
background-color:green;
}
</style>
Controller:
<?php
class attemptTest extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('test_model');
$this->load->model('attempt_model');
$this->load->helper('html');
$this->load->helper('url');
$this->load->library('session');
// if not logged in redirect
if(($this->session->userdata('logged_in'))!='TRUE'){ redirect('login'); }
}
public function index($tid='',$access_token='0')
{
$this->load->helper('cookie');
$this->load->library('form_validation');
// if test cookies exist bypass to getquestion function
if(isset($_COOKIE['tid']) && isset($_COOKIE['qids']) && isset($_COOKIE['resultid']) && isset($_COOKIE['qno']) && isset($_COOKIE['access_token']))
{
$this->getquestion($_COOKIE['qno'],$_COOKIE['qids'],$_COOKIE['tid']);
}
else
{
// else started new test so check access token cookie exist for test if not redirect
if(!isset($_COOKIE['access_token'])){
redirect('test/view/'.$tid);
}
// getting test data
$data['test'] = $this->test_model->get_test($tid,'0');
// getting multi dimensonal arrays of question ids which are going to assign user
$data['qids'] = $this->attempt_model->get_questions_id($data['test']);
// make one dimensonal array
$qid=array();
foreach($data['qids'] as $question)
{
$qid[]=$question['qid'];
}
// convert array into string
$qids=implode(",",$qid);
// insert result row
$data['resultid']=$this->attempt_model->insert_result_row($data['test'],$qids);
// creating required cookies
$cookie = array('name'=>'resultid','value'=>$data['resultid'],'expire'=>'86500');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qids','value'=>$qids,'expire'=>'86500');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'tid','value'=>$data['test']['tid'],'expire'=>'86500');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qno','value'=>'0','expire'=>'86500');
$this->input->set_cookie($cookie);
$this->getquestion('0',$qids,$tid,$data['resultid']);
}
}
public function getquestion($qno=0,$qids=0,$tid=0,$resultid=0)
{
if(!isset($_COOKIE['access_token'])){
redirect('index');
}
$this->load->helper('cookie');
$this->load->library('form_validation');
//define question ids
if(isset($_COOKIE['qids'])){
$qids=$_COOKIE['qids'];
}else{
$qids=$qids;
}
$eqid=explode(",",$qids);
//addition od code by vivek
$data['questions_all'] = $eqid;
//define test id
if(isset($_COOKIE['tid'])){
$tid=$_COOKIE['tid'];
}else{
$tid=$tid;
}
if(isset($_COOKIE['resultid'])){
$resultid=$_COOKIE['resultid'];
}
$iniTime = $this->attempt_model->get_result($resultid);
$test_time = $this->test_model->get_test($tid,'0');
// check time
if(((($test_time['test_time']*60)-(time()-$iniTime['iniTime']))<="0") || ((time())>($test_time['end_time']))){
$this->submit('Time Over');
return;
}
// define question no.
if(isset($_POST['qno'])){
$qno=$_POST['qno'];
$pqid=$eqid[$qno-1];
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
$time1=$_POST['time1'];
}else{ $answer='NULL'; $time1=$_POST['time1']; }
$this->attempt_model->submit_answer($_COOKIE['resultid'],$qno,$_COOKIE['tid'],$pqid,$answer,$time1);
}
if(isset($_POST['submitTest']))
{
if($_POST['submitTest']=='1')
{
$this->submit();
return;
}
}
// next question
if(isset($_POST['direction']))
{
// if direction is for next question add one
if($_POST['direction']=='N'){
$data['qno']=$qno+1; $qid=$eqid[$qno];
}
else
{
// else subtract one
$data['qno']=$qno-1;
$qid=$eqid[$qno-2];
}
}
else{
$data['qno']=$qno+1;
$qid=$eqid[$qno];
}
// getting earlier submitted answer if any
$data['result'] = $this->attempt_model->get_result($resultid);
// getting test data
$data['test'] = $this->test_model->get_test($tid,'0');
// getting question data
$data['question'] = $this->attempt_model->get_question($qid);
// load header page
$this->load->view('header_attemptTest.php',$data);
// load attempt test body page
$this->load->view('attemptTest',$data);
// load footer
$this->load->view('footer');
// update cookies with question number
$cookie = array('name'=>'qno','value'=>$data['qno'],'expire'=>'86500');
$this->input->set_cookie($cookie);
}
public function submit($msg='')
{
$this->load->helper('cookie');
$this->attempt_model->submit_test($_COOKIE['resultid'],$_COOKIE['tid']);
// page title meassage
$data['title']="Test Submitted";
$data['msg']=$msg;
$data['resultid']=$_COOKIE['resultid'];
$this->load->view('header.php',$data);
$this->load->view('submitTest',$data);
$this->load->view('footer');
// unset test cookies
$cookie = array('name'=>'resultid','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qids','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'tid','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qno','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'access_token','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
}
}
?>
Model:
<?php
class attempt_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_questions_id($test)
{
$noqs=explode(',',$test['random_question_no']);
$sids=explode(',',$test['subject_ids']);
foreach($noqs as $key => $noq){
$sid=$sids[$key];
if($key==0){
$sql="(select qid from questionBank where sid='$sid' order by rand() LIMIT $noq )";
}else{
$sql=$sql."UNION (select qid from questionBank where sid='$sid' order by rand() LIMIT $noq )";
}
}
$query = $this->db->query($sql);
$questions=$query->result_array();
return $questions;
}
// return single question
public function get_question($qid)
{
$sql="select * from questionBank where qid='$qid' ";
$query = $this->db->query($sql);
$questions=$query->row_array();
return $questions;
}
// return multiple questions
public function get_questions($qids)
{
$sql="select * from questionBank where qid in($qids) ORDER BY FIELD(qid, $qids) ";
$query = $this->db->query($sql);
$questions=$query->result_array();
return $questions;
}
public function get_result($resultid)
{
$sql="select * from result where result_id='$resultid' ";
$query = $this->db->query($sql);
$result=$query->row_array();
return $result;
}
public function submit_answer($resultid,$qno,$tid,$qid,$posted_answer,$time1)
{
$sql="select * from result where result_id='$resultid' ";
$query1 = $this->db->query($sql);
$result=$query1->row_array();
// getting correct answer from question
$sql="select answer from questionBank where qid='$qid' ";
$query = $this->db->query($sql);
$answer=$query->row_array();
//compare posted answer with correct answer
$correct_answer=explode(",",$result['correct_answer']);
$selected_answers=explode(",",$result['selected_answers']);
$time_taken=explode(",",$result['time_taken']);
$time2=(time()-$time1);
$time_taken[$qno-1]=($time_taken[$qno-1])+$time2;
if($posted_answer!='NULL'){
if($posted_answer==$answer['answer']){ $correct_answer[$qno-1]="1"; }else{ $correct_answer[$qno-1]="0"; }
$selected_answers[$qno-1]=$posted_answer;
}
$correct_answer=implode(",",$correct_answer);
$selected_answers=implode(",",$selected_answers);
$time_taken=implode(",",$time_taken);
// Update result row
$data = array('correct_answer' => $correct_answer,'selected_answers' => $selected_answers,'time_taken' => $time_taken);
$this->db->where('result_id', $resultid);
$this->db->update('result', $data);
}
public function submit_test($resultid,$tid)
{
// getting test data
$sql="select * from test where tid='$tid' ";
$query1 = $this->db->query($sql);
$test=$query1->row_array();
// getting result data
$sql="select * from result where result_id='$resultid' ";
$query2 = $this->db->query($sql);
$result=$query2->row_array();
// calculating result
$correct_answer=$result['correct_answer'];
$correct_answer=str_replace("2","0",$correct_answer);
$correct_answer=explode(",",$correct_answer);
$correctans=array_sum($correct_answer);
$wrong_answer=$result['correct_answer']; $wrong_answer=explode(",",$wrong_answer); $wrong_ans=count( array_keys( $wrong_answer, "0" ));
$total_marks=$correctans-$wrong_ans*$test['minus_mark'];
$percentage=($total_marks/$result['total_question'])*100;
if($percentage>=$test['reqpercentage'])
{
$status="1";
}
else
{
$status="0";
}
// update result row
$data = array('obtained_percentage' => $percentage,'status' => $status,'submitTime'=>time());
$this->db->where('result_id', $resultid);
$this->db->update('result', $data);
}
public function insert_result_row($test,$qids)
{
$totq=explode(",",$test['random_question_no']);
$totqs=array_sum($totq);
$temp_value=array();
for($i=0; $i < $totqs; $i++)
{
$temp_value[$i]='2';
}
$temp_value=implode(",",$temp_value);
$temp_time=array();
for($j=0; $j < $totqs; $j++)
{
$temp_time[$j]='0';
}
$temp_time=implode(",",$temp_time);
$uid=$this->session->userdata('uid');
// insert row
$data=array('uid'=>$uid,'tid'=>$test['tid'],'total_question'=>$totqs,'correct_answer' => $temp_value,'time_taken' => $temp_time, 'selected_answers' => $temp_value,'question_ids'=>$qids,'status'=>'2','iniTime'=>time());
$this->db->insert('result', $data);
return $this->db->insert_id();
}
}
?>
I heaven't read your code but I suppose you would want this ;)
In this case, we assume this code is all in index.php file.
This is how you link to your question (or you can redirect there in any other way you want):
<?php
$id = -1;
if (isset($_GET["questionid"])) {
$id = $_GET["questionid"];
}
?>
<a href="index.php?questionid=1" <?php if($id == 1){echo " class='active'"} ?>>Question 1</a>
<a href="index.php?questionid=2" <?php if($id == 2){echo " class='active'"} ?>>Question 2</a>
<a href="index.php?questionid=3" <?php if($id == 3){echo " class='active'"} ?>>Question 3</a>
And here is the code how to show question with id questionid.
<?php
if ($id > -1) {
showQuestion($id);
// = your code to show question with id questionid
}
else {
echo "No question selected";
}
?>

Using DOM to get HTML code.

I am collecting username, title, video count and comment count from html source. Consider this link
Here is my code to get it:
function getParameter($url)
{
$html = file_get_html($url);
if($html)
{
$containers1 = $html->find('div.v div.v-link v-meta-entry');
foreach($containers1 as $container)
{
$plays = $container->find('v-num'); // get nos of time video played
$item = new stdClass();
foreach($plays as $play)
{
$nos = $play->plaintext;
}
//echo $address;
}
$containers2 = $html->find('div.v div.v-link v-meta va a'); //get user name
foreach($containers2 as $username)
{
$user = $username->plaintext;
}
$containers3 = $html->find('div.v div.v-link a'); //get video title
foreach($containers3 as $title)
{
$title = $title->plaintext;
}
$commentcontainers = $html->find('div.ico-statcomment span'); //get nos of comments
foreach($commentcontainer as $cont)
{
$comments = $cont->plaintext;
}
return $data;
}
}
But it gives this error:
Warning: Invalid argument supplied for foreach() in /var/www/html/scrap/yelp/yoku.php on line 41
any hints for this?
Here is the source code snippet:
<div class="v" >
<div class="v-thumb">
<img src="http://r1.ykimg.com/0515000052B7B3D46714C0318106EA36" alt="和小姚晨约会激动抽筋" />
<div class="v-thumb-tagrb"><span class="v-time">08:56</span></div>
</div>
<div class="v-link">
</div>
<div class="v-meta va">
<div class="v-meta-neck">
<a class="v-useravatar" href="http://i.youku.com/u/UMTQxNDg3NzA4" target="_blank"><img title="嘻哈四重奏" src="http://g3.ykimg.com/0130391F455211D40E180C021BBB97D37C4057-26F4-2F53-A8CD-4A4139415701"></a>
<span class="v-status"> </span>
<a class="v-username" href="http://i.youku.com/u/UMTQxNDg3NzA4" target="_blank">嘻哈四重奏</a>
</div>
<div class="v-meta-title">和小姚晨约会激动抽筋</div>
<div class="v-meta-entry">
<i class="ico-statplay" title="播放"></i><span class="v-num">588万</span> <i title="评论" class="ico-statcomment"></i><span class="v-num">1,290</span> </div>
<div class="v-meta-overlay"></div>
</div>
</div>
Check selectors, documentation here simplehtmldom. For example, I did some changes in selectors.
function getParameter($url)
{
$html = file_get_html($url);
if($html)
{
//we iterate all 'div.v' and select data from every 'div.v' separately
$containersDiv = $html->find('div.v');
foreach($containersDiv as $div)
{
$containers1 = $div->find('div[class=v-meta va] div.v-meta-entry');
foreach($containers1 as $container)
{
$plays = $container->find('.v-num'); // get nos of time video played
$item = new stdClass();
foreach($plays as $play)
{
$nos = $play->plaintext;
}
//echo $address;
}
$containers2 = $div->find('div[class=v-meta va] a'); //get user name
foreach($containers2 as $username)
{
$user = $username->plaintext;
}
$containers3 = $div->find('div.v-link a'); //get video title
foreach($containers3 as $title)
{
$title = $title->plaintext;
}
$commentcontainers = $div->find('div[class=v-meta va] div.v-meta-entry span'); //get nos of comments changed
foreach($commentcontainer as $cont)
{
$comments = $cont->plaintext;
}
}
return $data;
}
}

setting a value for index.php page

is there a way set a value for the index.php page on its first load?
i would like index.php it to load like so "index.php?subject=1" when it loads for the first time.
as this value will change as they move around in the site i don't want it to be a fixed value.
some one sugested
if(empty($_SESSION['visited'])) {
//DO STUFF
$_SESSION['visited'] = true; }
i cant seem to get that to work with my function.
find_selected_page()
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_page = null;
$current_subject = null;
}
}
index.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/session/session.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/db/dbcon.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/functions.php');
$context = "public";
find_selected_page ();
?>
<html>
<head>
<title>Home</title>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/style.php'); ?>
<body>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); ?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/nav_ribbon.php');?>
<div id="p2dbg">
<div id="p2dcontent">
<div class="p2dcontent">
<h1><?php echo htmlentities($current_subject ["menu_name"]); ?></h1><br />
<?php if ($current_page) { ?>
<p><?php echo nl2br($current_page ["content"]); ?></p><br />
<?php } else { ?>
This page does not exist! please slect a page from the menu.
<?php } ?>
</div>
</div>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); ?>
</div>
</body>
</html>
Instead of setting everything to null if your value isn't passed, just assume the default value:
// Set this to the value you want as your default
define("DEFAULTSUBJECT",1);
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_subject = find_subject_by_id (DEFAULTSUBJECT);
$current_page = find_default_post($current_subject ["id"]);
}
}

Categories