我相信我一直在编写一些效率低下的代码,但似乎无法找到一种更有效的编写方式.
这通常发生在json输出中,尽管我也遇到了一些html或xml输出的问题.
我在我的数据库中运行一个查询,它返回一个数组.说一个人最喜欢的食物.php的mysql_fetch_assoc返回
Array([person]=>john [food]=>chocolate) Array([person]=>john [food]=>pizza) Array([person]=>john [food]=>coffee) Array([person]=>susan [food]=>licorice)
为了创建我的json或html,我一直在寻找独特的人,然后添加这样的食物
$jsonOut=''
$personAdd='';
while($gotArray=mysql_fetch_assoc(foodArray)){
if($personAdd!='$gotArray['person']){
$jsonOut.="person: $gotArray['person'], foods{";
}
$jsonOut.="food:$gotArray['food'],";
rtrim(jsonOut,',');
$jsonOut.="}";
$personAdd=$array['person'];
}
现在,当你只有一个值在mysql响应中不断重复时,这不是什么大问题,但当你开始有4或5列的值相同时,它开始变得非常冗长.
有一个更好的方法吗?
-------------澄清输出应该是什么样的-----------------上面数组的最终json应该是这样的
[
{
"person": "john",
"foods": [
{
"food": "chocolate",
"food": "pizza",
"food": "coffee"
}
]
},
{
"person": "susan",
"food": "licorice"
}
]
或苏珊会有"食物":[{"food":"licorice"}]类似的东西.
我正在尝试为站点添加一小部分安全性并对某些ID进行编码.id已经是链表行的连续,因此在db中存储加密效率不高.因此我需要对字符串进行编码和解码.
我从myphpscripts中发现了这个很小的功能,我想知道碰撞的可能性是多少.
我真的不太了解这些事情.我假设我的钥匙越久,碰撞的碰撞就越少.
我最终可能拥有超过1000万个独特的连接ID,并希望确保我不会遇到问题.
function encode($string,$key) {
$key = sha1($key);
$strLen = strlen($string);
$keyLen = strlen($key);
$j=0;
$hash='';
for ($i = 0; $i < $strLen; $i++) {
$ordStr = ord(substr($string,$i,1));
if ($j == $keyLen) { $j = 0; }
$ordKey = ord(substr($key,$j,1));
$j++;
$hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
}
return $hash;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将多个jQuery数据条目添加到单个元素.
我怀疑以下是可行的
jQuery('td.person#a'+personId).data('email',thisPerson.email).data('phone',thisPerson.phone);
但是,当我这样做时,我什么都没有得到错误.
jQuery('td.person#a'+personId).data('email',thisPerson.email);
jQuery('td.person#a'+personId).data('phone',thisPerson.phone);
是否有另一种方法可以在元素上获得多个数据条目?希望链接?
我有一个Backbone.js/Rails应用程序,我正在尝试通过Backbone创建一个新对象model.save().
服务器返回a 302 moved,但检查rails控制器,创建对象存在,我可以导航到索引,因此路由正确到位.
我已经尝试用常规的jquery .ajax函数替换backbone.js,并且两者都返回302,因为我最初认为错误是在主干中,但我认为这表明错误实际上是在rails中.
请求有效负载/参数非常简单
{"user_id":130,"message_text":"does this go 302","authenticity_token":"GxN8nPf5YwS2j2HhWZxWiKej3Y72Vb5IQZ98u5Nl2gs="}
骨干保存方法是
var user_message = new Myapp.Models.UserMessage({
user_id: user.id,
message_text: $('input[name="message"]',this.el).val()
});
user_message.save({
success: function(response) {
new Message({message: response.message});
},
error: function() {
new Error({ message: "adding message" });
}
});
我有一个把手辅助方法,它通过ajax获取一个json对象,然后我想将json格式化为html并将其注入模板.
我将html和输出构建到控制台,并正确查看输出,但是
把手助手的结果永远不会显示在模板中.
Handlebars.registerHelper("accounts_dropdown", function() {
function get_dropdown(callback){
var dropdown='Select Account';
$.ajax({
url: 'accounts',
success: function(response){
for(var i=0;i<response.length;i++){
dropdown+=' < option value="'+response[i].id+'">'+response[i].name+'</option>';
}
callback(dropdown);
}
});
}
get_dropdown(function(dropdown){
console.log(dropdown);
return new Handlebars.SafeString(dropdown);
});
});
在我的模板中我有
{{accounts_dropdown}}
我有一个骨干集合,我试图基本上在属性中过滤id,用户有类,并且类有一个location_id,我想按位置id过滤.我的收藏看起来像这样给你一个想法.
-user
-models
-0
-attributes
-location_id
-1
-attributes
-location_id
-2
-attributes
-location_id
我以为我可以通过使用来过滤它
var get_locations = user_class_collection.filter(function(classes){
console.log(classes);
return classes.get(location_id)==location.id;
});
console.log(get_locations);
但是当我知道location_id在集合中时,它返回一个空数组.知道为什么这不起作用吗?我也试图抓住classes.attributes.get,但它没有任何好转
在前几个回复中,恰当地提到我必须引用它get('location_id').我现在已经这样做了,但不幸的是,我仍然得到一个空阵列.我认为filter会循环遍历类,我会得到每个类的控制台输出,但console.log(classes)只会被触发一次.这是暗示吗?还是红鲱鱼?
我一直试图让量角器通过使用接受浏览器本地相机
browser.actions().sendKeys(protractor.Key.TAB, protractor.Key.TAB, protractor.Key.ENTER).perform();
Run Code Online (Sandbox Code Playgroud)
这会正确选中"接受"按钮,但输入键永远不会被提交.我也试过用
browser.actions().sendKeys(protractor.Key.TAB, protractor.Key.TAB).perform();
browser.actions().sendKeys(protractor.Key.ENTER).perform();
Run Code Online (Sandbox Code Playgroud)
认为可能需要进入它自己的路线.
有什么想法吗?
我已经采用了一些我之前使用过的memcache脚本没有问题,但在新页面上,我没有得到任何回复.
内存缓存位于另一个页面中的函数中.我所做的是将md5哈希值放入mysql查询并将其存储为memcached数据的密钥.如果密钥不在memcache中,那么我去创建数据,将其放入memcache并返回它.
我认为代码非常简单.这里有重要的部分(这是一个创建输出的大页面,所以你不需要所有这些,虽然我认为'返回'很重要,因为我担心这可能是我搞砸的地方.
我用这个函数调用
$outList.= outData($getList);
Run Code Online (Sandbox Code Playgroud)
其中$ getList是一个mysql查询
$ outList函数是
<?php
@$memcache = new Memcache;
@$memcache->connect("localhost",11211);
function outData($getList)
{
$memVal = @$memcache->get(MD5($getList));
if($memVal=='')
{
$results=mysql_query($getList)or die(mysql_error());
// then I do a bunch of stuff with the data
@$memcache->set(MD5($getList), $memVal, false, 60000);
}
return $memVal;
}
Run Code Online (Sandbox Code Playgroud)
我可以显示所有的东西来创建$ memVal,但我怀疑错误是在if行中,但是相同的代码在另一个页面上使用没有问题.
这有什么不妥吗?
我有一组字符串,我需要将其组合成一个数组.字符串需要拆分为"/",",","带"或"&".不幸的是,字符串可能包含需要拆分的两个字符串,因此我不能使用拆分或爆炸.
例如,一个字符串可以说'先过去/超越然后转',所以我试图得到一个数组,它将返回数组('先过去','超越',然后转'')
我目前使用的代码是
$splittersArray=array('/', ',', ' with ','&');
foreach($splittersArray as $splitter){
if(strpos($string, $splitter)){
$splitString = split($splitter, $string);
foreach($splitString as $split){
我似乎无法在PHP中找到允许我这样做的函数.我是否需要将字符串传回漏斗的顶部,并在字符串一次又一次地拆分后继续通过'foreach'?
这看起来效率不高.任何建议都会很棒.谢谢,皮特
我想制作一个简单的服务器,以便在开发时可以提供本地html和JS文件。
我试图让一个节点应用程序仅获取URL中的内容,然后对页面进行响应,但是没有运气(这是我对express的尝试)。
var fs = require(“ fs”);
var host =“ 127.0.0.1”;
var port = 1337;
var express = require(“ express”);
var app = express();
app.use(app.router); //同时使用root和其他路由
app.use(express.static('c:\\ users \\ pete \\ projects \\')); //使用ROOT / public文件夹中的静态文件
app.get(“ / *”,function(req,res){//根目录
fs.readFile(req.path,function(err,html){
如果(错误){
console.log(err);
返回;
}
res.write(html);
重发();
});
});
app.listen(端口,主机);
但这总是在处寻找文件c:\,而不是正确的路径。
我也尝试过将http-server用于简单的静态服务器,但是在提供js文件时,它总是崩溃。https://github.com/nodeapps/http-server
我需要能够简单地为您的基本html,css,js文件提供服务,并希望从URL中提供的任何路径提供服务。这仅用于本地前端开发。有什么帮助吗?
php ×3
backbone.js ×2
angularjs ×1
chaining ×1
collections ×1
encryption ×1
filter ×1
hash ×1
html ×1
jquery ×1
json ×1
memcached ×1
mysql ×1
node.js ×1
performance ×1
protractor ×1
selenium ×1
static ×1
string ×1
testing ×1