Last active
December 19, 2015 18:19
-
-
Save luxixing/5997938 to your computer and use it in GitHub Desktop.
面试题答案
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 打印前一天时间 | |
<?php | |
echo date("Y-m-d H:i:s", strtotime("-1 days")); | |
?> | |
2 过滤html代码 | |
如果此题单纯值的html代码,则 htmlspecialchars($str),处理输入存储 | |
如果包含用户输入过滤,则sql语句防注入,过滤关键词,使用pdo的时候prepare sql语句 ' 转义 | |
还有和谐社会的关键词过滤(这个非技术而属于政治了) | |
php 原生 Filler 族函数能做相当一部分工作,可在此基础上扩展一个过滤类 | |
总之此类过滤主要是防 xss sql注入攻击等,验证输入合法性 | |
3 css元素样式 | |
<span style="color:red;font-family: sans-serif;font-size:14px;text-decoration: underline">some things</span>"; | |
4 tab 切换 | |
可以使用 bootstrap中的相关组件实现该功能 | |
代码如下 | |
<html> | |
<head> | |
<link href="http://cdnjs.bootcss.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap.min.css" rel="stylesheet"> | |
<script src="http://cdnjs.bootcss.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script src="http://cdnjs.bootcss.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<ul class="nav nav-tabs" id="myTab"> | |
<li class="active"><a href="#home">in the news </a></li> | |
<li><a href="#profile">word</a></li> | |
<li><a href="#messages">local</a></li> | |
<li><a href="#settings">finace</a></li> | |
</ul> | |
<div class="tab-content"> | |
<div class="tab-pane active" id="home">in the news</div> | |
<div class="tab-pane" id="profile">word</div> | |
<div class="tab-pane" id="messages">local</div> | |
<div class="tab-pane" id="settings">finace</div> | |
</div> | |
<script> | |
$('#myTab a').click(function (e) { | |
e.preventDefault(); | |
$(this).tab('show'); | |
}) | |
$(function () { | |
$('#myTab a:last').tab('show'); | |
}) | |
</script> | |
</body> | |
</html> | |
5 代码排错及优化 | |
源代码: | |
<?php | |
function baz($y $z) { | |
$x = new Array(); | |
$x[sales] = 60; | |
$x[profit] = 20: | |
foreach($x as $key = $value) { | |
echo $key+" "+$value+"<BR>"; | |
} | |
} | |
?> | |
修正代码 | |
<?php | |
function baz() | |
{ | |
$x = array( | |
"sales"=> 60, | |
"profit"=>20, | |
); | |
foreach($x as $k=>$v) | |
{ | |
echo $k . " " . $v . "<br/>"; | |
} | |
} | |
?> | |
6 mysql sql | |
1)查询选修语文的学生学号姓名 | |
select * from course where name="语文";//假设返回值ID 100,之所以使用* 是该表数据变化情况较小,可被缓存 | |
select studentID from sc where courseID=100;//假设返回值是 1, 2, 3, 4 | |
select ID,name from student where ID in(1, 2, 3, 4); | |
之所以分为三个简单查询,原因有3: | |
<1>简单查询可以被mysql自身缓存 | |
<2>简单查询在程序端加入缓存非常容易,比如course表里的数据,变化的概率应该不是特别高 | |
可以使用redis,memcache,file等缓存方式进行缓存 | |
多表查询语句实现: | |
select s.ID,s.name | |
from sc | |
left join student as s on s.ID = sc.studentID | |
left join course as c on c.ID = sc.courseID | |
where c.name='语文' | |
group by s.ID | |
<3> 使用索引查询的概率比较高 | |
2)查询周星星同学选修的课程 | |
select ID from student where name='周星星';//ID 值 7 | |
select courseID from sc where studentID=7;//1,2,3,4 | |
select * from course where ID in(1,2,3,4); | |
除了借助redis file等方式缓存数据,在同一次会话中,我们的查询结果也应该由程序员自己缓存起来 | |
降低同一次会话对mysql数据库的访问次数 | |
本case中,couse表里的ID 为 1-4的数据即可被缓存,假设在会话中有其他业务逻辑正好要查询相关数据, | |
那么程序段的缓存就有很积极的意义 | |
多表关联查询 | |
select c.ID,c.name | |
from sc | |
left join student as s on s.ID = sc.studentID | |
left join course as c on c.ID = sc.courseID | |
where s.name="周星星" | |
group by c.ID | |
3)查询选修5门课程的学生学号和姓名 | |
select studentID,count(*) as num from sc where 1 group by studentID | |
select studentID,count(*) as num from sc where 1 group by studentID having num >=5 | |
以上两句相同,只不过第二个语句先显示的时候过滤掉了选修课程小于5的数据 | |
explain结果 rows 返回的数据行是一样的,使用第二个sql,程序中循环结果集即可 | |
组合结果集中的 studentID 为一个数组,或者直接拼串 | |
slect ID ,name from student where ID in(1,3,4,5); | |
多表关联查询 | |
select count(sc.*) as n,s.ID,s.name | |
from sc | |
left join student as s on s.ID = sc.studentID | |
group by sc.studentID | |
having n >=5 | |
7 文件数据处理 | |
<?php | |
/** | |
* string $rfile read the file contents and handle data | |
* string $wfile write result to this file | |
* return boolean | |
*/ | |
function dataCount($rfile, $wfile) | |
{ | |
if(!file_exists($rfile)) | |
{ | |
return false; | |
} | |
$fp = fopen($rfile, "r"); | |
$a = array(); | |
$s = ''; | |
$pattern = "/^\d{4}-\d{2}-\d{2}.*/"; | |
while(!feof($fp)) | |
{ | |
$line = fgets($fp); | |
$a[] = $line; | |
$n = array_count_values(explode(' ', $line)); | |
arsort($n); | |
$i = 0; | |
foreach($n as $k=>$v) | |
{ | |
if($i > 10) | |
{ | |
break; | |
} | |
echo "the words {$k} nums:{$v}<br/>\n"; | |
$i++; | |
} | |
if(preg_match($pattern, $line)) | |
{ | |
$s .= $line; | |
} | |
} | |
if($s) | |
{ | |
file_put_contents($wfile, $s); | |
} | |
fclose($file); | |
return true; | |
} | |
?> | |
8 设计方案解决坐标查询问题 | |
方案1 : | |
设计数据表 coordinate 字段 ID, xpoint,ypoint,主键 ID,索引 xpoint,ypoint | |
然后编写php 脚本读取坐标文件数据,插入数据表中 | |
编写方法,可以读取指定条件的数据 | |
方案2 : | |
存储数据至 redis | |
选择数据存储类型为有序集合 coordinate:xpoint 其中 score存储 x坐标,value存储y坐标值 | |
coordinate:ypoint score 存储y坐标,value 存储x坐标 | |
编写脚本读取文件内容,将数据存入redis | |
编写函数根据条件读取坐标,取出符合条件的x坐标数据,取出符合条件的y坐标数据,取两者 | |
之间的交集即可 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment