显示查询结果之间的间距

发布于 2024-11-18 22:11:05 字数 1039 浏览 1 评论 0原文

我是 php 和 MySQL 的新手。

我正在我的网站上制作一个处理移动下载的脚本。

我在这里设置了 3 张桌子。

  1. downloads(包含有关特定下载的信息,每个下载都有自己的 ID)。
  2. models(包含可能的设备型号(型号名称)列表,每个型号都有自己的 id)。
  3. 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; 
    ';

要显示哪些型号支持下载,请将此行 echod:

    $dpModSupport .= stripslashes($row['model']);

现在作为示例,我的页面的 $pageId 为“1”,即下载“example1”,“ example1”支持模型1(模型1)、2(模型2)和3(模型3)。当$dpModSupportechod 时,它会回显为"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.

  1. downloads (contains info about a specific download, each with its own id).
  2. models (contains a list a possible device models(model names), each with its own id).
  3. 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 echod:

    $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 echod, 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 技术交流群。

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

发布评论

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

评论(2

东京女 2024-11-25 22:11:05

问题

通过在循环中编写此内容:

$dpModSupport .= stripslashes($row['model']);

您正在构建一个像这样的字符串:

$dpModSupport .= stripslashes(<MODEL1>);
$dpModSupport .= stripslashes(<MODEL2>);
$dpModSupport .= stripslashes(<MODEL3>);

因此最终得到的字符串只是彼此相邻的每行的内容。


解决方案

相反,您可以在循环之前编写以下内容:

$dpModSupport = Array();

然后,在循环内部,而不是使用 .= 构建字符串,而是构建数组:

$dpModSupport[] = stripslashes($row['model']);

最后,获取包含每个元素的输出该数组的名称,用逗号连接:

echo implode(', ', $dpModSupport);

我将把它作为练习留给读者在 PHP 手册中查找这些函数和语言特性。

Problem

By writing this in a loop:

$dpModSupport .= stripslashes($row['model']);

you're building up a string like this:

$dpModSupport .= stripslashes(<MODEL1>);
$dpModSupport .= stripslashes(<MODEL2>);
$dpModSupport .= stripslashes(<MODEL3>);

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:

$dpModSupport = Array();

And then, inside it, instead of the line with .= building up a string, build up the array:

$dpModSupport[] = stripslashes($row['model']);

Finally, to obtain output consisting of each element of said array, joined by a comma:

echo implode(', ', $dpModSupport);

I will leave it as an exercise to the reader to look up these functions and language features in the PHP manual.

沉溺在你眼里的海 2024-11-25 22:11:05

将名称存储在数组中,然后将它们连接到末尾。

$dpModSupport = array();
$dpModSupport[] = stripslashes($row['model']);
echo implode(', ', $dpModSupport);

Store the names in an array, and then join them at the end.

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