0

I am a little new to the PHP world and despite my googling and manual reading, I can't

I am currently (ethically!) scraping a website that has a number of different categories (let us call them 'alpha', 'beta', 'gamma').

Currently, I go through these steps:

  1. Load the file
  2. Locate the correct table (php_siomple_dom)
  3. Then go through the elements to grab the information.

    $alpha_one = $element -> find etc;
    $alpha_two = $element -> find etc;
    $alpha_n   = $element -> find etc;
    
    $beta_one  = $element -> find etc;
    $beta _two = $element -> find etc;
    $beta_n    = $element -> find etc;
    
  4. All of the tables are the same for alpha, beta, gamma; thus I would like to write a function, but I am struggling with how to include the argument name in the variables I create. This was my idea, but it does not work.

    function grab($argument) {
    
        $argument . "_one" = $element -> find etc;
        $argument . "_two" = $element -> find etc;
        $argument . "_n"   = $element -> find etc;
    
    }
    

Thus, I could use:

grab('alpha');

grab('beta');

grab('gamma');

without having to write out the code for each table

I've looked into 'magic methods', but cannot swing it into working. I would really appreciate if some PHP wizard could shed some light upon this. Thank youu!

6
  • you mean : grab('alpha'); grab('beta'); grab('gamma'); ?? Commented Jan 7, 2015 at 20:22
  • If you have a situation where you're numbering a series of variables, then you have a situation that calls for the use of an array instead Commented Jan 7, 2015 at 20:23
  • @KimAlexander yes, I did. I will update the question. Thank you. Commented Jan 7, 2015 at 20:27
  • @MarkBaker they actually aren't numbers, they are categories, but the hope of simplicity, I used numbers instead of my confusing category names. Thanks for the reply, though! Commented Jan 7, 2015 at 20:28
  • just simple note: you can do : $myvarname='anyVarNemIWant'; $$myvarname=5; echo $anyVarNameIWant; output: 5 That allows you to use value of string var as a new (or existing) var name Commented Jan 7, 2015 at 20:29

3 Answers 3

1

That code is rather pointless. the $argument . "_one" variable would exist ONLY within the function, and be destroyed when the function returns. You probably want something more like this:

function grab($argument) {
    $data = array();
    $data[$argument . "_one"] = ...
    $data[$argument . "_two"] = ...
    etc...
    return ($data);
}

Essentially: build an array with dynamic key names, based on your $argument, then return the entire array to the calling context.

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

1 Comment

Then $data = grab('alpha'); echo $data['alpha_one'];
0

$argument only exists inside the function scope. It will not have any effect outside this function. If you're using a class you can create all possible variables (not recommended) and call:

$this->{$argument . "_one"} = ...

or store them in an array variable (best way):

$this->myArray[$argument . '_one'] = ...

Comments

0

Just do this:

function grab($argument) {
    ${$argument . "_one"} = $element -> find etc;
    ${$argument . "_two"} = $element -> find etc;
    ${$argument . "_n"}   = $element -> find etc;
}

1 Comment

${$argument . "_one"} :-) fix

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.