PHP Foreach一个返回并插入db

时间:2018-02-09 15:44:38

标签: php mysql xml

我已经创建了一个代码,可以从unibet的XML文件中将odds插入到数据库中,并且它工作得很好,但它不是插入我设置的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);

2 个答案:

答案 0 :(得分:0)

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

似乎增量逻辑不正确。

您必须在$i块之外增加索引foreach,因为计数器适用于while块。

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

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

答案 1 :(得分:0)

我发现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);