PHP do-while loop filesize check and stop [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So the problem is I'm having is looping to check the file size, for example a file A.txt is 10 bytes and with other php scripts A.txt changes to 20bytes so when the A.txt file is changed I want the iteration to stop.
<?php
$i = 1;
$a = filesize("A.txt");
do {
$c = filesize("A.txt");
if ($a >= $c) {
} else {
echo file_get_contents('chat.log');
$i++;
}
} while ($i <= 2);
?>
What is my mistake?

It seems like you want to implement long-polling for a chat application.
This approach could lead you to lose some chat messages being delivered. And this is not very scalable.
I would suggest you to receive the previously known file size via a GET parameter, and check if the file size is larger than that.
Then I would suggest you to use fopen to open the file and read from the previous pointer (GET parameter) to the end of file, than reading the whole file every time.
you may also want to clearstatcache before each filesize, and add a usleep(100); inside the while loop.

Related

PHP string larger than 2048 charatcter turns tu null [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In contradiction with the documentation, in the following cycle (I am creating a select) when the variable $retval reaches 2048 characters, it's set to null.
foreach ($cache_ecmcategories as $category) {
$retval.= '<option value="'.$category['rowid'].'"';
if (($isfilter ? $category['label'] : $category['rowid']) == $defaulttx)
$retval.= ' selected="selected"';
$retval.= '>'.$category['label'].'</option>'; <== This line generate the problem
}
Also, change the PHP version from 5.6.25 to 7.0.10, the problem remains the same
What is the cause of this strange behavior?
I can't reproduce your issue on my server with the following script:
<?php
$foo='';
for($i=0; $i < 1000; $i++) $foo .= "cur:$i ";
var_dump($foo);
Even an additional $foo .= NULL; doesn't trigger the bug for me.
Are you really sure that the bug happens on your posted code? Have you tried to add additional debug statements?

rand() function that should not repeat [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a code that present 4 pictures from a folder.
I use rand() function in order to present each time different picture for each one of these pictures, but sometimes, I receive the same picture in the same page.
what should I do in order to not receive the same picture twice?
I'd save all possible images in an array and shuffle it, something like this:
$images = array('1.png','2.png','3.png','4.png');
shuffle($images);
foreach ($images as $image) {
// do something with the image
}
I have make an function to do this. Use the code below
<?php
function GenRandom($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
return array_slice($numbers, 0, $quantity);
}
foreach(GenRandom(1,4,4) as $test){
echo $test.".png<br>"; // generates unique id everytime
}
?>
Hope this helps you
Generate the array with all the pictures not randomized.
Random the whole array.
If needed save the randomized array in db (or somewhere else).
When showing a picture get the first value from the array to get random name.
Remove first value from the array.
Repeat points 4-5 on each request.
If array is empty go to point 1.
Do you start with all images in an array?
If so, I would personally use shuffle($pictures);
Then perhaps array_chunk($pictures, 4) to get groups of 4.

Split file every n lines [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I’m working with the royal mail PAF database in csv format (approx 29 million lines), and need to split the data into files of approx 1000 lines.
I found this solution to write the files, but do not know how to a. open the file, and b. tell the script to delete the lines from the original file after copying them.
Can anyone advise?
Does it need to be in PHP? If you're on a Unix/Linux system, you can use the split command.
split --lines=1000 mybigfile.csv
http://en.wikipedia.org/wiki/Split_%28Unix%29
I don't know the royal PAF database, but you open files with fopen(), read a line with fgets() and delete files with unlink().
Your found solution shows the idea of splitting every 1000 lines, but in your case there is no need for calling any csv function at all. It's just a simple "copy each 1000 lines into new file".
$bigFile = fopen("paf.csv", "r");
$j = 0;
while(! feof($bigFile)) {
$smallFile = fopen("small$j.csv", "w");
$j++;
for ($i = 0; $i < 1000 && ! feof($bigFile); $i++) {
fwrite($smallFile, fgets($bigFile));
}
fclose($smallFile);
}
fclose($bigFile);
unlink("paf.csv");

Loop skipping in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How do I skip a loop and go to the next one? Lets say my query in a loop returns result 'Nothing', I want to skip updating the base and go to the next loop. If I have 7 loops and my 5th loop returns 'Nothing' from my base, skip it don't do nothing beyond that point and just go to the next, 6th loop.
I have a for loop like so:
for((int) $i = 0; $i < $number_of_updates; $i++)
{
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
if ($name = 'Nothing')
{
go to next loop;
}
updating my database and stuff;
}
EDIT: I have a $_SESSION variable at the end that stores all of my loops and displays them. I also don't want results with 'Nothing' to be stored there.
continue will end the current iteration and start the next one:
if ($name == 'Nothing')
// -------^^ Make sure you're using double (==).
{
continue;
}

pass variable from javascript function to php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have javascript function and it returns a sum variable. But i couldnt reach it from php code.
my function is :
function add()
{
var sum = 0; // sum initially equals to zero.
var newNumber = 0; // Since textfields are initially text format, I convert them into integer and equalled to newNumber variable.
if(document.RodeoForm.checkbox.checked == true) // If checkbox changed to true,
{
for(var i=0;i<<?php echo $_SESSION['us']; ?>;i++) // Since we have 3 numbers, loop will work 3 times.
{
newNumber = parseInt(document.getElementById("fiyat"+i).value); // Take the number from field and convert it into an integer.
sum += newNumber; // Add the numbers into each other.
}
}
document.RodeoForm.tf.value = sum; // Print the sum onto the screen.
}
JavaScript runs on client side, whereas PHP runs on server side. They can't communicate directly with each other.
Once the page is rendered (and sent to the user), all PHP code was executed and is no longer visible in the source. The JavaScript however is sent along with the HTML and will be executed in the browser of the client. If you want PHP to be aware of a value generated by JavaScript, you have to manually send the data using AJAX.
You can only reach PHP from JavaScript using AJAX. Google how to do AJAX calls from Javascript.
Make sure you have session_start(); to the beginning of that php file, or in the php file which includes that code.
You can add PHP into Javascript but not the other way around because PHP is server side, so it can echo information into client slide.
Depending on what you're using this for , you can use AJAX to send the information to a PHP page.

Categories