dongqishou7471 2011-07-06 19:06
浏览 80
已采纳

显示的查询结果之间的间距

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.

  • 写回答

2条回答 默认 最新

  • dongyi9023 2011-07-06 19:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动