0

I am posting a text area to my php script where I then do a loop for each line of the text area and then using the data from the text area loop another time. I am then trying to set an array for each one and at the end retrieve an array for all of them. However my code is giving me errors:

if (isset($_POST['submit'])) {
        $entries = array();
    $text    = trim($_POST['facebookpage']);
    $text    = explode("\n", $text);
    foreach ($text as $line) {
        $data = $html2->find("table.profileInfoTable");
        $text2 = trim($data[0]);
        $text2 = explode("<tr>", $text2);
        foreach ($text2 as $line) {
            if (strpos($line, 'Location') !== false) {
                $location = $line;
            }
        }
        $data1 = $html2->find("table.profileInfoTable");
        $text2 = trim($data1[0]);
        $text2 = explode("<tr>", $text2);
        foreach ($text2 as $line) {
            if (strpos($line, 'Email') !== false) {
                $email = $line;
            }
        }
    $mainarray = array("Email" => $email, "Location" => $location);
    array_push(($mainarray),$entries);
    }
    var_dump($entries);
}`

Also the error is:

Fatal error: Only variables can be passed by reference in /home2/statonme/public_html/scraper.php on line 61
0

2 Answers 2

1

Usually, the error messages of PHP give you a very good idea of what's wrong with your script.

I don't know where 'Line 61' in your script is, but I believe that if you put anything into brackets, it is considered an 'equation'...meaning that ($main_array) is not a variable...and you can't change an equation. You can only change variables.

Try removing the brackets around $main_array in array_push(($mainarray),$entries); (making it: array_push($mainarray, $entries);) and come back with results please.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply, that has made it do something however the browser window is just stuck loading now. I think it did this earlier when I was trying to write to an array.
Please go to: simonstaton.co.uk/scraper.php and type in facebook.com/MauiNuiBotanicalGardens into the form then click submit and see what it does
@SimonStaton I believe this happens, because you're having an infinite loop in your previous code. If memory serves, PHP first parses the entire script to learn what it has to do and alerts you of obvious errors...stuff it doesn't know what to do with. Your code is now syntactically correct, but that doesn't mean that your code is flawless.
I see I have actually just run the script on my xampp server and it ran fine :) however the array is empty which is very confusing, am I writing to the array correct? I want the array to have a new level for each of the loops
@SimonStaton You should put some echoes in your code, to see what it does where. How often it iterates through the loops, how the variables change, etc. I'm afraid I cannot help you with this issue without the complete script.
1

I think you have the arguments to array_push backwards. The array you're adding to should be the first argument, the rest of the arguments are the elements being added. So it should be:

array_push($entries, $mainarray);

Or, more simply:

$entries[] = array("Email" => $email, "Location" => $location);

1 Comment

Ah, completely missed that. I was so occupied with the error message, that I didn't look at the greater picture. For that +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.