Avatar of rgb192
rgb192
Flag for United States of America asked on

only solve this with a cookie

<?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_TEXT



foreach ($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;
        }
    }
}
?>

Open in new window



this program takes hours (almost a day to run) because I am comparing many large product descriptions

I read
http://php.net/manual/en/function.setcookie.php
and other php.net pages and do not understand how I can implement cookie for this script
and how I can resume the foreach loop

I am not looking for a cpu answer or to change the code or use another database or scripting language or to write to a database or write to a file

want to put a cookie so I know $x['id']
so if browser crashes I can continue comparison
PHP

Avatar of undefined
Last Comment
rgb192

8/22/2022 - Mon
Mark Gilbert

Try this:

To set a cookie value:
setcookie("myVar","myValue",time()+3600);

Open in new window


To retrieve a value:
$myvar = $_COOKIE['myVar'];

Open in new window


To check the existence of a cookie :

if (isset($_COOKIE['myVar']){ echo "a cookie myVar was found"; }

Open in new window

Matthew Kelly

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_TEXT



foreach ($string as $x)
{
//set cookie to the new x id value
setcookie("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 done
setcookie ("x_id", "", time() - 3600);
?>

Open in new window

rgb192

ASKER
Only want to set x.
Your sets x and y
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
rgb192

ASKER
>>Agree with ingwa about computing the restart point

so I do,

how is this done


I do not think that matthewstevenkelly computed restore point for both x and y
ASKER CERTIFIED SOLUTION
Mark Gilbert

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
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.
ask a question
rgb192

ASKER
if script stops and x stops is on the last id:

want:
x to be compared y1,y2,y3,y4,y5,y6,y7....y4000

but your script:
x will only be compared y4000
rgb192

ASKER
if script stops and x stops is on the last id:

want:
x to be compared y1,y2,y3,y4,y5,y6,y7....y4000

but your script:
x will only be compared y4000
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
rgb192

ASKER
this is the closest example to keeping track of the rows that have been created
thank you