现在我正在使用下面的查询对每篇文章auto_increment id进行排序
mysql_query("SELECT * FROM articles ORDER BY id DESC");
我想知道如何按我所做的日期字段进行排序,通过,strtotime()存储当前日期,它应查询从最新到最旧的日期.
目前的代码
$alist = mysql_query("SELECT * FROM articles ORDER BY id DESC");
$results = mysql_num_rows($alist);
if ($results > 0){
while($info = mysql_fetch_array($alist)) {
// query stuff
echo $info['time'];
}
我刚写了这个,这是将数组添加到预先存在的数组的最有效方法.
$c=4;
$i=1;
$myarray = array();
while($i <= $c):
array_push($myarray, array('key' => 'value'));
$i++;
endwhile;
echo '<pre><code>';
var_dump($myarray);
echo '</code></pre>';
Run Code Online (Sandbox Code Playgroud)
更新:如何在不创建新阵列的情况下推送键和值.
所以这array_push($myarray,'key' => 'value');
不是这个array_push($myarray, array('key' => 'value'));
如何在字符串上使用jQuery .each()
// For Exmaple
var mystring = '<div> bleh content </div> <div> bleh content </div>';
$('div', mystring).each(function(e) {
alert('do something');
});
//上面的代码是不是为字符串中的每个div启动警报?我不知道为什么?
这应该很简单,但我无法弄清楚
例如,假设类.contentdiv正在搜索.
我想获取(或选择)文档中的第二个或(x金额).contentdiv然后获取该div的html.
x是我想要选择的div,所以假装x是1,2或3或任何数字
jQuery('#slider').filter('.contentdiv').match(x).html();
Run Code Online (Sandbox Code Playgroud) 在jQuery中你可以编写类似的东西
$(selector).bind('click', function(e) {
// something was true - do these actions.
});
Run Code Online (Sandbox Code Playgroud)
我想知道如果在php中你可以做一些类似的东西而不使用eval.
这样的事情?我知道这不会工作顺便说一句.
class act{
public function bind($pref, $callback) {
if($pref == 'something' ) {
// return and perform actions in $callback?
eval($callback);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有些人可能会问这是什么需要?好吧,我正在尝试简化我的代码,而不使用如此多的if语句.
$act = new act;
$act->bind('something', function(e) {
echo 'this was the php code to run in the callback';
});
Run Code Online (Sandbox Code Playgroud)
上面的代码将避免使用一堆if语句.
$act = new act;
if( $act->bind('something') ) {
// bind returned true so do this?
}
Run Code Online (Sandbox Code Playgroud)
我知道你可以使用ob_get_contents来返回被破坏的代码,但是ewww.
ob_start();
eval('?> …Run Code Online (Sandbox Code Playgroud) 我最近问了一个类似的问题,但没有得到一个明确的答案,因为我太具体了.这个更广泛.
有谁知道如何在正则表达式模式中替换(x)出现?
示例:假设我想替换字符串中第5次出现的正则表达式.我该怎么办?
这是模式:
preg_replace('/{(.*?)\|\:(.*?)}/', 'replacement', $this->source);
@anubhava请求的示例代码(最后一个函数不起作用):
$sample = 'blah asada asdas {load|:title} steve jobs {load|:css} windows apple ';
$syntax = new syntax();
$syntax->parse($sample);
class syntax {
protected $source;
protected $i;
protected $r;
// parse source
public function parse($source) {
// set source to protected class var
$this->source = $source;
// match all occurrences for regex and run loop
$output = array();
preg_match_all('/\{(.*?)\|\:(.*?)\}/', $this->source, $output);
// run loop
$i = 0;
foreach($output[0] as $key):
// …Run Code Online (Sandbox Code Playgroud) 我想只对<code>中要剥离的内容</ code>中的内容运行htmlentities()
我写了一个函数,它接受一个字符串并在<code> </ code>之间找到内容
function parse_code($string) {
// test the string and find contents with <code></code>
preg_match('@<code>(.*?)</code>@s', $string, $matches);
// return the match if it succeeded
if($matches) {
return $matches[1];
}else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
function parse_code($string) {
<div class="myclass" rel="stuff"> things in here </div>
<code> only run htmlentites() here so strip out things like < > " ' & </code>
<div> things in here </div>
但是我需要一些实际运行htmlentities()的函数的帮助; 在<code> </ code>中的内容然后implode()将它们全部重新组合在一起.例如,假设我们在下面有字符串.
// test the string and find contents with …Run Code Online (Sandbox Code Playgroud) 我需要在删除Backbone.js视图时触发一个函数.我想有点像去构造函数.下面的代码是我写的代码片段; 我知道它不会起作用.但是,我确实记得在过去看过如何编写执行此操作的函数的视频教程.我需要运行解构函数的原因是在删除视图时清除视图内设置的间隔.
ViewWeather = Backbone.View.extend({
interval: setInterval(function() {console.log('interval fire');}, 1000),
// made up function
deconstructor: function () {
// if the view is removed
clearInterval(this.interval);
}
});
var viewweather = new ViewWeather();
基本上我想做一个基于base64_encodes自己5次的php循环.
//例如,我想编码"test",即"dGVzdA ==",
然后编码"dGVzdA ==",即"ZEdWemRBPT0 =",
然后编码"ZEdWemRBPT0 =",即"WkVkV2VtUkJQVDA9"
我无法弄清楚如何创建一个循环,每次运行时修改它自己.
// this is what i had
function enloop($dowork){
for ($i=1; $i<=5; $i++)
{
return base64_encode($dowork);
}
}
enloop($code);
Run Code Online (Sandbox Code Playgroud)
这个脚本只重复编码5次,让你说你的编码单词测试例如输出就是dGVzdA == dGVzdA == dGVzdA == dGVzdA == dGVzdA ==
这不是我想要的.
php ×6
html ×2
jquery ×2
ajax ×1
arrays ×1
backbone.js ×1
callback ×1
implode ×1
intervals ×1
javascript ×1
mysql ×1
regex ×1
return ×1
string ×1
while-loop ×1