开发者

How do I have values from an array show up in a php string?

I have two values in an array that I want to have show up in a string variable that 开发者_运维技巧I am building. Theses values are dynamic and are subject to change, so I want to be able to build the string straight from the array. I thought I was on the right track but the values are not showing up.

My Code:

$range = "WHERE transdate >= \"{$halfway['startdate']}\" AND transdate <= \"{$halfway['enddate']}\"";

The output:

WHERE transdate >= "" AND transdate <= ""


The following works for me in php 5.3.5 cli. I haven't modified your string

$halfway = array(
    'startdate' => 'somedate',
    'enddate' => 'otherdate'
);

$range = "WHERE transdate >= \"{$halfway['startdate']}\" AND transdate <= \"{$halfway['enddate']}\"";

print $range;

Output:

WHERE transdate >= "somedate" AND transdate <= "otherdate"

Check that $halfway actually has data.


Try removing the quotes in the array element.. I think that would work..

WHERE transdate >= \"{$halfway[startdate]}\" AND transdate <= \"{$halfway[enddate]}\"


Why do you use a framework and not use its ORM?

$this->ModelName->find('all', array(
    'conditions' => array(
        'ModelName.transdate BETWEEN ? AND ?' => array(
            $halfway['startdate'],
            $halfway['enddate']
         )
    )
));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜