我使用以下数组:
$name = array("Veldspar","Scordite","Pyroxeres","Plagioclase","Omber","Kernite","Jaspet","Hemorphite","Hedbergite","Gneiss","Dark Ochre","Crokite","Spodumain","Bistot","Arkanor","Mercoxit");
$typeids= array(1230,1228,1224,18,1227,20,1226,1231,21,1229,1232,1225,19,1223,22,11396);
$url="http://api.eve-central.com/api/marketstat?usesystem=30000142&typeid=".join('&typeid=',$typeids);
$pricexml=file_get_contents($url);
$xml=new SimpleXMLElement($pricexml);
foreach($typeids as $typeid)
{
$item=$xml->xpath('/evec_api/marketstat/type[@id='.$typeid.']');
$price= (float) $item[0]->buy->max;
$price=round($price,2);
$query1 = "INSERT INTO data (Price) VALUES ('$price');";
$q1 = mysqli_query($conn,$query1) or die ('Error posting data');
echo $typeids[$index].$name[$index].$price[$index]."\n";
}
foreach($typeids as $index => $value)
{
$query = "INSERT INTO data (typeID, Name) VALUES ('$typeids[$index]','$name[$index]');";
$q = mysqli_query($conn,$query) or die ('Error posting data');
echo $typeids[$index].$name[$index]."\n";
}
它是我玩的游戏,Eve在线。我想要完成的是获取项目的价格/ itemID /名称并将其注入我的数据库。我可以在我的2个foreach循环之间获得所有3个项目。但是,当我把它放在数据库中时,它创建了2组行。
+--------+-------------+-------+
| typeID | Name | Price |
+--------+-------------+-------+
| 0 | | 15 |
| 0 | | 27 |
| 0 | | 55 |
| 0 | | 58 |
| 0 | | 91 |
| 0 | | 227 |
| 0 | | 434 |
| 0 | | 740 |
| 0 | | 708 |
| 0 | | 914 |
| 0 | | 1505 |
| 0 | | 3202 |
| 0 | | 1600 |
| 0 | | 2900 |
| 0 | | 3180 |
| 0 | | 11800 |
| 1230 | Veldspar | 0 |
| 1228 | Scordite | 0 |
| 1224 | Pyroxeres | 0 |
| 18 | Plagioclase | 0 |
| 1227 | Omber | 0 |
| 20 | Kernite | 0 |
| 1226 | Jaspet | 0 |
| 1231 | Hemorphite | 0 |
| 21 | Hedbergite | 0 |
| 1229 | Gneiss | 0 |
| 1232 | Dark Ochre | 0 |
| 1225 | Crokite | 0 |
| 19 | Spodumain | 0 |
| 1223 | Bistot | 0 |
| 22 | Arkanor | 0 |
| 11396 | Mercoxit | 0
我已经尝试将所有3个内容合并到1个foreach循环中,但它并没有尊重API调用来获取价格,这就是为什么我产生了2个不同的foreach循环。
我当前的解决方案是将它组合在一起,是否有某种SQL命令我可以为价格foreach循环运行它只是将它添加到顶行并向下运行?
答案 0 :(得分:1)
我认为这应该在一个循环中完成:
foreach($typeids as $index => $typeid)
{
$item=$xml->xpath('/evec_api/marketstat/type[@id='.$typeid.']');
$price= (float) $item[0]->buy->max;
$price=round($price,2);
$query1 = "INSERT INTO data (typeID, Name, Price) VALUES ('$typeid', '$name[$index]', '$price');";
$q1 = mysqli_query($conn,$query1) or die ('Error posting data');
echo $typeids[$index].$name[$index].$price[$index]."\n";
}