首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >操作字符串信息和正则表达式。php

操作字符串信息和正则表达式。php
EN

Stack Overflow用户
提问于 2011-08-30 07:12:10
回答 1查看 66关注 0票数 0

我正在尝试检查表中的特定值(购物车结账),然后根据这些值显示提货日期。因此,如果我订购的产品具有圣诞节代码,则只会显示圣诞日历。或者,如果我为圣诞节和感恩节订购了一些东西,顾客会得到一份既是圣诞节又是感恩节的日历。

下面是代码(我非常确定我没有用正确的方法来做这件事)。我正在尝试绑定到现有的购物车模板系统中。所以我不能做直接查询。

代码语言:javascript
复制
<?php
foreach($this->rows as $i => $row){

if(preg_match("/thanks/",$row->dates)) { $thanks ='thanks';}else {$thanks = '';}
if(preg_match("/xmas/",$row->dates)) { $xmas ='xmas';}else {$xmas = '';}
if(preg_match("/newyears/",$row->dates)) { $newyears ='newyears';}else {$newyears = '';}

$mydates=$thanks.$xmas.$newyears;


//print_r($mydates);

}

?>

<?php
if(preg_match("/xmas/",$mydates)) { ?>
<tr><td class="key">mylabel</td><td>myselect box</td></tr>
<?php }?><?php
<?php
if(preg_match("/thanks/",$mydates)) { ?>
<tr><td class="key">mylabel</td><td>myselect box</td></tr>
<?php }?>
<?php
if(preg_match("/newyears/",$mydates)) { ?>
<tr><td class="key">mylabel</td><td>myselect box</td></tr>
<?php }?>

print_r返回正确的值,但是上面的代码只显示购物车中商品的一次(第一次)选择。

说有人从圣诞节下了订单,谢谢。然后,如果他们订购了感恩节print_r returns致谢,那么他们将返回the致谢

html看起来像列中的普通选择框。它们应该像print_一样显示,而不是只显示购物车中的最后一种商品类型,所以如果你先订购圣诞礼物,然后是感谢,那么只会显示从感恩节中选择。

EN

回答 1

Stack Overflow用户

发布于 2011-08-30 08:16:29

嗯..。像这样的东西应该可以工作--我相信其他人有更简单和更好的方法。尽量避免使用preg函数进行简单的匹配。strstr甚至strpos似乎可以在这里很好地工作。

如果您不需要它来创建整个表格,只需去掉表格的起始线和结束线。-实际上,现在我想起来了,你可以很容易地将两个循环组合成一个循环。

代码语言:javascript
复制
    <?php

    $datecodes = array();
    foreach($this->rows as $i => $row){

    if(strstr($row->dates, 'thanks')) { $datecodes[] = 'thanks';}
    if(strstr($row->dates, 'xmas')) { $datecodes[] = 'xmas';}
    if(strstr($row->dates, 'newyears')) { $datecodes[] = 'newyears'; }

    }




    if((is_array($datecodes)) && (count($datecodes) > 0)){

    $out .= '<table>';

    foreach($datecodes as $code){

      $out .= '<tr>';

        switch($code){

            case 'thanks':
                $out .= '<td class="key">Thanksgiving</td><td>';
                //Display Thanksgiving Calendar
                $out .= '</td>';
            break;

            case 'xmas':
                $out .= '<td class="key">Xmas</td><td>';
                //Display Xmas Calendar
                $out .= '</td>';
            break;

            case 'newyears':
                $out .= '<td class="key">New Years</td><td>';
                //Display New Years Calendar
                $out .= '</td>';
            break;
        }

      $out .= '</tr>';

    }

    $out .= '</table>';

    echo $out;
    }
    else{
    echo 'No Holiday Orders Found';

    }
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7237063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档