PHP 不回显最后一行的文本

发布于 2024-12-02 15:45:35 字数 395 浏览 1 评论 0原文

我正在尝试在 php 中编写一个相对简单的 twitter 查询,结果如下:

from:TravisBenjamin3 OR from:AshleyElisaG OR from:tncvaan ...

这段代码可以工作,接受它不会回显第二个 OR,我也不这样做想要在最后一行显示 OR。

$counter = 0;
while($row = mysql_fetch_array($result)){
$counter++;
echo " from:";
echo $row['twitter'];
if ($counter < count($row)) {
               echo " OR";
           }
}

一些帮助将不胜感激。谢谢,

I'm trying to write a relatively simple twitter query in php with result like this:

from:TravisBenjamin3 OR from:AshleyElisaG OR from:tncvaan ...

This code works accept it doesn't echo the second OR, also I don't want to display an OR on the last row.

$counter = 0;
while($row = mysql_fetch_array($result)){
$counter++;
echo " from:";
echo $row['twitter'];
if ($counter < count($row)) {
               echo " OR";
           }
}

Some help would be greatly appreciated. Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青朷 2024-12-09 15:45:35
<?php
$tweets = array();
while($row = mysql_fetch_array($result)) {
    $tweets[] = $row['twitter'];
}
if(count($tweets) > 0) {
    echo "from:" . implode(" OR from:", $tweets);
}
?>
<?php
$tweets = array();
while($row = mysql_fetch_array($result)) {
    $tweets[] = $row['twitter'];
}
if(count($tweets) > 0) {
    echo "from:" . implode(" OR from:", $tweets);
}
?>
空心空情空意 2024-12-09 15:45:35
$rowCount = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
$counter++;
echo " from:";
echo $row['twitter'];
if ($counter < $rowCount) {
               echo " OR";
           }
}
$rowCount = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
$counter++;
echo " from:";
echo $row['twitter'];
if ($counter < $rowCount) {
               echo " OR";
           }
}
葬花如无物 2024-12-09 15:45:35
$count = mysql_num_rows($result);

for($r = 0; $r < $count; $r++)
{
   $row = mysql_fetch_array($result));
   echo " from:";
   echo $row['twitter'];
   if ($r < ($count - 1)) 
   {
           echo " OR";
   }
}

或者,更简单:

$search = "";

while($row = mysql_fetch_array($result))
{
   $search .= " from:" . $row['twitter'] . " OR";
}

// Cut the last " OR"
$search = substr($search, 0, strlen($search) - 3);
echo $search;

这实际上是 Aaron W. 建议的 implode() 的穷人实现。

$count = mysql_num_rows($result);

for($r = 0; $r < $count; $r++)
{
   $row = mysql_fetch_array($result));
   echo " from:";
   echo $row['twitter'];
   if ($r < ($count - 1)) 
   {
           echo " OR";
   }
}

Or, even easier:

$search = "";

while($row = mysql_fetch_array($result))
{
   $search .= " from:" . $row['twitter'] . " OR";
}

// Cut the last " OR"
$search = substr($search, 0, strlen($search) - 3);
echo $search;

Which is actually a poor man's implementation for implode() suggested by Aaron W..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文