for loop keeps timing out, but it looks fine - php

Morning, I have a script tha is calling entries from a database, their history actually, and then displays the entries in a table, right now in order to create the table im using a pretty big for loop to make comparisons and add the results in. However for some reason it is timing out after the 30 seconds(the loop doesnt make that many cycles) i have dtermined that the cause is an issue with the first inner loop due to the fact that the echo statement in there just repeats for the duration of the loop, it never leaves that. any ideas?
for($i = 1; $i <= $FirstCount; $i++)
{
$HistoryTable .= "<tr>";
if($i = 1)
{
for($j = 0; $j < $ThirdCount; $j++)
{
if($EntryTwo[0][$j+1] == $EntryOne[$j])
{
$HistoryTable .= "<td></td>";
}
else
{
$HistoryTable .= "<td>".$EntryTwo[0][$j+1]."</td>";
}
echo $EntryTwo[0][$j+1].' == '.$EntryOne[$j];
}
}
else
{
$first = 0;
$second = 1;
for($k = 1; $k <= $SecondCount; $k++)
{
if($EntryTwo[$first][$k] == $EntryTwo[$second][$k])
{
$HistoryTable .= "<td>".$EntryTwo[$second][$k]."</td>";
}
else
{
$HistoryTable .= "<td></td>";
}
$first++;
$second++;
}
unset($k);
unset($first);
unset($second);
}
$HistoryTable .= "</tr>";
}
variables:
$FirstCount = 4;
$SecondCount = 18
$ThirdCount = 17

if($i = 1) is setting $i to 1 every time, so it's an infinite loop.
What you want is if ($i == 1).

Related

I am trying to get number of occurrences of a charaters in a string

Below is the code i tried, where count is showing improper.
Please help me to get where i am missing the logic.
I am attaching the code which i have tried so far.
PS Note:- I am not intended to use more built in function of php and so I created function for string length.
error_reporting(E_ALL);
$string = "ssddk";
function checkString($addinString, &$stringBK) {
if (empty(count($stringBK))) {
$stringBK[] = $addinString;
return false;
}
foreach ($stringBK as $key => $val) {
if ($addinString == $val) {
return true;
}
}
$stringBK[] = $addinString;
return false;
}
for ($i = 0; $i < checkstrlength($string); $i++) {
$count = 0;
for ($j = 0; $j < checkstrlength($string); $j++) {
if ($string[$i] == $string[$j]) {
if (checkString($string[$i], $stringBK)) {
continue 2;
}
$count++;
echo "Column => " . $string[$j] . " for count" .$count . "<br>";
}
}
}
function checkstrlength($string) {
$count = 0;
for ($i = 0; $string[$i] != ""; $i++) {
$count++;
}
return $count;
}
It gives below output ,
Column => s for count1
Column => d for count1
Column => k for count1
I am expecting it as ,
Column => s for count 2
Column => d for count 2
Column => k for count 1
Ok, there a couple of things to look at here.
The checkstrlength() has the below loop.
for ($i = 0; $string[$i] != ""; $i++) {
Formally speaking, we usually look at \0 terminating character in the string to terminate our loop. But in PHP, everything is a string. So, \0 is now a string to match on rather than a character match. Better, we do an isset check to stop our loop. So, code would look like:
for ($i = 0; isset($string[$i]); $i++) {
Second is your not caching the result which you got from checkstrlength(). Do it. Also, you can start the inner loop from $i itself. There is no need to go from start again. So, for loop would look like:
$length = checkstrlength($string);
for ($i = 0; $i < $length; $i++) {
for ($j = $i; $j < $length; $j++) {
Third is that there is no need of empty and count checks in checkString. This also reduces inbuilt function calls. You can simply loop over and return true if found. If not found, we are adding it anyway. So it would look like:
function checkString($addinString, &$stringBK) {
foreach ($stringBK as $key => $val) {
if ($addinString == $val) {
return true;
}
}
$stringBK[] = $addinString;
return false;
}
Now, in your nested loop, you add it to $stringBK outside of the inner loop, because there is no point in checking with the inner loop when chars match. This is because if some character was visited, why initialize the inner loop at all. Just have a check above and continue the search and count. Also note that you are having echo statements inside the inner loop which doesn't make sense because we haven't finished the count yet. Let's do and print it outside of the inner loop at the end. Snippet as follows:
for ($i = 0; $i < $length; $i++) {
$count = 0;
if (checkString($string[$i], $stringBK)) {
continue;
}
for ($j = $i; $j < $length; $j++) {
if ($string[$i] == $string[$j]) {
$count++;
}
}
echo "Column => " . $string[$i] . " for count : " .$count,PHP_EOL;
}
Final Code Demo: https://3v4l.org/4dpST

<PHP> How to make echo stay on the page permanently with sessions?

I have code, which is supposed to echo if it's true.
I did this with an if, but every time I run the code it will replace the previously echo'd number.
Can I make it so echo doesn't replace the past echo, but add an additional $i every time I run the code?
$i = $_SESSION["priem"];
$i++;
$aantaldelers = 2;
for ($j = 2; $j < $i; $j++) {
if ($i%$j == 0) {
$aantaldelers++;
}
}
if ($aantaldelers == 2) {
echo "$i, ";
}
$aantaldelers = 2;
$_SESSION["priem"] = $i;
yes you can do it by adding a string in session same as variable
$i = $_SESSION["priem"];
$i++;
$str = isset($_SESSION["str"]) ? $_SESSION["str"] : ''; //my changes
$aantaldelers = 2;
for ($j=2; $j<$i; $j++)
if ($i%$j == 0)
$aantaldelers++;
if ($aantaldelers == 2) {
$str .= "$i, "; //my changes
echo $str; //my changes
}
$aantaldelers = 2;
$_SESSION["priem"] = $i;
$_SESSION["str"] = $str; //my changes

Display result after each loop

In the code below, a url variable is increasing by 1 every time in a while loop. When $i is equal to 1000, the loop will end and $i will be displayed (from 1 all the way to 1000).
How do I display the value of $i after every loop, rather than waiting to the end?
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
echo $i;
}else{
break;
}
$i++;
}
You will need to flush message after each iteration. That way your browser will receive part of the information even if your request/response is still pending.
ob_implicit_flush(true);
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i) && $i < 1000)
{
echo $i;
$i++;
ob_flush();
flush();
}
ob_end_flush();
Move echo $i; outside of the if() statement:
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
// echo $i;
}else{
break;
}
echo $i;
$i++;
}
This scenario is surely better suited for for() loop instead of while()...
PHP happen on the server. To see each value for $i immediately you would need to make the server:
do it's thing -> display that thing -> reload.
you can reload with header() but that is limited to 20 reloads (I think) unless you use header("Refresh:0");
I would make it display by making a stacking condition.
if(isset($diplay)){//2+ times through
$display = $display . $i . "<br>";
}else{//first time through
$diplay = $i."<br>";
}
then put that inside your condition
if($i !== 1000) {
if(isset($diplay)){
$display = $display . $i . "<br>";
}else{
$diplay = $i."<br>";
}
}else{
break;
}
echo $display
echo $display;
Then increment your variable
$i++;
then refresh page and pass the variable at the same time.
header("Refresh:0; url=page.php?i=$i");
then you'll have to add a condition at the beginning that gets $i or assign it if not found.
if(isset($_GET['i'])){//meaning if it's found in the url like so ?i=$i
$i = $_GET['i'];
}else{//first time through
$i = 1;
}
///////////////////putting it all together////////////////////////
if(isset($_GET['i'])){
$i = $_GET['i'];
}else{
$i = 1;
}
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
if(isset($diplay)){
$display = $display . $i . "<br>";
}else{
$diplay = $i."<br>";
}
}else{
break;
}
echo $display;
$i++;
header("Refresh:0; url=page.php?i=$i");
}

Second row from MySql Database

I am pulling rows from a MySql DB, and I want to enter a line break on the 2nd row.
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
}
if ($i % 4 == 0) {
echo '';
}
Is there a way of knowing if it is the 2nd row? Im guessing its something to do with $i?
Sorry if it is a silly question!
You're right. It has something to do with the $i variable.
If you want to enter it the 2nd row only you can evaluate $i==2. Also you could add it every 2nd row by evaluating $i % 2 == 0
This results in the following code:
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
if ($i == 2){ // Or replace the evaluation with $i % 2 == 0
echo '<br />'; //HTML linebreak
//echo '\n'; //This is for a newline character.
}
}
if ($i % 4 == 0) {
echo '';
}
This should do it:
for ($i = 1; $i <= mysql_num_rows($result); $i++) {
$row = mysql_fetch_array($result);
$status = $row['status'];
echo "$status";
if ($i == 2) {
echo "\n"; //line break
}
}
if ($i % 4 == 0) {
echo '';
}
If you want an HTML line break, use:
echo "<br/>";
Your indenting might lead you to think the $i mod 4 is inside of the for loop when in fact it is not.
No questions are silly :)

PHP altering row output based on different column

I have some data outputting via PHP/MySQL. It's simply a list of Chinese characters that corresponds to a certain string.
$i = 0;
while ($i <= $x) {
echo "<div class=\"char-option\">" . $results[$i]['mainchar'] . "</div>";
$i++;
}
I have another row with a character ranking in it, about 20% of the characters have a ranking. I would like to:
Change the class to "char-option char-common" for the character with the highest ranking.
Don't change the class if none of the characters have a ranking.
Thanks in advance.
Step 1: Determine, WHICH character has the highest rank.
$high = -1, $hrank = 0;
for($i = 0; $i <= $x; ++$i) {
if(isset($results[$i]['rank']) && $hrank < $results[$i]['rank']) {
$high = $i;
$hrank = $results[$i]['rank'];
}
}
Step 2: With the information, which has the highest character ranking, we can determine, who gets the normal class and who gets the additional class.
for($i = 0; $i <= $x; ++$i) {
$class = "char-option"; //Default class
if($i == $high) $class .= " char-common"; //Adds the second class for the highest ranked character.
echo "<div class=\"$class\">" . $results[$i]['mainchar'] . "</div>";
}
The -1 in $high makes sure, that the class will not be changed, unless there is a ranking somewhere within the list.
set your ranking in the result and do in this way.
$i = 0;
while ($i <= $x) {
if ($results[$i]['highest_ranking'] == 1) {
$class = "char-option char-common";
} else {
$class = "char-option";
}
echo "<div class=\"$class\">" . $results[$i]['mainchar'] . "</div>";
$i++;
}

Categories