How to cache JSON request URL - php

Tried to cache request URL with no hope
Down here is my JSON request. I want to cache the JSON Result for 1 hour on Server Side.
Store my JSON data in a .json file on my PHP server
Cache the result or at least save it in a session so it's not repeated on each page load.
<?php
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->GetTickerJSONResult)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->GetTickerJSONResult as $idx => $GetTickerJSONResult) {
// Output a row
$avgPerChangeValue = floatval($GetTickerJSONResult->AvgPerChange);
$symbolName = $GetTickerJSONResult->Symbol ;
$hrefLink = "";
switch ($symbolName) {
case "ALK":
$hrefLink = "https://www.mse.mk/mk/issuer/alkaloid-ad-skopje";
break;
case "GRNT":
$hrefLink = "https://www.mse.mk/mk/issuer/granit-ad-skopje" ;
break;
case "KMB":
$hrefLink = "https://www.mse.mk/mk/issuer/komercijalna-banka-ad-skopje" ;
break;
case "MPT":
$hrefLink = "https://www.mse.mk/mk/issuer/makpetrol-ad-skopje" ;
break;
case "MTUR":
$hrefLink = "https://www.mse.mk/mk/issuer/makedonijaturist-ad-skopje" ;
break;
case "SBT":
$hrefLink = "https://www.mse.mk/mk/issuer/stopanska-banka-ad-bitola" ;
break;
case "TEL":
$hrefLink = "https://www.mse.mk/mk/issuer/makedonski-telekom-ad-%E2%80%93-skopje" ;
break;
case "TNB":
$hrefLink = "https://www.mse.mk/mk/issuer/nlb-banka-ad-skopje" ;
break;
case "TTK":
$hrefLink = "https://www.mse.mk/mk/issuer/ttk-banka-ad-skopje" ;
break;
default:
echo "Does not supported symbol";
}
if ($avgPerChangeValue > 0 ) {
echo "<tr>" ;
echo "<td class='bold-green'>" . "<a href='" . $hrefLink . "'>" . $GetTickerJSONResult->Symbol . "</a></td>";
echo "<td class='bold-green'>$GetTickerJSONResult->AvgPrice</td>";
echo "<td class='bold-green'>$GetTickerJSONResult->AvgPerChange</td>";
echo "</tr>";
} else if ($avgPerChangeValue < 0 ){
echo "<td class='bold-red'>" . "<a href='" . $hrefLink . "'>" . $GetTickerJSONResult->Symbol . "</a></td>";
echo "<td class='bold-red'>$GetTickerJSONResult->AvgPrice</td>";
echo "<td class='bold-red'>$GetTickerJSONResult->AvgPerChange</td>";
echo "</tr>";
} else {
echo "<td class='bold-blue'>" . "<a href='" . $hrefLink . "'>" . $GetTickerJSONResult->Symbol . "</a></td>";
echo "<td class='bold-blue'>$GetTickerJSONResult->AvgPrice</td>";
echo "<td class='bold-blue'>$GetTickerJSONResult->AvgPerChange</td>";
echo "</tr>";
}
}
// Close the table
echo "</table>";
}
echo "<style type='text/css'> td.bold-red { color: red; font-weight: bold; } td.bold-green { color: green; font-weight: bold; } td.bold-blue { color: blue; font-weight: bold; } </style>"
?>
Please provide me working solution on my code

try this, stored all json in session and after use another page
session_start();
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$_SESSION['data']=$json;

Related

PHP For Loop jQuery UI Tabs

I am trying to display a list of Links in a table according to Distance and Category. I would like each distance to be a Tab and have the appropriate Links in each Tab. I am trying to accomplish this with A PHP Foreach loop and jQuery-UI Tabs.
Here is the code which gets the data and displays it in the table in each Tab.
The index function in the controller for the View and the function that gets the table data:
public function index() {
$data = array();
$db_name = $this->uri->segment(2);
$this->db->db_select($db_name);
$tables = $this->db->get('tableinfo');
$data['distances'] = array();
$data['tables'] = array(
'men' => array(),
'women' => array()
);
foreach($tables->result() as $row) {
if(!in_array($row->distance, $data['distances'])) {
array_push($data['distances'], $row->distance);
}
if(substr($row->displayname, 0, 4) == "Male") {
array_push($data["tables"]['men'], $row->displayname);
} else {
array_push($data["tables"]['women'], $row->displayname);
}
}
$data['dbname'] = $db_name;
$this->parser->parse('templates/header', $data);
$this->parser->parse('select/index', $data);
$this->parser->parse('templates/footer', $data);
}
public function gettable($table) {
$db_name = "championchipco_sowetomarathon2019";
$this->db->db_select($db_name);
redirect("results/index/" . $db_name . "/" . $table);
}
And the View:
<?php
$i = 1;
echo "<div class='row'>";
echo "<div class='col' id='tabs'>";
echo "<ul>";
foreach($distances as $distance) {
echo "<li><a href='#tabs-" . $i . "'>" . $distance . "</a></li>";
}
echo "</ul>";
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}
echo "</div>";
echo "</div>";
?>
At the moment, all of the data is being displayed in every Tab, instead of only display the Links for the particular category in a different tab. I can see that the 2nd table of Men and Women is slightly to the left of the top one so I think the loop is causing something to go wrong.
I have tried re-arranging the way the loops display the data in the View but cannot seem to get only the 10KM in the 10KM Tab, 21KM in 21KM Tab, etc.
Get the data of your second foreach by Ajax it will made your need simple
I mentioned to remove below foreach
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}

Split table in php

i got this script. it looks into a file and read all the lines and put it nicely in a table.. what i need to do now is to take all the table data and split it into two tables ..
eg. if their is 100 rows.. then instead of one long list, i will get 50 data in one table and the other 50 in the other table...
enter code here<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo "<table border=\"1\">";
for ($count = 1; $count < sizeof($lines); $count++)
{
$fields = explode(",",$lines[$count]);
$sz = sizeof($fields);
if ($sz > 1)
{
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
?>
Use a foreach instead of a for and $count to calculate if it is needed to echo a table tag. $count is only incremented when a row was inserted.
<style>
div.container {
width: 100%; /* Change the width to the desired width */
padding: 0;
}
div.container table {
width: 50%; /* Change the width to half of the width of the div */
margin: 0;
float: left;
}
</style>
<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo '<div class="conatiner">';
$count = 0;
foreach ($lines as $currLine)
{
$fields = explode(",",$currLine);
$sz = sizeof($fields);
if ($sz > 1)
{
if ($count == 0) {
echo "<table border=\"1\">";
}
elseif ($count % 50 == 0) {
echo "</table>";
echo "<table border=\"1\">";
}
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
$count++;
}
}
echo "</table>";
echo '</div>';
}

PHP compare current array value to previous value

I've got an array with images and a date for each one. I'm retrieving the date via array explode. I want to take this date value and compare it to the previous date value for the previous image. My goal here is to display these dates as headers for the sets of images that correspond to each different date.
Here is my code I've been fiddling with to try and make this work (sorry for all my slashes):
$p = 0;
$a = -1;
echo "<table>";
echo "<tr>";
foreach (array_combine($images,$locs) as $image => $loc)
{
$p++;
$a++;
$datesort = explode('>',$image);
echo "<style>";
echo "input[label=t" . $p . "] {";
echo "display: none;";
echo "}";
echo "input[label=t" . $p . "] + label {";
echo "border: 5px solid #FFFFFF;";
$image = "background: url('http://blah.com/" . $loctb[$p] . "');";
echo $image;
echo "height: 61px;";
echo "width: 92px;";
echo "display: inline-block;";
echo "padding: 0 0 0 0px;";
echo "}";
echo "input[label=t" . $p . "]:checked + label {";
echo "border: 5px solid #FF9900;";
echo "background: url('http://blah.com/" . $loctb[$p] . "');";
echo "height: 61px;";
echo "width: 92px;";
echo "display: inline-block;";
echo "padding: 0 0 0 0px;";
echo "}";
echo "</style>";
$lastdate = array("");
$lastdate[$a] = $datesort[1];
echo "<td>";
if ($lastdate[$a] <> $datesort[1]){
echo "<h2>";
echo $datesort[1];
echo "</h2>";
}
echo "<input type=\"checkbox\" label=\"t" . $p . "\" id=\"t" . $p . "\" name =\"boxes[]\" value=\"<img src=http://blah.com/" . $loc . "\" />";
echo "<label for=\"t" . $p . "\"></label>";
echo "</div>";
echo "</td>";
if ($p % 2 == 0)
{
echo "</tr>";
}
}
echo "</table>";
I've been trying to store the current date value into another array variable and then compare that to the new value the next time through my foreach loop, but I'm doing it wrong :-/...
I'd appreciate any help you can offer. Thanks
Here is a quick and dirty way, though you'll want to tune it to your needs (I had this issue but it was worse as I was also filling in the blank dates in between):
$previous_dates = array();
foreach (array_combine($images,$locs) as $image => $loc) {
$datesort = explode('>',$image);
if (end($previous_dates) <> $datesort[1]) {
echo "<h2>";
echo $datesort[1];
echo "</h2>";
}
// ... more stuff ...//
$previous_dates[] = $datesort[1];
}
Really I would reconsider the approach you're taking for the explode within the loop, but first-things-first, see if that gets you where you need to be.

Infinite loop - why?

for($i=0; $i<5; $i++) {
switch($i) {
case 0:
echo "<div class=\"darkgrey topdarkgrey\">";
break;
case 1:
case 3:
echo "<div class=\"lightgrey\">";
break;
case 2:
case 4:
echo "<div class=\"darkgrey\">";
break;
case 5:
echo "<div class\"darkgrey bottomdarkgrey\">";
break;
}
if($i=$idagInt)
echo "<div id=\"idag\">" . $dag[$i] . "<br>";
else
echo "<div class=\"dag\"><span class=\"veckoDag\">" . $dag[$i] . "</span><br>";
echo "<span class=\"month\">" . $datumDay[$i] . " " . $month[$i] . "</span></div>";
echo "<div class=\"mat\">" . strip_tags($mat[$i], "<p>") . "</div></div>";
}
This is the code I'm using to print things to the website and after some trouble-shooting my conclusion is that there's something wrong with the switch-statement, but I cant see what?
Here's your problem. You're assigning $i the value of $idagInt rather then comparing it. As a result $i never reaches 5.
if($i=$idagInt)
Change it to:
if($i==$idagInt)

PHP Help: if ($row['cDeviceRegistrationId'] > 0) { $a = 1;} echo $a;

I'm trying to have a "1" printed if there is a value in cDeviceRegistrationId column in the database. Here is the code:
$result is an SQL query
while($row = mysql_fetch_assoc($result))
{
if ($row['cDeviceRegistrationId'] > 0) {
$a = 1;
}
echo "<tr class='forum'>";
echo "<td class='forum'>" . $row['intUserID'] . "</td>";
echo "<td class='forum'>" . $row['cUsername'] . "</td>";
echo "<td class='forum'>" . $row['cEmail'] . "</td>";
echo "<td class='forum'>$a</td>";
echo "<td class='forum'>" . $row['uCreateDate'] . "</td>";
echo "</tr>";
}
The value of $a is not overwritten if it does not meet the condition, meaning other iterations may get the value of 1 incorrectly. Here is a fix (replace your if statement):
$a = ($row['cDeviceRegistrationId'] > 0) ? 1 : '';
How about:
if( !is_null($row['cDeviceRegistrationId']) ){
$a = 1;
}

Categories