【问题标题】:PHP Foreach a function with a return and insert into dbPHP Foreach 一个带有返回值的函数并插入到数据库中
【发布时间】:2018-02-09 15:44:37
【问题描述】:

我创建了一个代码,该代码将来自 unibet 的 XML 文件中的赔率插入数据库,并且运行良好,但我知道它不是插入我设置的 100 场比赛的所有赔率,而是仅插入 2 个赌注它与函数dec2frac 挂钩,因为当我删除它时,它会将每个投注报价插入正确的行,但是我无法计算赔率,然后我需要手动插入它们。

整个 PHP 是

 <?php


$xml=simplexml_load_file("unibetapi") or die("Error: Cannot create object");
$connection = mysqli_connect("localhost", "root", "password", "db") or die ("ERROR: Cannot connect");

/* Assumes that the number of IDs = number of customers */
$size = sizeOf($xml->id);
$i = 5; //index


/* Add each customer to the database, See how we reference it as    $xml->ENTITY[INDEX] */
while($i != $size) 
{

    print_r($xml->participant);
    echo $xml->participant; //Test
    foreach($xml->betoffers as $betoffer) //Extract the Array Values by using Foreach Loop
    {
         //Function to convert from decimels to fraction
    function dec2frac($dec) { 

        $decBase = --$dec; 

        $div = 1; 

        do { 

            $div++; 

            $dec = $decBase * $div; 

        } while (intval($dec) != $dec); 

        if ($dec % $div == 0) { 
            $dec = $dec / $div; 
            $div = $div / $div; 
        } 

        return $dec.' '.$div; 

    } 

        //Label of the bet
        $criteria = ($betoffer->criterion->label);
        //Criteria of the bet - Home,Draw, Away etc
        $crit = ($criteria);  

        //BET Participant
        $string = ($betoffer->outcomes->participant);

        //Convert odds to fracture
        $bet = ($string); 
        $number = ($betoffer->outcomes->odds);
        $format_number = number_format("$number" , 0, ',', '.');
        $decimal = ($format_number);

        //Explode Fracture
        $pizza  = dec2frac($decimal);
        $pieces = explode(" ", $pizza);



        echo $bet;


    //$sql = "INSERT INTO xf_nflj_sportsbook_event (event_id, category_id, user_id, username, title, description, event_status, date_create, date_open, date_close, date_settle, date_edit, event_timezone, wagers_placed, amount_staked, amount_paidout, likes, like_users, view_count, outcome_count, comment_count, thread_id, prefix_id, last_comment_date, limit_wagers_single_outcome) VALUES ('$event->id',2,1,'tipstr', '$event->name', '$event->group', 'open', 1517755596,1517755596,1517761200,1517761200,0,'Europe/London', 0, 0,0,0,0,0,0,0,0,0,0,0)";
    $sql = "INSERT INTO xf_nflj_sportsbook_outcome (`outcome_id`, `event_id`, `outcome_date`, `outcome_date_edited`, `outcome_title`, `outcome_current_odds_against`, `outcome_current_odds_for`, `outcome_max_wagers`, `outcome_min_wager_amount`, `outcome_max_wager_amount`, `outcome_pays`, `outcome_wagers_placed`, `outcome_amount_staked`, `outcome_amount_paidout`, `outcome_settled`, `outcome_date_settled`) VALUES ('$betoffer->id', '$betoffer->eventId', 1518116479, 0, '$bet - $crit ', '$pieces[0]', '$pieces[1]', 0, 10, 0, 'N', 0, 0, '0.00', 'N', 0)";

    mysqli_query($connection, $sql) or die ("ERROR: " .mysqli_error($connection) . " (query was $sql)");

    $i++; //increment index


    }



}


mysqli_close($connection);

【问题讨论】:

  • 只是一个建议,但您应该将函数声明放在循环之外,这样它就不会被不断地重新定义。只需将其放在脚本的顶部即可。
  • 我已经在更新版本中做到了,但它仍然会发生。

标签: php mysql xml


【解决方案1】:

$i 索引移出foreach 块并更改$i 的初始声明

看来增量逻辑不正确。

您必须在foreach 块之外增加索引$i,因为计数器是针对while 块的。

由于它在foreach 循环内递增,因此$i 值会根据betoffers 中存在的元素数多次递增。

另外,您已将 $i 的初始值声明为 5。也将其删除。

【讨论】:

  • 我将 $I++ 从 foreach 中移出,但它仍然只是添加了 2 个项目。有什么想法吗?
  • @Tipstr 一定和你的核心逻辑有关。无论如何,如果我的回答对您有任何帮助,请考虑将其标记为已接受。谢谢。
【解决方案2】:

我发现 unibet 也有骨折,所以我修改了脚本,现在它可以 100% 运行。这是最终结果。

  <?php


$xml=simplexml_load_file("unibet_api_xml") or die("Error: Cannot create object");
$connection = mysqli_connect("localhost", "root", "password", "db") or die ("ERROR: Cannot connect");

/* Assumes that the number of IDs = number of customers */
$size = sizeOf($xml->betOfferResponse);
$i = 2; //index




//Explode fraction



/* Add each customer to the database, See how we reference it as    $xml->ENTITY[INDEX] */
while($i != $size) 
{


    include'defrac.php';
    foreach($xml->betoffers as $betoffer) //Extract the Array Values by using Foreach Loop
    {

    //Convert odds to fraction

      /*  $number = ($betoffer->outcomes->odds);
        $format_number = number_format("$number" , 0, ',', '.');
        $decimal = ($format_number);*/

        $pizza  = ($betoffer->outcomes->oddsFractional);
        $pieces = explode("/", $pizza);
        echo $pieces[0];
        echo $pieces[1];

        //Label of the bet
        $criteria = ($betoffer->criterion->label);
        //Criteria of the bet - Home,Draw, Away etc
        $crit = ($criteria);  

        //BET Participant
        $string = ($betoffer->outcomes->participant);
        $bet = ($string); 






        //$sql = "INSERT INTO xf_nflj_sportsbook_event (event_id, category_id, user_id, username, title, description, event_status, date_create, date_open, date_close, date_settle, date_edit, event_timezone, wagers_placed, amount_staked, amount_paidout, likes, like_users, view_count, outcome_count, comment_count, thread_id, prefix_id, last_comment_date, limit_wagers_single_outcome) VALUES ('$event->id',2,1,'tipstr', '$event->name', '$event->group', 'open', 1517755596,1517755596,1517761200,1517761200,0,'Europe/London', 0, 0,0,0,0,0,0,0,0,0,0,0)";
       $sql = "INSERT INTO xf_nflj_sportsbook_outcome (`outcome_id`, `event_id`, `outcome_date`, `outcome_date_edited`, `outcome_title`, `outcome_current_odds_against`, `outcome_current_odds_for`, `outcome_max_wagers`, `outcome_min_wager_amount`, `outcome_max_wager_amount`, `outcome_pays`, `outcome_wagers_placed`, `outcome_amount_staked`, `outcome_amount_paidout`, `outcome_settled`, `outcome_date_settled`) VALUES ('$betoffer->id', '$betoffer->eventId', 1518116479, 0, '$bet - $crit ', '$pieces[0]', '$pieces[1]', 0, 10, 0, 'N', 0, 0, '0.00', 'N', 0)";

        mysqli_query($connection, $sql) or die ("ERROR: " .mysqli_error($connection) . " (query was $sql)");




    }

    $i++; //increment index

}


mysqli_close($connection);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多