将循环结果保存到字符串中

发布于 2024-07-30 09:03:43 字数 809 浏览 1 评论 0原文

是否可以将循环结果保存到字符串中?

$sql = "SELECT SUBSTR(a.`title`, 1,1) FROM articles a WHERE a.`tag` = 'human_resources'";
$results = db_query($sql);
  while ($fields = db_fetch_array($results)) {
     foreach($fields as $key => $value) {
       echo $value;
     }
  }

上面的代码输出带有标签 human_resources 的文章标题。 我想为目录制作一个字母栏,所以我使用这个:

if(stripos($string, "A") !== false) {
    echo ('<a href="http://www.mysite.com/articles/A">A</a>');
}
else echo '<span class="inactive">A</span>';

if(stripos($string, "B") !== false) {
    echo ('<a href="http://www.mysite.com/articles/B">B</a>');
}
else echo '<span class="inactive">B</span>';

...etc

但我不知道如何从代码第二部分的循环中获取 $string 。

非常感谢任何解决此问题的建议或更好的方法。

Is it possible to save loop results to a string?

$sql = "SELECT SUBSTR(a.`title`, 1,1) FROM articles a WHERE a.`tag` = 'human_resources'";
$results = db_query($sql);
  while ($fields = db_fetch_array($results)) {
     foreach($fields as $key => $value) {
       echo $value;
     }
  }

The code above outputs titles of articles with a tag human_resources. I want to make an alphabar for a catalog, so I'm using this:

if(stripos($string, "A") !== false) {
    echo ('<a href="http://www.mysite.com/articles/A">A</a>');
}
else echo '<span class="inactive">A</span>';

if(stripos($string, "B") !== false) {
    echo ('<a href="http://www.mysite.com/articles/B">B</a>');
}
else echo '<span class="inactive">B</span>';

...etc

But I don't know how to get that $string from the loop for the second part of code.

Any suggestions or better approach to this problem are greatly appreciated.

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

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

发布评论

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

评论(4

单身情人 2024-08-06 09:03:44

除了其他人所说的之外,您可能还需要使用循环来显示字母顺序菜单:

for ($i = 65; $i < 91; $i++) {
     $letter = chr($i);
     if(stripos($string, $letter) !== false) {
          echo ('<a href="http://www.mysite.com/articles/'.$letter.'">'.$letter.'</a>');
     }
     else echo '<span class="inactive">'.$letter.'</span>';
}

这将使您不必复制并粘贴每个字母的代码。

In addition to what everybody else has said, you will likely want to use a loop to display your alphabetical menu:

for ($i = 65; $i < 91; $i++) {
     $letter = chr($i);
     if(stripos($string, $letter) !== false) {
          echo ('<a href="http://www.mysite.com/articles/'.$letter.'">'.$letter.'</a>');
     }
     else echo '<span class="inactive">'.$letter.'</span>';
}

This will save you having to copy and paste the code for each letter.

不羁少年 2024-08-06 09:03:43

将此:更改

echo $value;

为:

$string .= $value;

Change this:

echo $value;

to this:

$string .= $value;
亢潮 2024-08-06 09:03:43

我不确定您到底想要什么...更新您的示例以显示您拥有的内容以及您想要的结果。

您可以使用数组存储值列表:

$list = array();
for (...) {
    /*
    Some code here...
    */
    // Add the current string to the list.
    $list[] = $string;
}

如果您只需要一个长字符串,则可以附加:

$all = "";
for (...) {
    /*
    Some code here...
    */
    // Add the current string to a string with all the strings concatenated.
    $all .= $string;
}

I'm not sure exactly what you want... update your examples to show what you have and what you want the result to be.

You can store a list of values using an array:

$list = array();
for (...) {
    /*
    Some code here...
    */
    // Add the current string to the list.
    $list[] = $string;
}

If you just want one long string, you can append:

$all = "";
for (...) {
    /*
    Some code here...
    */
    // Add the current string to a string with all the strings concatenated.
    $all .= $string;
}
心的位置 2024-08-06 09:03:43
while ($fields = db_fetch_array($results)) {
     foreach($fields as $key => $value) {
      echo $value;
     }
}

尝试

$result = "";
while ($fields = db_fetch_array($results)) {
     foreach($fields as $key => $value) {
      $result = $result . $value;
     }
}
restun $result
while ($fields = db_fetch_array($results)) {
     foreach($fields as $key => $value) {
      echo $value;
     }
}

try

$result = "";
while ($fields = db_fetch_array($results)) {
     foreach($fields as $key => $value) {
      $result = $result . $value;
     }
}
restun $result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文