简体   繁体   中英

Generate a dynamic switch structure in PHP

Is there a way you can create a dynamic generated switch statement? I will explain further, I have a table with all the possible coins. Every person has a table with their own coins. If you click on a coin a new action will happen in the php and you will be direct for example to index.php?actie=Ripple .

My code:

case "Ripple":
    if ($_SESSION["name"] == "d"){
        $dataFromTransactions = $pol->toonAllesD("XRP/BTC");
    }
    else{
        $dataFromTransactions = $pol->toonAlles("XRP/BTC");
    }
    Uitvoer::toonDeRippleTable($dataFromTransactions,"Ripple");
    break;
case "LiteCoin":
    if ($_SESSION["name"] == "d"){
        $dataFromTransactions = $pol->toonAllesD("LTC/BTC");
    }
    else{
        $dataFromTransactions = $pol->toonAlles("LTC/BTC");
    }
    Uitvoer::toonDeRippleTable($dataFromTransactions,"LiteCoin");
    break;

Is there a way that I need to do like a:

$alleCoins = $pol->getAlleCoinsYouBuyed()
foreach($alleCoins as $coinInfo){
case $coinInfo->Coinname :
if ($_SESSION["name"] == "d"){
        $dataFromTransactions = $pol->toonAllesD($coinInfo->Market);
    }
    else{
        $dataFromTransactions = $pol->toonAlles($coinInfo->Market);
    }
    Uitvoer::toonDeRippleTable($dataFromTransactions,$coinInfo->Coinname);
    break;
}

this code must be in the index.php

This is the solution

if ($_SESSION["name"] == "d"){
    $alleCoins = dataBaseTrade::getPoloniexInstantie()->getAlleCoinsYouBuyedD();
}
else {
    $alleCoins = dataBaseTrade::getPoloniexInstantie()->getAlleCoinsYouBuyed();
}
$searchNameArray = json_decode(json_encode($alleCoins), true);
$searchNameArray = array_column($searchNameArray, "Coinname" );
switch ($actie){
    case ($actie != "home" || "user"):
        $valueOfThePlaceInTheArray = array_search($actie,$searchNameArray);
        $thisCoin = $alleCoins[$valueOfThePlaceInTheArray];
        if ($_SESSION["name"] == "d"){
            $dataFromTransactions = $pol->toonAllesD($thisCoin->Market);
        }
        else{
            $dataFromTransactions = $pol->toonAlles($thisCoin->Market);
        }
        Uitvoer::toonDeRippleTable($dataFromTransactions,$thisCoin->Coinname);
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM