<?php // GETTING THE TITLES INTO THE ARRAY$sql = 'SELECT id,content FROM products';$res = mysql_query($sql) or die("FAIL: $sql <br/> " . mysql_error());while ($row = mysql_fetch_assoc($res)){ $string[]=$row;}// SHOWING THE ARRAY OF TITLES//print_r($string);// COMPARISONS USING SIMILAR_TEXT() BUT SEE THE NOTES HERE BEFORE YOU USE IT!// http://php.net/manual/en/function.similar-text.php#109507// COMPARISONS USING SIMILAR_TEXTforeach ($string as $x){ //echo PHP_EOL . "TESTING <b>$x</b> WITH SIMILAR_TEXT()"; // COMPARE TO THE OTHER STRINGS foreach ($string as $y) { $ss = similar_text( $x['content'], $y['content'], $sp); if (number_format($sp,0)>=75 && number_format($sp,0)<100){ $sql="insert into similar(orig,sim,per) values('{$x['id']}','{$y['id']}',".number_format($sp,0).");"; echo '<br>'.$sql; } }}?>
If you use cookies, you won't be able to echo anything out to the browser, because you cannot setcookie after you have sent anything else to the browser.
This below code is completely untested, but the concept is you would:
1. Check if cookie is set, and if it is only retrieve values greater and equal to the value it stopped
2. Add an ORDER BY id so the IDs order smallest to largest if they aren't already so the above works
3. set cookie inside of each foreach x loop
4. delete the cookie at the end
<?php // GETTING THE TITLES INTO THE ARRAY$sql = 'SELECT id,content FROM products';//if there is still a cookie, it didn't finish last time. Only query results with ID's greater than where it stopped.if (isset($_COOKIE["x_id"])) { $sql .= " WHERE id >= ".$_COOKIE["x_id"];}// order by size so the where will work$sql .= " ORDER BY id ASC";$res = mysql_query($sql) or die("FAIL: $sql <br/> " . mysql_error());while ($row = mysql_fetch_assoc($res)){ $string[]=$row;}// SHOWING THE ARRAY OF TITLES//print_r($string);// COMPARISONS USING SIMILAR_TEXT() BUT SEE THE NOTES HERE BEFORE YOU USE IT!// http://php.net/manual/en/function.similar-text.php#109507// COMPARISONS USING SIMILAR_TEXTforeach ($string as $x){//set cookie to the new x id valuesetcookie("x_id", $x['id']); //echo PHP_EOL . "TESTING <b>$x</b> WITH SIMILAR_TEXT()"; // COMPARE TO THE OTHER STRINGS foreach ($string as $y) { $ss = similar_text( $x['content'], $y['content'], $sp); if (number_format($sp,0)>=75 && number_format($sp,0)<100){ $sql="insert into similar(orig,sim,per) values('{$x['id']}','{$y['id']}',".number_format($sp,0).");"; 'echo '<br>'.$sql; } }}//Delete cookie after all donesetcookie ("x_id", "", time() - 3600);?>
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur
If you use cookies, you won't be able to echo anything out to the browser, because you cannot setcookie after you have sent anything else to the browser.
While that is true, there are some alternatives that might be useful. This can buffer the output, so you can be using echo and setcookie() at the same time. http://us2.php.net/manual/en/function.ob-start.php
Another alternative, if you know the order of the data from the SELECT query, might be to use a SELECT query against the output table, and restart the processing at a position in the input table that is based on what has been already done.
Mark Gilbert
Ray is right here, object starts and flushes are a great way of updating headers AND doing out put to the database. I just had a thought here, why track the progress via a cookie? What happens if the users browser crashes, they restart and the cookies have disappeared (perhaps they were browsing in privacy mode)? Then your application is at the mercy of a positional point that no longer exists so it will start again.
In your loop, where you insert into the database, you could also do an update against the existing table you're querying for id's and update the last processed time. That way if the application is ran and half the records were already processed, it could continue to resume through the remaining records without relying on input from the end user. This has an added advantage in that you could then run the script via cron job and the php cli, thus automating the processes.
Just a thought.
Ray Paseur
Agree with ingwa about computing the restart point - I do this sort of thing all the time when I know that my script could be so long running that it might time out. Just make sure to wrap any irrevocable updates (such as across two tables) in a transaction so you can make a sensible restart point.
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
To set a cookie value:
Open in new window
To retrieve a value:
Open in new window
To check the existence of a cookie :
Open in new window