仅在变量更改时才打印? [英] Printing a variable only when it changes?

查看:92
本文介绍了仅在变量更改时才打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的问题有点含糊或令人困惑,因为我不确定如何具体说明我的问题。我正在尝试查询一家针织公司的库存商数据库(使用PHP的学校项目),但我希望将城市打印为标题,而不是使用每个库存商的信息。



这是我目前所拥有的:

  $ sql = SELECT * FROM mc16korustockists,这里的locale ='south '; 
$ result = pg_exec($ sql);
$ nrows = pg_numrows($ result);
打印$ nrows;
$ items = pg_fetch_all($ result);
print_r($ items);

for($ i = 0; $ i <$ nrows2; $ i ++){
print< h2>;
打印$ items [$ i] [’city’];
打印< / h2>;

print $ items [$ i] [’name’];
打印$ items [$ i] [’address’];
打印$ items [$ i] [’city’];
打印$ items [$ i] [’phone’];

打印< br />;
打印< br />;

}



我正在查询数据库中的所有数据,行是ref,名称,地址,城市和电话,然后执行它。查询行数,然后使用它来确定循环要运行多少次迭代就可以了,但是我想让h2标题出现在for($ i = 0;)行上方。



尝试只是中断了我的页面,所以可能就没问题了。我认为我必须计算城市中的条目数,直到它检测到更改,然后将标题更改为我认为的名称?那或进行大量查询并为每个名称设置一个变量,但是到那时,我不妨手动进行(我非常怀疑这是最佳实践)。哦,自从我刚入门以来,我就欢迎对我的PHP提出任何批评。



谢谢,如果您需要更多信息,请问! >

PS

解决方案

这将解决您的问题:

  $ sql = SELECT * FROM mc16korustockists,其中locale =按城市的南顺序; 

...


$ city =’;

for($ i = 0; $ i< $ nrows2; $ i ++){
if($ items [$ i] ['city']!= $ city)
{
print< h2>;
打印$ items [$ i] [’city’];
打印< / h2>;
$ city = $ items [$ i] [’city’];
}
print $ items [$ i] [’name’];
打印$ items [$ i] [’address’];
打印$ items [$ i] [’city’];
打印$ items [$ i] [’phone’];

打印< br />;
打印< br />;
}


First off, my question was a little vague or confusing since I'm not really sure how to phrase my question to be specific. I'm trying to query a database of stockists for a Knitting company (school project using PHP) but I'm looking to print the city as a heading instead of with each stockists information.

Here is what I have at the moment:

$sql = "SELECT * FROM mc16korustockists where locale = 'south'";
    $result = pg_exec($sql);
    $nrows = pg_numrows($result);
    print $nrows;
    $items = pg_fetch_all($result);
    print_r($items);

for ($i=0; $i<$nrows2; $i++) {
    print "<h2>";
        print $items[$i]['city'];
    print "</h2>";

    print $items[$i]['name'];
    print $items[$i]['address'];
    print $items[$i]['city'];
    print $items[$i]['phone'];

    print "<br />";    
    print "<br />";

}

I'm querying the database for all of the data in it, the rows being ref, name, address, city and phone, and executing it. Querying the number of rows then using that to determine how many iterations for the loop to run is all fine but what I'd like to have is for the h2 heading to appear above the for ($i=0;) line.

Trying just breaks my page so that might be out of the question. I figure I'd have to count the number of entries in 'city' until it detects a change then change the heading to that name I think? That or make a heap of queries and set a variable for each name but at point, I might as well do it manually (and I highly doubt it would be best practice). Oh, and I'd welcome any critiques to my PHP since I'm just starting out.

Thanks and if you need any more information, just ask!

P.S. Our class is learning with PostgreSQL instead of MySQL as you can see in the tags.

解决方案

This will solve your problems:

$sql = "SELECT * FROM mc16korustockists where locale = 'south' order by city";

...


$city = '';

for ($i=0; $i<$nrows2; $i++) {
    if($items[$i]['city']!=$city)
    {
        print "<h2>";
            print $items[$i]['city'];
        print "</h2>";
        $city = $items[$i]['city'];
    }
    print $items[$i]['name'];
    print $items[$i]['address'];
    print $items[$i]['city'];
    print $items[$i]['phone'];

    print "<br />";    
    print "<br />";
}

这篇关于仅在变量更改时才打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆