坚持阵列

时间:2018-01-29 16:16:34

标签: php arrays for-loop mysqli foreach

您好我正在建立一个网站,人们可以查看cyrptocurrencies的价格。我想要观看的硬币是数组,因此它将逐个读取并将其放入URL中。

我现在要做的是从数据库中获取我想要的所有硬币并将其逐一放入URL中。我自己已经尝试了一些东西,但效果不好,而且我经常遇到错误或不正确的结果。

$ array是手动变量,上面的数字是数据库中的硬币

这是我的代码:

require_once('config.php');

$query = "SELECT * FROM targets";
$result = mysqli_query($mysqli, $query);
$datas = array(); 
if (mysqli_num_rows($result) > 0) {
  while ($row = mysqli_fetch_assoc($result)) {
    $datas[] = $row;
  }
}


foreach ($datas as $data) {
  $test = "'".$data['coin']."', ";
}



$array = array('vertcoin', 'spectrecoin', 'carvertical');

foreach ($array as $coins) { 
  $url = "https://api.coinmarketcap.com/v1/ticker/".$coins."/";

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $data = curl_exec($ch);

  curl_close($ch);

  $parsed_json = json_decode($data);

  $name = $parsed_json['0']->name;
  $symbol = $parsed_json['0']->symbol;
  $price_usd = $parsed_json['0']->price_usd;
  $price_btc = $parsed_json['0']->price_btc;
  $percent_change_1h = $parsed_json['0']->percent_change_1h;
  $percent_change_24h = $parsed_json['0']->percent_change_24h;
  $percent_change_7d = $parsed_json['0']->percent_change_7d;

  echo "<br><br>name: ". $name ."<br>";
  echo "symbol: ". $symbol ."<br>";
  echo "price_usd: ". $price_usd ."<br>";
  echo "price_btc: ". $price_btc ."<br>";
  echo "1h change: ". $percent_change_1h ."<br>";
  echo "24h change: ". $percent_change_24h ."<br>";
  echo "7d change: ". $percent_change_7d ."<br><br>";

} 

2 个答案:

答案 0 :(得分:0)

好像你每次都要覆盖$test值。

我不明白哪个变量是什么,但是如果我没有错,下面的语句是你试图连接你的值的地方:

foreach ($datas as $data) {
  if($test){
      //when $test is not null remember the value.
      $test = $test."'".$data['coin']."', ";
  }else{
      //Otherwise set $test
      $test = "'".$data['coin']."', ";
  }
}

答案 1 :(得分:0)

假设下表:

CREATE TABLE `targets` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `coin` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

有了这些数据:

INSERT INTO `targets` (`id`, `coin`) VALUES
(1, 'vertcoin'),
(2, 'spectrecoin'),
(3, 'carvertical');

此代码:

require_once 'config.php';

$query = "SELECT * FROM targets";
$result = mysqli_query($mysqli, $query);
$coinList = array(); 
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $coinList[] = $row['coin'];
    }
}

//print_r($datas);
foreach ($coinList as $coinItem) { 
    $url = "https://api.coinmarketcap.com/v1/ticker/".$coinItem."/";
    echo sprintf('URL for coin '%s' is %s', $coinItem, $url);
    // Rest of code goes here.
    // curl etc.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $data = curl_exec($ch);
    $parsed_json = json_decode($data);
    $name = $parsed_json['0']->name;
    $symbol = $parsed_json['0']->symbol;
    $price_usd = $parsed_json['0']->price_usd;
    $price_btc = $parsed_json['0']->price_btc;
    $percent_change_1h = $parsed_json['0']->percent_change_1h;
    $percent_change_24h = $parsed_json['0']->percent_change_24h;
    $percent_change_7d = $parsed_json['0']->percent_change_7d;

    echo "<br><br>name: ". $name ."<br>";
    echo "symbol: ". $symbol ."<br>";
    echo "price_usd: ". $price_usd ."<br>";
    echo "price_btc: ". $price_btc ."<br>";
    echo "1h change: ". $percent_change_1h ."<br>";
    echo "24h change: ". $percent_change_24h ."<br>";
    echo "7d change: ". $percent_change_7d ."<br><br>";
}

生成此输出:

URL for coin vertcoin is https://api.coinmarketcap.com/v1/ticker/vertcoin/


name: Vertcoin
symbol: VTC
price_usd: 4.53398
price_btc: 0.00040362
1h change: -0.11
24h change: -2.77
7d change: 6.88

URL for coin spectrecoin is https://api.coinmarketcap.com/v1/ticker/spectrecoin/


name: Spectrecoin
symbol: XSPEC
price_usd: 2.93546
price_btc: 0.00026103
1h change: 0.01
24h change: -3.42
7d change: 14.64

URL for coin carvertical is https://api.coinmarketcap.com/v1/ticker/carvertical/


name: carVertical
symbol: CV
price_usd: 0.011548
price_btc: 0.00000103
1h change: 3.75
24h change: -8.44
7d change: