PHP:在 while 中创建“假”/自定义语句
我有这个 while 循环。
while($show = $queryActivities->fetch()){ echo $show["name"]."
"; 这从查询
中获取数据并输出名称。
现在可以制作自定义/假数据吗?如果 $queryActivities 中没有数据/语句,我想制作一个,那么它应该制作一个名为“name”的自定义数据/语句,其值“这里什么都没有..”
这可能吗?你能做到吗?
我知道我可以做 if($queryActivities->rowCount == 0){ echo "Nothing here" ;但
我更多地考虑创建自定义数据,因此它使用自定义数据运行 while 循环,只有在 $queryActivities 中没有任何内容时才会创建自定义数据。
像这样的东西:
if($queryActivities->rowCount == 0){
# ..MAKE CUSTOM DATA..
# ..SOMETHING LIKE THIS MAYBE: ..
# $queryActivities = MAKE ARRAY WITH name => 'Nothing here'.. (just a thought)
}
while ($show = $queryActivities->fetch()){
echo $show["name"]."<br>";
}
像这样的东西,就像我想象的那样,尽管我不知道到底是怎么做的。
谢谢
I have this while loop.
while($show = $queryActivities->fetch()){ echo $show["name"]."<br>"; }
This takes data from the query and outputs the names..
Now is it possible to make a custom/fake data? I would like to make one if theres no data/statement in the $queryActivities then it should make a custom one called "name" with value "Nothing here.."
Is this possible? Can you do that?
I know I can do if($queryActivities->rowCount == 0){ echo "Nothing here" ; }
But I more thought of creating a custom data, so it runs the while loop, with the custom data, that only gets made if theres nothing in $queryActivities.
Something like..:
if($queryActivities->rowCount == 0){
# ..MAKE CUSTOM DATA..
# ..SOMETHING LIKE THIS MAYBE: ..
# $queryActivities = MAKE ARRAY WITH name => 'Nothing here'.. (just a thought)
}
while ($show = $queryActivities->fetch()){
echo $show["name"]."<br>";
}
Something like this, just what I imagine, although I dont know really how.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将数据检索到数组中,然后将其打印出来。在这两个步骤之间,您可以根据需要进行修改。
无论如何,这更加有序。我不喜欢从数据库检索并在一步中显示
您可以使用 if/else 来检查数组是否为空并打印一条默认消息,或者创建一个带有“名称”属性的默认条目,然后显示该条目具有与普通数据相同的输出循环。我更喜欢第一种,但我们会采用第二种风格,因为这就是你所问的。
这是一个简单的解决方案。
我认为仅使用 if/else 是更好的解决方案。不过,根据您的输出样式,这可能会涉及更多代码重复。
Retrieve your data to an array, and then print it out. In between these two steps, you can modify it as desired.
This is more orderly, anyhow. I don't like retrieving from the DB and displaying in one step
You could either have an if/else that checks if the array is empty and prints a default message, or creates a default entry with a 'name' property that then is displayed with the same output loop as normal data. I'd prefer the first, but we'll do the second style since that's what you asked about.
Here's one simple solution.
I think just using an if/else is a better solution. Depending on your output style, though, this might involve more code repetition.