1. get a letter combination
Question requirements:
Make a combination of three letters, such as "abd, ade, acc, aef" and so on:
Target
1. all letters must be printed
2. the combination of letters should be excluded, abc (three consecutive letters), CBA (three letters reverse), aaa (three letters the same)
3. calculate the number of abc cases, the number of CBA cases, the number of aaa cases, and the number of correct combinations of letters.
------------------------
Code:
$ B = array (a, B, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z );
For ($ I = 0; $ I <= 25; $ I ++ ){
For ($ j = 0; $ j <= 25; $ j ++ ){
For ($ k = 0; $ k <= 25; $ k ++ ){
If ($ I ==$ J-1 & $ k ==$ j + 1) {// Number of abc cases
$ M ++;
} Elseif ($ I ==$ j + 1 & $ k ==$ J-1) {// number of CBA cases
$ N ++;
} Elseif ($ I ==$ j & $ k ==$ I) {// number of aaa cases
$ O ++;
} Else {
$ P ++;
Echo $ B [$ I]. $ B [$ j]. $ B [$ k]."
";
}
}
}
}
Echo $ p ."
";
Echo $ m ."
";
Echo $ n ."
";
Echo $ o ."
";
?>
Code 2:
$ R = range (a, z );
Foreach ($ r as $ a => $ x)
{Foreach ($ r as $ B => $ y)
{Foreach ($ r as $ c => $ z)
{
If ($ a = $ B + 1 & $ B = $ c + 1) $ abc ++;
If ($ c ==$ B + 1 & $ B ==$ a + 1) $ CBA ++;
If ($ a ==$ B & $ B ==$ c) $ aaa ++;
Else $ s [$ a. $ B. $ c] = $ x. $ y. $ z;
}
}
}
$ R = $ s;
Echo 'ABC: '. $ abc .'
';
Echo 'CBA:'. $ CBA .'
';
Echo 'AAA: '. $ aaa .'
';
Foreach ($ r as $ t ){
Echo $ t .'
';
}
?>
2. assemble an SQL statement using different conditions
Question requirements:
Currently, you need to perform advanced queries for members based on different member information conditions. at this time, you need to assemble SQL statements. The conditions to be queried include: member ID, member nickname, member account, member registration time, Member gender, and condition passed to the program through GET. you can select any of these conditions for query, get the corresponding result.
Refer:
The member table is user_bas and its field structure;
Correspondence between query conditions and database fields:
Member id: id; structure: smallint (6). The query is precise (for example, querying members numbered 1 and 2)
Member nickname: nickname; structure: varchar (20) (for example, querying nicknames with "charm" in nicknames)
Member Account: uname; structure: varchar (16) fuzzy query
Member registration time: regtime; structure: int (10) query is a time period query
Member gender: sex; structure: tingint (1) the query is precise.
Objectives:
Write a program to implement such a query function, and the "query condition" will be increased and reduced as needed. the examinee needs to write a flexible program.
Note:
1. Flexibility,
You can select one or none of the conditions, or multiple conditions ."
2. Considering the system performance
Some interviewers use:
"Select * from user_bas where id = '$ ID' and nickname =' $ nickname' and sex = '$ sex '..."
No. if the condition database without $ id still needs to be passed, it will be very slow in databases with more than 0.1 million.
3. You don't have to consider accepting data and how to explain it to the database.
If you do not know how to write, you can simply think about it. there are several variables. $ Id, $ nickname, $ uname, $ sex, $ regtime can use these variables, but some of them are empty. for how to query databases, there is no need to care about how the database is interpreted, for example, you do not need to write query-related function statements.
4. syntax of the "where 1" or "where 1 = 1" type is not allowed
Code
_________________________________________________________________________
If ($ _ SERVER ['request _ method'] = 'get '){
$ Id = $ _ GET ['id'];
$ Nickname = $ _ GET ['nickname'];
$ Uname = $ _ GET ['uname'];
$ Regtime = $ _ GET ['regtime'];
$ Sex = $ _ GET ['Sex '];
}
$ ConditionsNumber = 5;
$ ConditionsArray = array ("$ id", "$ nickname", "$ uname", "$ regtime", "$ sex ");
$ SearchSQLArray = array ("where id = '$ ID'", "where nickname =' $ nickname'", "where uname = '$ uname '", "where regtime = '$ regtime'", "where sex =' $ sex '");
For ($ I = 0; $ I <$ ConditionsNumber; $ I ++)
{
If ($ ConditionsArray [$ I] = "")
$ SearchSQLArray [$ I] = "";
$ HaveWhere = false;
For ($ j = 0; $ j <$ I; $ j ++)
{
$ WherePosition = strpos ($ SearchSQLArray [$ j], "where ");
If ($ wherePosition = "1") & ($ haveWhere = false ))
{
$ SearchSQLArray [$ I] = ereg_replace ("where", "and", $ SearchSQLArray [$ I]);
$ HaveWhere = true;
}}}}
For ($ I = 0; $ I <$ ConditionsNumber; $ I ++)
$ SQL = $ SQL. $ SearchSQLArray [$ I];
$ Query = "SELECT * FROM user_bas". $ SQL. "order by id ;";
?>
3. Currently, there is an array of log records for member login. the log contains the member id, member account (uname), and logtime ).
The following statistics must be obtained through this array:
(1) obtain the logon status of different members in a day. if a member logs on to the website four times in a day, the logon status is recorded four times. The statistical result is as follows:
Logon status of members-
Member No. Member Account logon times
1 User1 4 times
3 User3 2 times
2 User2 once
(2) the login status of different members within a certain number of days is displayed. a member logs on to the website only once many times a day (it can be said that the member logs on to the website several days ). The statistical result is as follows:
Logon status of members within 3 days from:
Member No. Member Account logon times
3 User3 3 times
1 User1 2 times
2 User2 2 times
Attachment:
Member Login log array:
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 19:18:02 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 18:15:03 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 13:50:12 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-22 13:12:09 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-22 11:10:08 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 10:52:54 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-22 08:16:04 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 19:18:02 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 18:15:03 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 13:50:12 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-21 13:12:09 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-21 11:10:08 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 10:52:54 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 08:16:04 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 19:18:02 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-20 18:15:03 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 13:50:12 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-20 13:12:09 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-20 11:10:08 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 10:52:54 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 08:16:04 ′);
Code:
______________________________________________________________________________
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 19:18:02 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 18:15:03 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 13:50:12 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-22 13:12:09 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-22 11:10:08 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-22 10:52:54 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-22 08:16:04 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 19:18:02 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 18:15:03 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 13:50:12 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-21 13:12:09 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-21 11:10:08 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 10:52:54 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-21 08:16:04 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 19:18:02 ′);
$ Userlog [] = array ("id" => 2, "uname" => 'user2', "logtime" => '2017-07-20 18:15:03 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 13:50:12 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-20 13:12:09 ′);
$ Userlog [] = array ("id" => 3, "uname" => 'user3', "logtime" => '2017-07-20 11:10:08 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 10:52:54 ′);
$ Userlog [] = array ("id" => 1, "uname" => 'user1', "logtime" => '2017-07-20 08:16:04 ′);
// Question 1
$ User = ";
$ Date = '2017-07-20 ′;
Foreach ($ userlog as $ val)
{
If (! Isset ($ user [$ val ['id'])
{
$ User [$ val ['id'] ['id'] = $ val ['id'];
$ User [$ val ['id'] ['uname'] = $ val ['uname'];
$ User [$ val ['id'] ['logcounts'] = 0;
}
If (strstr ($ val ['logtime'], $ date ))
{
$ User [$ val ['id'] ['logcounts'] + = 1;
}
}
Echo'
’;
print_r($user);
echo ‘
';
// Question 2
$ Users = ";
$ Start = strtotime ('2017-07-20 ′);
$ End = strtotime ('2017-07-22 ') + 2006*24;
$ Time = ";
Foreach ($ userlog as $ val)
{
If (! Isset ($ users [$ val ['id'])
{
$ Users [$ val ['id'] ['id'] = $ val ['id'];
$ Users [$ val ['id'] ['uname'] = $ val ['uname'];
$ Users [$ val ['id'] ['Days'] = array ();
$ Users [$ val ['id'] ['logs'] = 0;
}
$ Time = strtotime ($ val ['logtime']);
If ($ time >=$ start & $ time <$ end)
{
If (! In_array (substr ($ val ['logtime'],), $ users [$ val ['id'] ['Days'])
{
$ Users [$ val ['id'] ['Days'] [] = substr ($ val ['logtime );
$ Users [$ val ['id'] ['logs'] + = 1;
}
}
}
Echo'’;
print_r($users);
echo ‘
';
?>