Ricardo Ricardo - 1 month ago 9
PHP Question

Minimize efforts with foreach

I'm trying to build a script that will download users from a db table and attach a new random IP to each user based on his state.

The problem is that I wrote a lot of code and there is still much Copy/Paste job to be done if I keep it with this approach.

Can someone point me to the right direction on how to properly do that?

So first I have 50 of these:

$California_Text = file_get_contents('state/California.txt');
$California_textArray = explode("\n", $California_Text);
$Idaho_Text = file_get_contents('state/Idaho.txt');
$Idaho_textArray = explode("\n", $Idaho_Text);
$Illinois_Text = file_get_contents('state/Illinois.txt');
$Illinois_textArray = explode("\n", $Illinois_Text);
$Indiana_Text = file_get_contents('state/Illinois.txt');
$Indiana_textArray = explode("\n", $Indiana_Text);
$Iowa_Text = file_get_contents('state/Iowa.txt');


Then I have 50 of these:

while($row = $result->fetch_assoc()) {

if (isset($row["state"])) {

foreach ($row as $value){
$California_randArrayIndexNum = array_rand($California_textArray);
$p_California = $California_textArray[$California_randArrayIndexNum];

$Texas_randArrayIndexNum = array_rand($Texas_textArray);
$p_Texas = $Texas_textArray[$Texas_randArrayIndexNum];

$Alabama_randArrayIndexNum = array_rand($Alabama_textArray);
$p_Alabama = $Alabama_textArray[$Alabama_randArrayIndexNum];

$Alaska_randArrayIndexNum = array_rand($Alaska_textArray);
$p_Alaska = $Texas_textArray[$Alaska_randArrayIndexNum];

$Arizona_randArrayIndexNum = array_rand($Arizona_textArray);
$p_Arizona = $California_textArray[$Arizona_randArrayIndexNum];
.....


Then I have 50 of these:

if ($row["state"] == "california") {
$stateip = $p_California;
}
else if ($row["state"] == "texas") {
$stateip = $p_Texas;
}
else if ($row["state"] == "alabama") {
$stateip = $p_Alabama;
}
else if ($row["state"] == "alaska") {
$stateip = $p_Alaska;
}


I'm pretty much sure that it's a bad approach.. Maybe there's a way to do all this with like 3 lines of
foreach
?

Answer

Something like this:

// holds your content
$state_content = [];

while($row = $result->fetch_assoc()) {

    // check do we have state set
    if (!empty($row["state"])) {
        $stateip = getStateIpByName($row["state"]);
    }
}

/**
 *  Returns random IP
 */
function getStateIpByName($state_name) {
    $content = getStateContent($state_name);

    return $content[array_rand($content)];
}

/**
 * Returns your's state content by state name
 */
function getStateContent($state_name) {

    // checks do we already have content for this state
    if(!isset($state_content[$state_name])) {

        // generate file name
        $file_name = "state/";
        $file_name .= str_replace(" ", "", ucwords($state_name));
        $file_name .= ".txt";

        $state_text = file_get_contents($file_name);
        $state_content[$state_name] = explode("\n", $state_text);
    }

    return $state_content[$state_name];
}

There are probably some errors but you will get idea.

Comments