【问题标题】:Fetch each user and each user has multiple image on different API and data on different API too获取每个用户,每个用户在不同的 API 上有多个图像,在不同的 API 上也有数据
【发布时间】:2021-10-07 17:59:35
【问题描述】:

我正在尝试获取数据!使用不同的API!我的问题是在获取图像时!它会创建重复,并且图像会重复!

我希望我的结果是!

Name1 AGE1 image1 image2 image3 image4
Name2 AGE2 image2 image2 image3 image4
Name3 AGE3 image3 image3
Name4 AGE4 image4 image4 image4 image4

但我的代码上显示的结果

Name1 AGE1 image1 image1 image1 image1
Name2 AGE2 image2 image2 image2 image2 image1 image1
Name3 AGE3 image3 image3 image2 image2 image1 image1 image2 image2 image1 image1
Name4 AGE4 image4 image4 image4 image4 image2 image2 image1 image1 image2 image2 image1 image1

这是我使用的代码

  <?php
    $query = 
    "SELECT 
    * 
    FROM 
    user 
    ";
    $rows =array();
    $result = mysqli_query($conn, $query);
    while($row = mysqli_fetch_assoc($result))
    {
    $rows[] = $row;
    }
    
    foreach ($rows as $row)
    {
        $id = $row['id'];
        date_default_timezone_set('Asia/Manila');
        $url = 'https://api.user.com/'.$id;
        $ch=curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $r=curl_exec($ch);
        curl_close($ch);
        $data = json_decode($r, true);
        $stats =$data[status];
?>
        <tr><td>
<?php 
        $urlserver = 'https://api.images.com'.$id;
        $initiate=curl_init($urlserver);
        curl_setopt($initiate, CURLOPT_RETURNTRANSFER, true);
        $result=curl_exec($initiate);
        curl_close($initiate);
        $code = json_decode($result, true);
        foreach($code["results"] as $result)
        {
            $images[] = $result['image'];
        }
            foreach ($images as $img)
        {
        
        ?>
        <img src="<?php echo $img;?>" ></td>
<?php } ?>
        <td><?php echo $row['fname'];?></td>
        <td><?php echo $row['lname'];?></td>
        <td><?php echo $row['age'];?></td>
        <td><?php echo $stats;?></td>
        </tr>
<?php } ?>

数据结果

提前致谢!

【问题讨论】:

  • 请分享保存图像数组的内容或请更具体地了解输入
  • 对不起,我忘了添加数据结果!我已经更新了!

标签: php arrays curl


【解决方案1】:

问题解决了

在循环中使用循环

foreach() {
//#value
}
//value{
for i value here
{
the other loop
}
}

【讨论】:

    【解决方案2】:

    我的问题已经解决了

    foreach() {
    //#value
    }
    //value{
    for i value here
    {
    the other loop
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 2013-10-14
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多