Home > Article > Backend Development > PHP programming tips: Return array data with specified field as index_PHP tutorial
In many cases, from the time we are exposed to a new project to the completion of development, we then go back and carefully browse the codes we have written. Many of them are codes that we have used proficiently before. Therefore, when completing each new project, do some project summary and code summary appropriately. Maybe you will use it in future projects, and you are very likely to get unexpected gains, such as code optimization and better ideas. , faster implementation methods, etc.
Sometimes what makes a great program developer is not the amount of code, but the simplicity of the program code and the ease of implementation despite complex logic. These are the factors that show whether a programmer is a good programmer. We don’t want to be programmers who work overtime day and night until late at night, scrambling to write a huge amount of code!
This blog will share with you several PHP programming skills. Some of them were learned while looking at other people’s codes, and some were summarized by myself.
Using a specific field as an index to return the data array retrieved from the database
It’s easier to give an example:
Suppose you want to count the traffic of a specified site from other websites, and make a small backend to check the traffic brought by each website every day. Let’s first create 2 data tables:
Table 1. Site configuration table (only the traffic of these websites is counted)
CREATE TABLE `site_config` ( `id` smallint(5) unsigned NOT NULL auto_increment COMMENT '主键,自增', `sid` smallint(5) unsigned NOT NULL COMMENT '网站ID', `site_url` varchar(128) NOT NULL COMMENT '网站URL地址', `site_name` varchar(80) NOT NULL COMMENT '网站名称', `add_time` timestamp NOT NULL default CURRENT_TIMESTAMP COMMENT '添加时间', PRIMARY KEY (`id`), UNIQUE KEY `adid` (`sid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='站点配置表';
Table 2. Site traffic statistics table (one record per user)
CREATE TABLE `site_stat` ( `id` int(11) unsigned NOT NULL auto_increment COMMENT '主键,自增', `sid` smallint(5) unsigned NOT NULL COMMENT '网站ID', `ip_address` varchar(32) NOT NULL COMMENT '用户IP', `add_time` timestamp NOT NULL default CURRENT_TIMESTAMP COMMENT '添加时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='站点流量统计表' ;
Because the read operations of the site_config table are greater than the write operations, the type is set to MyISAM; and the write operations of the site_stat table are greater than the read operations, so I set the table type to InnoDB. (These are also things you need to consider when designing data, and the speed will be improved a lot).
Back to the topic, if there are several pieces of data in the table site_config:
The data format is: id,sid,site_url,add_time
1,200,baidu.com,2013-06-30 14:20:00
2,201,google.com,2013-06-30 14:20:00
3,202,cnblogs.com,2013-06-30 14:20:00
4,203,codejia.net,2013-06-30 14:20:00
Some data has been generated in the site traffic statistics table site_stat, as follows:
The data format is: id,sid,ip_address,add_time
1,200,167.87.32.4,2013-06-30 14:40:00
2,200,192.168.11.56,2013-06-30 14:40:10
3,202,167.87.32.4,2013-06-30 14:40:10
4,202,192.168.11.56,2013-06-30 14:40:20
5,203,167.87.32.4,2013-06-30 14:40:20
6,202,10.10.10.10,2013-06-30 14:40:30
7,200,167.87.32.4,2013-06-30 14:40:31
The report format you need to make in the background is: date, website ID, website URL, traffic number (sites with no traffic should also be displayed, and the traffic will be displayed as 0)
You may think of group by sid in table site_stat based on date and then left join table site_config. This is obviously not the method I will share.
Use 2 SQLs to do it, one is to get all sites; the other is to get site traffic statistics on a specified date.
SQL1 gets all sites:
SELECT sid,site_url,site_name FROM site_config
SQL2 gets site statistics (if today):
SELECT sid,COUNT(1) AS come_total FROM site_stat WHERE add_time>='2013-06-30 00:00:00' AND add_time<='2013-06-30 23:59:59' GROUP BY sid
A lot of what I said earlier is to pave the way for what follows. Let me share a PHP method that uses the passed field as an index to return a two-dimensional array:
protected function getList($sql,$filed = null){ $res = mysql_query($sql,$this->link_sc); $data = array(); if($filed === null){ while($row = mysql_fetch_assoc($res)){ $data[] = $row; } }else{ while($row = mysql_fetch_assoc($res)){ $data[$row[$filed]] = $row; } } return $data; }
Pass in your SQL and the fields that need to be indexed. The premise is that the fields passed in must be among the fields returned by select.
We do not need to pass the past field when fetching all sites, but when fetching site traffic statistics, we pass the site sid, which is as follows:
$sites = getList($sql1); //所有站点 $data = getList($sql2,'sid'); //站点流量数据,以sid为索引返回二维数组
Finally, when displaying the report, use foreach to loop $sites, and when getting the traffic, go to the array $data to get the data with the index sid.
Very convenient and easy to master, I do it often.
Finally, take a look at the statistical results report in the above table:
The format is: date, website ID, website URL, traffic number
2013-06-30,200,baidu.com,3
2013-06-30,201,google.com,0
2013-06-30,202,cnblogs.com,3
2013-06-30,203,codejia.net,1
Summary: It seems that writing so much just shows such a small skill, but don’t underestimate this skill, it can save you a lot of time. Therefore, everyone should summarize more when writing code, come to the blog park to communicate and learn with everyone, and your programming level will definitely improve quickly!