separate data from a api - php

we are trying to make a website for our class, here you can see important news, the class schedule, homework schedule and a contact page for tips and bugs...
So, we want to have the schedule always up to date and a class mate made a api to get the schedule-data from our school site. but now we have an issue... he don't want to help us anymore!!! -,-
here is the question?
The schedule is echo in a table but it stand next to eachother, because the api only separate the days including the lesson. but we want the lesson seperate from the day! and echo the table below each other..
Is there anyone who want to help me / Us out???
Link for a live previeuw how its now!
<?php
$apis = array('verandermij');
if (!isset($_GET['afdeling'])) //school sector
{
echo "FOUTCODE 1"; //Wrongcode
exit;
}
if (!isset($_GET['klas'])) // class
{
echo "FOUTCODE 2"; //Wrongcode
exit;
}
if (!isset($_GET['api']))
{
echo "FOUTCODE 3"; //Wrongcode
exit;
}
$apigeldig = false;
foreach ($apis as $api)
{
if ($api == $_GET['api'])
{
$apigeldig = true;
}
}
if ($apigeldig == false)
{
echo "FOUTCODE 4"; //Wrongcode
exit;
}
$afdeling = $_GET['afdeling'];
$klas = $_GET['klas'];
// get the class data
$url = "https://rooster.rocfriesepoort.nl/emmeloord.aspx?group=" . $afdeling . "&specs=" . $klas . "&a=1";
$content = file_get_contents($url);
// little bit cleaning
$content = explode("<div class=header>Rooster</div>", $content);
$content = $content[1];
// Splits dagen
$raw_dagen = explode("<td valign=top style='background-color: #e5e5e5'>", $content); //Raw_Days
$clean_dagen = array(); // Clean_Days
$dagen = array( //Days
$raw_dagen[1],
$raw_dagen[2],
$raw_dagen[3],
$raw_dagen[4],
$raw_dagen[5]
);
// Beginnen met sorteren
$opgeschoond = array(); //Cleand
$stoploop = false;
foreach ($dagen as $dag) //Days as Day
{
$tmp = explode("</div>", $dag);
$i = 0;
foreach ($tmp as $item)
{
if ($stoploop == true) { $i++; continue; }
if ($tmp[$i] == null) { $i++; continue; }
$tmp[$i] = str_replace("<div class='dagHeader'>", "", $tmp[$i]);
$tmp[$i] = str_replace("<div class=tablecell3>", "VRIJ", $tmp[$i]);
$tmp[$i] = str_replace("<div class=tablecell1>", "", $tmp[$i]);
$tmp[$i] = str_replace("<div class=tablecell2>", "", $tmp[$i]);
$tmp[$i] = str_replace('<br style="clear:both"></br><br><br><div id=footer style="width:95%;">© ROC Friese Poort 2015 - ', "", $tmp[$i]);
$tmp[$i] = str_replace("<a href='javascript:void(0)'". ' onclick="Disclaimer' . "('500','500','YES')" . '" title=' . "'Disclaimer'>", "", $tmp[$i]);
if ($tmp[$i] == "</td>")
{
$tmp[$i] = "DAG EINDE";
}
if ($tmp[$i] == "</td></table>")
{
$tmp[$i] = "DAG EINDE";
unset($tmp[$i + 1]);
unset($tmp[$i + 2]);
unset($tmp[$i + 3]);
unset($tmp[$i + 4]);
unset($tmp[$i + 5]);
unset($tmp[$i + 6]);
$stoploop = true;
}
$i++;
}
array_push($opgeschoond, $tmp);
}
print(json_encode($opgeschoond));
?>
this we use the echo the data!!!!!
<table>
<tr>
<?php
foreach ($maandag as $lesuur)
{
print("<td>");
print($lesuur);
print("</td>");
}
?>
</tr>
<tr>
<?php
foreach ($dinsdag as $lesuur)
{
print("<td id='oneven'>");
print($lesuur);
print("</td>");
}
?>
</tr>
<tr>
<?php
foreach ($woensdag as $lesuur)
{
print("<td>");
print($lesuur);
print("</td>");
}
?>
</tr>
<tr>
<?php
foreach ($donderdag as $lesuur)
{
print("<td id='oneven'>");
print($lesuur);
print("</td>");
}
?>
</tr>
<tr>
<?php
foreach ($vrijdag as $lesuur)
{
print("<td>");
print($lesuur);
print("</td>");
}
?>
</tr>
</table>
link for the raw data: enter link description here
[["Maandag 09-11-2015","MTM
Nederlands
C2.02
","MTM
Nederlands
A2.08
","VRIJ","KAS
Wiskunde
B2.02
","KSR
SLB
A2.07
","DAG EINDE"],["Dinsdag 10-11-2015","VRIJ","CRL
Database techn.
A2.07
","CRL
Database techn.
A2.07
","CRL
CMS
A2.07
","VRIJ","CRL
CMS
A2.07
","CRL
Webservers
A2.07
","CRL
Webservers
A2.07
","DAG EINDE"],["Woensdag 11-11-2015","VRIJ","VRIJ","KSR
Java Script
A2.07
","KSR
Java Script
A2.07
","VRIJ","VRIJ","VSS
Routeplanner
B2.04
","VSS
LevO
B2.04
","VSS
LLB
B2.04
","DAG EINDE"],["Donderdag 12-11-2015","VRIJ","KRJ
Project
A2.07
","KRJ
Project
A2.07
","KRJ
Project
A2.07
","VRIJ","KSR
Scrum
A2.07
","GFO
Rekenen
B2.01
","KSR
Scrum
A2.07
","DAG EINDE"],["Vrijdag 13-11-2015","ZAA
Engels
B2.03
","ZAA
Engels
B2.06a
","KRJ
Project
A2.07
","KRJ
Project
A2.07
","VRIJ","KSR
SLB
A2.07
","GFO
Rekenen
B2.06
","DAG EINDE"]]
thanks for helping me, a beginner (a)

The data returned from the 'api' is a two dimensional array, with the second dimension variable width, but the first entry is the day. data[0][0] = "Maandag 09-11-2015";.
If I understand correctly, what you want is something like this:
$apidata = file_get_contents($apiurl);
$data = json_decode($apidata, true);
$maandag = array_slice($data[0],1)
$dinsdag = array_slice($data[1],1)
$woensdag = array_slice($data[2],1)
$donderdag = array_slice($data[3],1)
$vrijdag = array_slice($data[4],1)
Then your echo from above will work, the day names removed from the arrays (though also the dates).
Once in this form, building a table for the days is relatively simple (though I'm sure others will have a cleaner way to do it):
<?php
$longest = 0;
for($i=0;$i<5;$i++) {
if($data[$i].count()>$longest) $longest=$data[$i].count();
}
$longest--; // we removed one entry already
?>
<table>
<tr><th>Maandag</th><th>Dinsdag</th><th>Woensdag</th><th>Donderdag</th><th>Vrijdag</th></tr>
<?php
for($i=0;$i<$longest;$i++) {
print "<tr><td>{$maandag[$i]}</td><td>{$dinsdag[$i]}</td><td>{$woensdag[$i]}</td><td>{$donderdag[$i]}</td><td>{$vrijdag[$i]}</td></tr>\n";
}
?>
</table>
That should have you set. There may be runtime errors attempting to access the array entries of the shorter days before the longest day is finished, but you can fix that with another loop adding empty strings.

Related

Unable to use an array as an array?

I am in the process of building a software download site for my company.
However, I have come across a problem that I am unsure how to get past.
I am retrieving all the information from a table about a particular software release and placing them in a multidimensional array. I am then trying to run a foreach (I have even tried a for loop) against that array and I get an error shown below:
When I run var dump against the original array, I get this:
So I am really confused as I don't know what I'm missing or where I am going wrong. The reason why I want to run this is so that I can filter the array into a one dimensional array.
Below is the code for the main web page
<?php
//Displays list of companies in 2 columns
$versionAccess = VersionAccess::findAccess($relId);//This gets set earlier in the webpage
$count = count($versionAccess);
var_dump($versionAccess);
foreach ($versionAccess as $va)
{
if ($va->company_access != '0')
{
$versionAcc[] = $va->comapny_id;
}
}
foreach ($company as $compAccess)
{
$compAccessId = $compAccess->company_id;
if (in_array($compAccessId, $versionAcc))
{
$access = 'checked disabled';
}
else { $access = 'disabled'; }
$accessName = 'access'.$compAccessId;
if ($ctr % 2 == 0)
{
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
else
{
if ($ctr < $compCount)
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
}
else
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
}
$ctr++;
}
?>
The function that brings the data from the database is:
public static function findAccess($accessId)
//find the version access in database
{
return self::findQuery("SELECT * FROM version_access WHERE version_id = '$accessId'");
}
The findQuery method:
public static function findQuery($sql)
{
global $database;
$resultSet = $database->query($sql);
$objectArray = array();
while ($row = mysqli_fetch_array($resultSet))
{
$objectArray[] = self::instant($row);
}
return $objectArray;
}
I am still relatively new and any help is greatly appreciated.

Total sum of several columns in a table

I am currently having problem with my total. Well, I have 3 different tables. billing_payments, billing_entry, and services.
My problem is, I can't get the total of each SERVICES. I provided a screenshot so that you'll get what I mean.
Here is the code of the report. I have added some comments to indicate where the problem starts and ends.
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>PATIENT NAME</th>
<th>CASE</th>
<?php
$servicesquery = $this->db->get('services');
foreach ($servicesquery->result() as $service) {
echo '<th>'.$service->service_name.'</th>';
}
?>
<th>MEDICAL SUPPLIES</th>
<th>PHILHEALTH</th>
<th>DISCOUNT</th>
<th>GROSS</th>
<th>NET</th>
<tr>
</thead>
<tbody>
<?php
$x = 1;
$billquery = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($billquery->result() as $data) {
echo '<tr>';
echo '<td>'.$x++.'</td>';
echo '<td>'.$data->patientname.'</td>';
echo '<td>'.$data->session_id.'</td>';
//SERVICES
$servicesquery = $this->db->get('services');
foreach ($servicesquery->result() as $service) {
$this->db->where('billing_serviceid', $service->service_id);
$this->db->where('billing_patientid', $data->patient_id);
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$billing = $this->db->get('billing_entry');
foreach ($billing->result() as $bill) {
echo '<td>'.$bill->billing_amount.'</td>';
}
}
//MEDICAL SUPPLIES
$this->db->where('billing_id', $data->billing_id);
$this->db->where('billing_servicename', 'MEDICAL SUPPLIES');
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
echo '<td>'.$med->billing_amount.'</td>';
}
//PHILHEALTH
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_philhealth');
$philhealth = $this->db->get('billing_entry');
foreach ($philhealth->result() as $phil) {
echo '<td class="bg-info">'.$phil->billing_philhealth.'</td>';
}
//DISCOUNT
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_discount');
$philhealth = $this->db->get('billing_entry');
foreach ($philhealth->result() as $phil) {
echo '<td class="bg-info">'.$phil->billing_discount.'</td>';
}
//GROSS
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$gross = $this->db->get('billing_entry');
foreach ($gross->result() as $gr) {
echo '<td class="bg-warning">'.$gr->billing_amount.'</td>';
}
echo '<td class="bg-danger">'.$data->total_amount.'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td colspan="3" style="text-align:right"><strong>TOTAL:</strong></td>';
//PROBLEM STARTS HERE
//TOTAL PER SERVICES
$quer = $this->db->get('services');
foreach ($quer->result() as $service) {
$totserv = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totserv->result() as $servdata) {
$id = $servdata->id;
$this->db->where('billing_id', $servdata->billing_id);
$this->db->where('billing_serviceid', $service->service_id);
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
echo '<td class="bg-success">'.$med->billing_amount.'</td>';
}
}
}
//PROBLEM ENDS HERE
//TOTAL NET
$totalamt = $this->db->query('SELECT SUM(total_amount) AS totalamount FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totalamt->result() as $data) {
echo '<td>'.$data->totalamount.'</td>';
}
echo '</tr>';
?>
If you look at your image, it's clear to see you're outputting each value from the columns (in the green box) in your final row (350, zero, 350, 485, 485, 485, zero, 15, 300)
You can fix this by adding a placeholder variable to calculate the total for each service, e.g.:
foreach ($quer->result() as $service) { // you want to output one <td> per service, so the <td> should be printed at the end of this foreach
$serviceTotal = 0; // initialize the total for each service
$totserv = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totserv->result() as $servdata) {
$id = $servdata->id;
$this->db->where('billing_id', $servdata->billing_id);
$this->db->where('billing_serviceid', $service->service_id);
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
$serviceTotal = $serviceTotal + $med->billing_amount
}
}
echo '<td class="bg-success">'. $serviceTotal .'</td>';
}
This way you will get one per service, and the $serviceTotal variable will contain the sum of each individual line item for that particular service.
You have more efficient options open to you as well - firstly, you could calculate the individual totals when you are getting them in the services code block, e.g.:
$serviceTotals = array(); // a holder for all the totals
foreach ($servicesquery->result() as $service) {
$this->db->where('billing_serviceid', $service->service_id);
$this->db->where('billing_patientid', $data->patient_id);
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$billing = $this->db->get('billing_entry');
foreach ($billing->result() as $bill) {
$serviceTotals[$service->service_id] = $serviceTotals[$service->service_id] + $bill->billing_amount; //this updates the total for each service ID
echo '<td>'.$bill->billing_amount.'</td>';
}
}
You could then loop through this array to output each total.
Ideally, you should also consider calculating all these values in your controller, which would call the queries you need from your model(s), and then packaging that data in your $data variable. The controller can then pass all of that to your view. CodeIgniter has a lot of good resources for explaining the Model-View-Controller (MVC) pattern (it's a bit outside of the scope of this question to go into it here), and using CI without implementing the MVC pattern is going to make it harder to make use of most of the documentation and support for CI on the net.

php sidebar and footer inside main

I help develop my school's website what I can not figure out is why on a certain page the sidebar and footer go into the table main. It only does this for two thing custodial and kitchen even when I think the code for say administrators is the same code and looks the same to me.
This is the code used to generate the teachers page
The link below will take you to the broken page since I can't post pictures
http://gwhs.kana.k12.wv.us/academics/display.php?action=teacher&id=103
While the link below will take you to one that actually works
http://gwhs.kana.k12.wv.us/academics/display.php?action=teacher&id=24
If you want the entire file for this code let me know and I will post a link
function dTeacher() {
global $db, $id;
if ($stmt = $db->prepare("SELECT name, department, schedule, education, whyteach, phone, email, image, quote FROM teachers WHERE id = ?"))
{
$stmt->bind_param('i', $id);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
$stmt->close();
$schedule = array();
$schedule = explode(",",$row['schedule']);
$education = explode(",",$row['education']);
$noshow = array(20, 14, 25, 15, 24, 21, 23);
print '<h1>Viewing '.$row['name'].'\'s Profile!</h1>';
if ($row['image']) {
print '<img style="max-width:40%; display: block; margin: 2% auto;" src="'.$row['image'].'" alt="Teacher Image Here">';
}
if (!(in_array($row['department'], $noshow))) {
print '<h3>Schedule</h3>
<table id="maindata">
<tr id="head"><td style="width:30%;">Period</td><td style="width:70%;">Class</td></tr>';
for ($i=0; $i<count($schedule); $i++)
{
$oddeven = ($i%2==0) ? "even" : "odd";
$scnum = $schedule[$i];
$scresult = $db->query("SELECT id, name FROM classes where id = {$scnum} LIMIT 1");
$scrow = $scresult->fetch_assoc();
if ($scnum == 1337) {
$scrow['name'] = "OFF";
} //off periods
print '<tr id="'.$oddeven.'"><td style="width:30%;">'.$i.'</td><td style="width:70%;">'.$scrow['name'].'</td></tr>';
}
}
if ($row['education']) {
print "</table><h3>Education</h3>";
for ($i=0; $i<count($education); $i++)
{
$oddeven = ($i%2==0) ? "even" : "odd";
print '<table id="maindata"><tr id="'.$oddeven.'"><td>'.$education[$i].'</td></tr>';
}
print '</table>';
}
if ($row['phone'] || $row['email']) {
print '
<h3>Contact</h3>
<table id="maindata"><tr id="even"><td style="width:30%;">Phone</td><td>'.$row['phone'].'</td></tr>
<tr id="odd"><td style="width:30%;">Email</td><td>'.$row['email'].'</td></tr></table>';
}
if ($row['whyteach'] != "") {
print '<h3>Why do you teach?</h3>
<table id="maindata"><tr id="even"><td><p>'.$row['whyteach'].'</p></td></tr></table>';
}
if ($row['quote'] != "") {
print '<h3>Favorite Quote</h3>
<table id="maindata"><tr id="even"><td><p>'.$row['quote'].'</p></td></tr></table>';
}
}
else {
print "Error with your request.";
die();
}
}
You have a bug here, you are not closing table tag:
if (!(in_array($row['department'], $noshow))) {
print '<h3>Schedule</h3>
<table id="maindata">
<tr id="head"><td style="width:30%;">Period</td><td style="width:70%;">Class</td></tr>';
for ($i=0; $i<count($schedule); $i++)
{
$oddeven = ($i%2==0) ? "even" : "odd";
$scnum = $schedule[$i];
$scresult = $db->query("SELECT id, name FROM classes where id = {$scnum} LIMIT 1");
$scrow = $scresult->fetch_assoc();
if ($scnum == 1337) {
$scrow['name'] = "OFF";
} //off periods
print '<tr id="'.$oddeven.'"><td style="width:30%;">'.$i.'</td><td style="width:70%;">'.$scrow['name'].'</td></tr>';
}
echo "</table>";
}
Because of this, when the profile has Schedule the table is not closed and the page will display bad.
And closes it here:
if ($row['education']) {
print "<h3>Education</h3>";
With two modifications the web should do its work fine.
Take a look this HTML validator, it will help you:
http://validator.w3.org/check?uri=http%3A%2F%2Fgwhs.kana.k12.wv.us%2Facademics%2Fdisplay.php%3Faction%3Dteacher%26id%3D103&charset=%28detect+automatically%29&doctype=Inline&group=0
Best regards.
First words: You should re-think the layout. Creating multiple tables to display page content is not the best approach and furthermore IDs have to be unique. So for valid HTML you can't have multiple tables with the ID maindata.
As mentioned by cmorrissey in the comments, you're not closing the first table, unless you enter the next if statement. Furthermore this part
if ($row['education']) {
print "</table><h3>Education</h3>";
for ($i=0; $i<count($education); $i++) {
$oddeven = ($i%2==0) ? "even" : "odd";
print '<table id="maindata"><tr id="'.$oddeven.'"><td>'.$education[$i].'</td></tr>';
}
print '</table>';
}
of your code closes the previous <table id="maindata"> but opens a new one in each iteration of the loop. After the loop, your closing just one of them. So if it is correct, that you want to create multiple of these tables, you should close them in the loop as well:
for ($i=0; $i<count($education); $i++) {
$oddeven = ($i%2==0) ? "even" : "odd";
print '<table id="maindata"><tr id="'.$oddeven.'"><td>'.$education[$i].'</td></tr>';
print '</table>';
}

Foreach loops in PHP and Joomla

I'm currently managing the display of MySQL content in HTML with foreach loop like this :
<?php
echo "<table class=\"tableau\">
<tr bgcolor=\"#a72333\" class=\"first\">
<th>Repere</th>
<th>Niveau</th>
<th>Enseigne</th>
<th>Activités</th>
</tr>
<tbody>";
$db= JFactory::getDBO();
$query = 'SELECT baseData, sid, fid FROM XXXX_sobipro_field_data';
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as &$value) {
if ($value->sid == 55) {
if ($value->fid == 20) {
$repere = $value->baseData;
}
if ($value->fid == 16) {
$level = $value->baseData;
}
if ($value->fid == 22) {
$title = $value->baseData;
}
if ($value->fid == 17) {
$activity = $value->baseData;
}
if ($value->fid == 21) {
$display = $value->baseData;
}
}
[...]
// It ends at if ($value->fid == 83)
}
So I name my variable like this $title_NUM, $activity_NUM, ..., where _NUM is a number starting at "nothing", it ends at 24 for now, but it could be more if I have more data in my table.
After I get the data I display the html like this :
if ($display == 1) {
echo "<tr bgcolor=\"#eaeaeb\">
<td valign=\"top\">".$repere."</td>
<td align=\"top\">".$level."</td>
<td valign=\"top\"><a data-lightbox=\"width:600;type:iframe;\" href=\"LINK\">".$title."</a></td>
<td align=\"top\">".$activity."</td>
</tr>";
}
And the same happens here I'm displaying each linke of the html "by hand" , O don't have any loop to do the job.
Is there a way to do the job with only loops ?
what i understand so far is that you have
$title1 , $title2 , $title3 , ...
you want to do loop for it
see this example
<?php
for($i=0;$i<=8;$i++)//note it start from 0 to 8
${'test'.$i}=5*$i;
$test9=5*9;
echo "let's test <br/>";
echo $test0.'<br/>';
for($i=1;$i<=9;$i++)//note it start from 1 to 9
echo ${'test'.$i}.'<br/>';
?>

Advice on combining two XML feeds together - own3d/twitch.tv

I am working on a small project to display live streams for Dota 2, League of Legends and StarCraft 2 and was using a 3rd party hosted api that had both feeds from Twitch.tv and own3d - I couldn't of been happier, until they shut down their servers!
I tried merging them together with Yahoo pipes but didn't have much luck!
These are the two feeds I am working with:
Twitch.tv/justin.tv - http://api.justin.tv/api/stream/list.xml?category=gaming&limit=15
Own3d - http://api.own3d.tv/live?limit=15
Here is my attempt at combining the two feeds, by just listing them on top of each other. Its a temporary solution but they are not sorted by live viewers (which is something I would like to do) -
The current script I have:
<table class="table">
<thead>
<tr>
<th colspan="4" class="streamheader">Current live Sstreams</th>
</tr>
</thead>
<tbody>
<?php
$xmlFileData2 = file_get_contents("http://api.own3d.tv/live?limit=15");
$xmlData2 = new SimpleXMLElement($xmlFileData2);
foreach($xmlData2->channel->item as $item)
if ($item->misc['game'] == 'Dota 2' OR $item->misc['game'] == 'League of Legends' OR $item->misc['game'] == 'StarCraft II') {
{
$titlelimit = $item->title;
$title = substr($titlelimit,0,20);
echo "<tr><td>";
if ($item->misc['game'] == 'League of Legends')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/lol.jpg' /></td><td>";
elseif ($item->misc['game'] == 'StarCraft II')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/sc2.jpg' /></td><td>";
elseif ($item->misc['game'] == 'Dota 2')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/dota2.jpg' /></td><td>";
else
echo "none";
echo "<a href='";
$link = $item->link;
$findthis ="/www.own3d.tv/live/";
$replacement ="ae/stream/";
echo str_replace ($findthis, $replacement, $link);
echo "'>";
echo $title;
echo "</a></td><td>";
$author = $item->author;
$find ="/rss#own3d.tv/";
$replace ="";
echo preg_replace ($find, $replace, $author);
echo "<td>";
echo $item->misc['viewers'];
echo "</td> </tr>";
}
}
else
{
echo "";
}
$xmlFileData1 = file_get_contents("http://api.justin.tv/api/stream/list.xml?category=gaming&limit=15");
$xmlData1 = new SimpleXMLElement($xmlFileData1);
foreach($xmlData1->stream as $itemtwitch) {
$game = $itemtwitch->meta_game;
$sc2 = "StarCraft II: Wings of Liberty";
if ($itemtwitch->meta_game == 'Dota 2' OR $itemtwitch->meta_game == 'League of Legends' OR $itemtwitch->meta_game == 'StarCraft II: Wings of Liberty') {
{
echo "<tr><td>";
$titlelimittwitch = $itemtwitch->title;
$titlelimittwitch = substr($titlelimittwitch,0,20);
if ($itemtwitch->meta_game == 'League of Legends')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/lol.jpg' /></td><td>";
elseif ($itemtwitch->meta_game == 'StarCraft II: Wings of Liberty')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/sc2.jpg' /></td><td>";
elseif ($itemtwitch->meta_game == 'Dota 2')
echo"<img src='http://localhost/ae/wp-content/themes/ae/img/dota2.jpg' /></td><td>";
else
echo "none";
$data = $itemtwitch->name;
$find ="/live_user_/";
$replace ="";
echo "<a href='";
echo "http://localhost/ae/stream/";
echo preg_replace ($find, $replace, $data);
echo "''>";
echo "$titlelimittwitch";
//print(" - " . $itemtwitch->meta_game . "");
echo "</a></td><td>";
echo preg_replace ($find, $replace, $data);
print("</td><td>" . $itemtwitch->channel_count . "</td></tr>");
}
}
else {
echo "";
}
}
?>
<tr>
<th colspan="4" class="streamheader centered">View all streams</th>
</tr>
</tbody>
</table>
Any guidance or being pointed in the right direction would be fantastic.
Cheers!
Dan, you will need to add both sets of information(own3d/twitch) to the same array, then sort it. Say you add them to an array named $streamArray you would use the following code to then sort them by viewers.
foreach ($streamArray as $key => $row) {
$viewers[$key] = $row['viewers'];
}
array_multisort($viewers, SORT_DESC, SORT_NUMERIC, $streamArray);
I have developed a similar setup at http://halfw.com/streams.php
If you have any questions just let me know!
You can use a library like Streamtastic (disclaimer: I wrote it) to get latest and top viewed streams for both Own3d and Twitch.tv
https://github.com/sergiotapia/Streamtastic
Usage is very simple:
Own3d Examples:
StreamFinder own3dStreamtastic = new StreamFinder(new Own3dStreamRepository());
// Let's find the most viewed streamers in general.
var topStreamers = own3dStreamtastic.FindTopViewedStreams();
foreach (var stream in topStreamers)
{
Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
}
// You can find the top viewed streamers for any game Own3d supports.
// For example, League of Legends.
var topStreamersForLeagueOfLegends = own3dStreamtastic.FindTopViewedStreamsByGame("League+of+Legends");
foreach (var stream in topStreamersForLeagueOfLegends)
{
Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
}
// Or how about League of Legends.
var topStreamersForLol = own3dStreamtastic.FindTopViewedStreamsByGame("League+of+Legends");
foreach (var stream in topStreamersForLol)
{
Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
}
Since you're using PHP, just run this application and save results to the database. Then have your PHP program just fetch results from the database.

Categories