显示查询结果之间的间距
我是 php 和 MySQL 的新手。
我正在我的网站上制作一个处理移动下载的脚本。
我在这里设置了 3 张桌子。
- downloads(包含有关特定下载的信息,每个下载都有自己的 ID)。
- models(包含可能的设备型号(型号名称)列表,每个型号都有自己的 id)。
- downloads_models(链接表声明 download_id = model_id,例如 (1,2) (1,3) (2,1))。
我有一个简单的查询来显示支持每个特定下载的模型。其内容如下:
$dlDetailsQuery = 'SELECT models.model
FROM models,downloads_models
WHERE downloads_models.modId = models.model_id
AND downloads_models.downId = "'.$pageId.'"
ORDER BY models.model ASC;
';
要显示哪些型号支持下载,请将此行 echo
d:
$dpModSupport .= stripslashes($row['model']);
现在作为示例,我的页面的 $pageId 为“1”,即下载“example1”,“ example1”支持模型1(模型1)、2(模型2)和3(模型3)。当$dpModSupport
为echo
d 时,它会回显为"model1model2model3"
。
我的问题是:如何在结果之间获得空格或逗号?
我的目标是输出“model1,model2,model3”。
我正在寻找一种通过 php 来做到这一点的方法。我不知道查询是否可以构造为输出结果之间的间距,所以我猜测 php 是唯一的解决方案。
我不想将空格或逗号添加到模型名称字段中,或者创建一个包含空格或逗号以及模型名称的新字段。
I am new to php and MySQL.
I am making a script on my website, that deals with mobile downloads.
I have 3 tables set up that I am dealing with here.
- downloads (contains info about a specific download, each with its own id).
- models (contains a list a possible device models(model names), each with its own id).
- downloads_models (linking table stating download_id = model_id, eg. (1,2) (1,3) (2,1)).
I have a simple query to display the models that support each specific download. It goes as follows:
$dlDetailsQuery = 'SELECT models.model
FROM models,downloads_models
WHERE downloads_models.modId = models.model_id
AND downloads_models.downId = "'.$pageId.'"
ORDER BY models.model ASC;
';
To display which models support the download, this line is echo
d:
$dpModSupport .= stripslashes($row['model']);
Now as an example, my page has a $pageId of "1" which is the download "example1", "example1" supports models 1(model1), 2(model2) and 3(model3). When$dpModSupport
is echo
d, it echoes as "model1model2model3"
.
My question is this: how do I get spacing, or a comma between the results?
What I am aiming for is an output of "model1, model2, model3".
I'm looking for a way to do this via php. I don't know whether a query can be structured to output spacing between results, so I'm guessing that php is the only solution there.
I do not want to add the spacing or commas into the model name field, or create a new field containing the spacing or commas along with the model's name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题
通过在循环中编写此内容:
您正在构建一个像这样的字符串:
因此最终得到的字符串只是彼此相邻的每行的内容。
解决方案
相反,您可以在循环之前编写以下内容:
然后,在循环内部,而不是使用
.=
构建字符串,而是构建数组:最后,获取包含每个元素的输出该数组的名称,用逗号连接:
我将把它作为练习留给读者在 PHP 手册中查找这些函数和语言特性。
Problem
By writing this in a loop:
you're building up a string like this:
thus ending up with a string that's just the contents of each row splattered next to each other.
Solution
Instead, you could write this before your loop:
And then, inside it, instead of the line with
.=
building up a string, build up the array:Finally, to obtain output consisting of each element of said array, joined by a comma:
I will leave it as an exercise to the reader to look up these functions and language features in the PHP manual.
将名称存储在数组中,然后将它们连接到末尾。
Store the names in an array, and then join them at the end.