停止生成IF小于X的表


Stop table from being generated IF less then X

我有一个在REST API上生成的表,我为结果中的前50个生成。我的问题是,如果REST的响应小于50,那么表仍然使表为50,只是X数量上的空行在响应中不可用。

所以,例如,我得到了47的结果,然后我的代码仍然只生成50,所以最后三行是空的。我该如何避免这种情况?

  for($x=0;$x<50;$x++)
  echo "<table><tr><td>" . "<img style='width:200px; height:150px;' src='" . $imagehost . $newImgUrl = preg_replace($pattern, $replacement, $hotelSummary[$x]['thumbNailUrl']) . "'/>" . "</td><td>" . $hotelSummary[$x]['name'] . "</td><td>" . $hotelSummary[$x]['hotelId'] . "</td><td>" . $hotelSummary[$x]['city'] . "</td><td>" . $hotelSummary[$x]['RoomRateDetailsList']['RoomRateDetails']['RateInfos']['RateInfo']['ChargeableRateInfo']['@total'] . "</td><td><a href='"/hotels/hotelPage.php?go&customerSessionId=$customerSessionId&hotelId=" . $hotelSummary[$x]['hotelId'] . " '"><button type='button'>Hotel Info</button></a><td><a href='" . $hotelSummary[$x]['deepLink'] . "' ><button type='button'>Book Now</button></a></td></tr></table>";

因此,如果响应小于50,则仅为找到的x数量创建,如果数量小于1,则回声输出,例如没有与您的搜索匹配的结果。

我可以在这里执行if语句吗?或者我应该如何执行?

不要比较

$x<50

但是

$x < $responseFromRest && $x < 50

您可以添加一个if来检查您的响应的sizeof是否大于$x,如果是,echo您的行else break。示例:

$reponse = $data;
for($x=0;$x<50;$x++) {
    if($x > sizeof($respnse)) {
        break;
    }
    echo "<table><tr><td>" . "<img style='width:200px; height:150px;' src='" . $imagehost . $newImgUrl = preg_replace($pattern, $replacement, $hotelSummary[$x]['thumbNailUrl']) . "'/>" . "</td><td>" . $hotelSummary[$x]['name'] . "</td><td>" . $hotelSummary[$x]['hotelId'] . "</td><td>" . $hotelSummary[$x]['city'] . "</td><td>" . $hotelSummary[$x]['RoomRateDetailsList']['RoomRateDetails']['RateInfos']['RateInfo']['ChargeableRateInfo']['@total'] . "</td><td><a href='"/hotels/hotelPage.php?go&customerSessionId=$customerSessionId&hotelId=" . $hotelSummary[$x]['hotelId'] . " '"><button type='button'>Hotel Info</button></a><td><a href='" . $hotelSummary[$x]['deepLink'] . "' ><button type='button'>Book Now</button></a></td></tr></table>";
}