gpt4 book ai didi

PHP Foreach 一个带有返回值的函数并插入到数据库中

转载 作者:行者123 更新时间:2023-11-29 17:57:04 25 4
gpt4 key购买 nike

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

整个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);

最佳答案

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

看来增量逻辑不正确。

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

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

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

关于PHP Foreach 一个带有返回值的函数并插入到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com