在PHP中生成动态切换结构

时间:2017-06-01 13:48:16

标签: php mysql database dynamic dynamically-generated

有没有办法可以创建动态生成的switch语句?我会进一步解释,我有一张桌子上有所有可能的硬币。每个人都有一张带有自己硬币的桌子。如果您点击硬币,则会在php中执行新操作,您将直接进入index.php?actie=Ripple

我的代码:

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;

有没有办法像我一样做:

$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;
}

此代码必须位于index.php

1 个答案:

答案 0 :(得分:0)

这是解决方案

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;
}