将循环结果保存到字符串中
是否可以将循环结果保存到字符串中?
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了其他人所说的之外,您可能还需要使用循环来显示字母顺序菜单:
这将使您不必复制并粘贴每个字母的代码。
In addition to what everybody else has said, you will likely want to use a loop to display your alphabetical menu:
This will save you having to copy and paste the code for each letter.
将此:更改
为:
Change this:
to this:
我不确定您到底想要什么...更新您的示例以显示您拥有的内容以及您想要的结果。
您可以使用
数组
存储值列表:如果您只需要一个长字符串,则可以附加:
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
:If you just want one long string, you can append:
尝试
try