10

My PHP code sends queries on Amazon.com to retrieve information about books. When it receives the information, there are two possibilities for execution of the following program. Possibility to define what should be used, it must look at the total number of book profit for research.

What I do at the moment, I am sending a first request and retrieves the total number of results. According to the number of results, I assign a new value to the variable $queryUrl. If the number of results is greater than 1200, the programs to execute as it should.

If the number of results is less than 1200, the program should finish execute the loop to iterate through the entire pages of results and the rest of the php code but only a single time.

At the moment, if there is less than 1200 results. The program goes through all pages of results, but places to stop at the end of PHP code. It executes all the code several times depending on the parameter of the queries is $searchMonthUrlParam inherit the variable recupMonth JavaScript.

For now, I have that

PHP :

//Retrieve variable value passed in POST from JavaScript
$pageNum = (isset($_POST["pageNum"]) && $_POST["pageNum"]) ? intval($_POST["pageNum"]) : 1;
        $writeMode = (isset($_POST["writeMode"]) && $_POST["writeMode"]) ? $_POST["writeMode"] : "w";
        $searchType = (isset($_POST["searchType"]) && $_POST["searchType"]) ? intval($_POST["searchType"]) : 0;
        $month = (isset($_POST["month"]) && $_POST["month"]) ? intval($_POST["month"]) : date("n");
        $year = (isset($_POST["year"]) && $_POST["year"]) ? intval($_POST["year"]) : date("Y") ;
        $keyword = (isset($_POST["keyword"]) && strlen($_POST["keyword"])) ? $_POST["keyword"] : "";
        $startMonth = (isset($_POST["startMonth"]) && strlen($_POST["startMonth"])) ? $_POST["startMonth"] : NULL;
        $startYear = (isset($_POST["startYear"]) && strlen($_POST["startYear"])) ? $_POST["startYear"] : NULL;
        $endMonth = (isset($_POST["endMonth"]) && strlen($_POST["endMonth"])) ? $_POST["endMonth"] : NULL;
        $endYear = (isset($_POST["endYear"]) && strlen($_POST["endYear"])) ? $_POST["endYear"] : NULL;
        if($keyword) {
            if($writeMode === "w") {
                file_put_contents(CSV_FILE, "");
            }

            $searchMonthUrlParam = "&field-datemod=".$month;
            $searchYearUrlParam = "&field-dateyear=".$year;

            $searchTypeUrlParam = "";
            switch($searchType) {
                case SEARCH_TYPE_TITLE:
                    $searchTypeUrlParam = "&field-title=";
                    break;
                case SEARCH_TYPE_KEYWORDS:
                    $searchTypeUrlParam = "&field-keywords=";
                    break;
                case SEARCH_TYPE_AUTHOR:
                    $searchTypeUrlParam = "&field-author=";
                    $searchTypeUrlParam = "&field-publisher=";
                     break;
                case SEARCH_TYPE_PUBLISHER:
                   break;
            }
    //send request to Amazon    
    $queryUrl    = AMAZON_TOTAL_BOOKS_COUNT . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;
                    $queryResult = file_get_contents($queryUrl);
                    //Search number total results
                    if (preg_match('/of\s+([0-9,]+)\s+Results/', $queryResult, $matches)) {
                        $totalResults = (int) str_replace(',', '', $matches[1]);
                    } else {
                        throw new \RuntimeException('Total number of results not found');
                    }
                    //this condition work
                    if ($totalResults > MAX_RESULT_ALL_PAGES) {
                        $queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;
                    }

                    //with this condition I don't know how to proceed
                    else {

                        $queryUrl = AMAZON_TOTAL_BOOKS_COUNT.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;

                }
$htmlResultPage = file_get_html($queryUrl);
$htmlQueryResult = $htmlResultPage->find("div[class=result]");
exit;

JavaScript :

if(processedResultCount === 0) {
                        pageNum = 1;
                        recupMonth--;
                        if(recupMonth === 0 && recupYear > endYear) {
                            recupMonth = 12;
                            recupYear--;
                        }
                        else if(parseInt(recupMonth, 10) === parseInt(endMonth, 10) && parseInt(recupYear, 10) === parseInt(endYear, 10)) {
                            alert("Processing finished");
                            if(totalResultCount != 0) {
                                contentElt.innerHTML = "Total processed results: " + totalResultCount + '<br/><br/>&gt; <a href="amazon_keyword_stats.csv" title="Download CSV result file">Download CSV result file</a>';
                            }
                            return;
                        }
                    }
                getAmazonResult(pageNum, writeMode);
                return;
            }
         }
     }
xmlHttp.open("POST", "ctrl/getAmazonResult.php", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send("pageNum=" + pageNum + "&writeMode=" + writeMode + "&searchType=" + searchType + "&month=" + recupMonth + "&year=" + recupYear + "&keyword=" + keyword + "&startMonth=" + startMonth + "&startYear=" + startYear + "&endMonth=" + endMonth + "&endYear=" + endYear);

Someone would have a solution on how to power stoper php code execution if it goes in the else but he is finished to execute once in full?

mortiped
  • 869
  • 1
  • 6
  • 28
  • It's a thinker alright. Have you thought about using nodejs or something that will return before getting to the end of the script for the initial processing before passing it off to the php? – alexandercannon Jan 29 '14 at 10:25
  • No I do not use nodejs but if the execution of more than 1200 result there already of controllers that are returned before the end of the script. But the problem is that i don't see how do that currently – mortiped Jan 29 '14 at 10:33
  • 2
    i dont understand your need properly? What do you want to do if the result count is below 1200? do you need to query the amazon again or do something else? – Nouphal.M Jan 30 '14 at 04:28
  • if the result is below 1200, I have to execute that there are in the loop and then exit the loop stop ther php script and send signal in JavaScript to say that the queries below 1200 was executed. – mortiped Jan 30 '14 at 06:43
  • 2
    Man, it is a little bit difficult to understand your question. Can you edit to make it more comprehensible? Ask a college to read it and then to explain to you, if he explains the very same thing as you need, then you are good to go, otherwise try to rephrase it again. – Lauro Wolff Valente Sobrinho Feb 03 '14 at 11:50
  • It's little bit more understandable?I totally re-explained the problem to make it easier to understand. – mortiped Feb 03 '14 at 13:15
  • 2
    This seems like a very simple question. The only trouble here is that nobody understands what exactly you are asking. Keep it simple, paste more code. I don't see a loop. I don't see communication between PHP and JS. All I see is an IF fork where you assign different values to `$queryUrl`. I've read your question 4 times, and still don't understand what loop, what "rest of php code", where do you want to exit, and what that has to do with JavaScript at all. – Alex Feb 03 '14 at 17:26
  • I paste more code for show communication between PHP and JS. – mortiped Feb 04 '14 at 06:47

1 Answers1

2

To escape from your loop and end PHP execution, simply return a result:

//this is the condition you indicated works
if ($totalResults > MAX_RESULT_ALL_PAGES) {
    $queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;
    }
//this is the condition you indicated does not work
else {
    return someSortOfResultProcessing($queryResult);
}
clarkatron
  • 529
  • 5
  • 12
  • with return, the queries will be sent a single time? – mortiped Jan 31 '14 at 06:49
  • It depends on the rest of your function or method. I don't know if the code snippet you laid out is in a loop or what. If it is, then yes, it should end the loop, return a result (presumably via AJAX to your javascript?) and stop execution of that function or method. More information would be helpful in addressing your question. – clarkatron Feb 04 '14 at 18:20