Infinite loop - why? - php

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)

Related

How to cache JSON request URL

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;

php add random array not repeating most recent 5

I'm trying to re-write this code so that it goes with the columns that are going to be user-defined. With this, my challenge consists of
a) Needs to select random starting item from array
b) Select the next random color from the original array that is not equivalent to the most recent items selected based the number: $intNotesColumn + 1
I was thinking a do-while statement nested inside another was appropriate for this but am unsure how to go about this. Here is my code so far:
$metroUIcolors = array( "#A30061", "#8200CC", "008987", "#A05000", "#B85A93", "#C07807", "#E51400", "#297A29" );
$metroUIcolorsLength = count($metroUIcolors);
$intNotesColumn = 3; // This will be user-defined later
// Now I query the SQL database to get my base-code
if ($result->num_rows > 0) {
// output data of each row
echo '<table border=0 valign=top>'
. '<tr>'
. '<td colspan=' . $intNotesColumn . '>' . '<h1>header</h1>' . '</td>'
. '</tr>'
. '<tr>';
$counterRank = 1;
while($row = $result->fetch_assoc()) {
echo "<td bgcolor=" . $metroUIcolors[rand(0, $metroUIcolorsLength - 1)]. ">"
. "<h2>" . $row["username"] . '</h2><br />'
. "<p class='notes'>" . $row["notes"] . "</p>"
. "<p class='footnotes'>"
. "<br />Last Reset: " . $row["lastReset"]
. '<br />Last Update: ' . $row['lastUpdate']
. '<br />SessionID: ' . $row["sessionID"]
. "<br />Counter = " . $counterRank . "</td>". '</p>';
if ($counterRank % $intNotesColumn == 0)
{
echo '</tr><tr>';
}
$counterRank++;
}
echo '</tr></table>';
} else{
echo "No Notes Found in databases";
}
Then, why don't you do it order picking one color at a time. You could use shuffle() so that there will be a different starting color everytime.
<?php
$counterRank = 1;
// shuffle the array
shuffle($metroUIcolors);
while($row = $result->fetch_assoc()) {
$rand_color = $counterRank % $metroUIcolorsLength;
echo "<td bgcolor=" . $metroUIcolors[$rand_color]. ">";
// everything else
$counterRank++;
}
?>
If you insist on doing the way you said, you may create an array $colorCount which has color codes as keys and count as values.
<?php
$counterRank = 1;
$colorCount = array_fill_keys($metroUIcolors, 0);
while($row = $result->fetch_assoc()) {
do {
$rand_color = $metroUIcolors[rand(0, $metroUIcolorsLength - 1)];
} while ($colorCount[$rand_color] > 5);
echo "<td bgcolor=" . $rand_color. ">";
// everything else
$counterRank++;
$colorCount[$rand_color] += 1;
}
?>

How to manipulate query result in PHP to repeat first row

How can i echo the first row of a query result 2 or more times, outside and inside of the while loop?
The following code just echo the first row outside of loop and doesn't allow to echo again inside of loop
case '1':
$db->Query("SELECT titulo_video,subtitulo_video,youtube_id,nome_norma FROM videos");
echo "<h2>" . $db->Row()->nome_norma . "</h2>";
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
break;
I want the data as follows:
<h3>row 1 - colum 1</h3>
<h2>row1 - colum 2</h2>
<p>row 1 - colum 3</p>
<h2>row2 - colum 2</h2>
<p>row 2 - colum 3</p>
<h2>row3 - colum 2</h2>
<p>row 4 - colum 3</p>
You can fiddle with the loops a bit.
if ($row = $db->Row ())
{
echo '<h2>' ...;
do
{
echo '<h3>' ...;
echo '<p>' ...;
} while ($row = $db->Row ());
}
Make use a Sentinel value:
$i = 0;
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
if($i==0){
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
$i++;
}
If you really want to treat the first row differently, handle it entirely outside of the loop, rather than including a conditional in your while loop, it's cleaner to handle it like this:
$first_row = $db->Row();
echo "<h2>" . $first_row->nome_norma . "</h2>";
echo "<h3>" . $first_row->titulo_video . "</h3>";
echo "<h3>" . $first_row->subtitulo_video . "</h3>";
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
break;

elseif statement not working

I have written some script that works great, BUT I have tried to add an elseif statement in which appears to do absolutely nothing, can't figure out where I am going wrong. (Yes I am a PHP newbie, but I am trying! :)
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
}
Everything up the elseif statement works perfectly.
Must be something simple I am missing or misunderstanding!
$i will never be 0 at this location.
You increase it in the if-statement before the test (++$i).
Note that elseif and else if will only be considered exactly the same when using curly brackets.
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
Won't work, change it to:
if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
elseif (++$i == 5) {
break;
}
elseif vs. else if
I see a lot of people discussing this in the thread.
These are both correct and will both work.
The difference being :
else if() {
}
Translates to:
else {
if(){
}
}
And can't be used in code blocks using colons like this:
if:
stuff
else if()://won't work
//stuff
endif;
basic syntax error ? you can't use the non braced short if's when doing an else if
if (++$i == 5) {
break;
} else if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
notice the colour highlighting on this block compared to yours
Here is the working outcome, thanks for all your help!
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
}
if ($i = 0) {
echo "<div class=\"job-ind\">\n";
echo '<img src="img/no-vac.png"/>';
echo "</div>\n";
}
else if (++$i == 5) {
break;
}

How I can simplify this PHP code? too many switch() function

I made listview in jQuery Mobile and I used PHP to feed its contents. It works properly, but the code is too long and most part of it are very similar to each other. Is there any way to simplify the code? Please have a look at the code, then I'll explain what I really need to do:
<ol data-role="listview">
<?php
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
// first column check
switch ($row[1]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
}
// second column check
switch ($row[2]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// third column check
switch ($row[3]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// fourth column check
switch ($row[4]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// fifth column check
switch ($row[5]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// sixth column check
switch ($row[6]) {
case "Behnam":
echo " B ";
break;
case "Tarin":
echo " T ";
break;
default:
echo " N ";
}
echo "</li></a>";
}
?>
</a></li>
</ol>
and the result is:
By using while ($row = mysql_fetch_array($result)){ I can fetch each row of the sql table one by one. But I also need to check the value of each cell(column) as well.
Group the code in a 2 parameter function:
function checkRowValue($row, $checkDefault) {
switch ($row) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
if ($checkDefault)
echo " N . ";
}
}
Invoke as:
<ol data-role="listview">
<?php
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
// first column check
for ($i = 0; $i < 7; $i++)
checkRowValue($row[i], $i > 0);
}
This generalizes it by applying a function over all the columns in your row (skipping first) and then string them together with " . ".
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<li><a href=\"#\">";
echo join(' . ', array_map(function($v) {
if ($v == 'Behnam') {
return 'B';
elseif ($v == 'Tarin') {
return 'T';
else {
return 'N';
}
}, array_slice($row, 1));
echo "</li></a>";
}
Try This
if(in_array("Behnam",$row) {
echo " B . ";
}
else if(in_array("Train",$row) {
echo " T . ";
}
else {
echo " N . ";
}
This is the final result:
<ul id="competition_list" data-role="listview" data-inset="true" data-theme="a">
<?php
// using the returned value from database to feed the list.
// showing the competiton rounds' outcome.
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
echo $row[0] . "."; //this is the id column in the table and will give me the number of the row
for($i = 1; $i <= 5; $i++){
switch ($row[$i]) {
case "Behnam":
echo " B ";
break;
case "Tarin":
echo " T ";
break;
}
} // end for()
echo "</a></li>";
} // end while()
?>
</ul>
<?php
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
// first column check
foreach($row as r)
{
switch (r) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
}
}
?>

Categories