Php strpos没有按预期工作 [英] Php strpos not working as expected

查看:54
本文介绍了Php strpos没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我的眼睛现在变得疯狂。它应该工作,但事实并非如此。我将用户从多选选择中选择的值(县名)存储在逗号分隔的字符串中。然后,当表单被提交并被拒绝时,我正在使用该字符串来选择之前选择的那些。



我尝试过:



这是我的测试代码:

<?php 
echo'上一个选择:'。$ saved_counties;
echo'< br />< br />所有县的名单:';
foreach($县为$ one_county):
echo'< br />'.$one_county['County'];
if(strpos($ saved_counties,$ one_county ['County'])){
echo'(**** IN PREVOUS SELECTION ***)';
}
endforeach;
?>



当我运行代码时,这就是我得到的:



上一个选择:Baraga,Barry,Bay



所有县的列表:

Alcona

Alger
Allegan

Alpena

Antrim

Arenac

Baraga

Barry(****在Prevous SELECTION ***)

Bay(****在Prevous SELECTION ***)

Benzie

Berrien

分行

Calhoun

Cass

Charlevoix

Cheboygan
Chippewa

Clare

Clinton

Crawford

Delta

Dickinson

Eaton

Emmet



为什么跳过Baraga?我选择哪个组无关紧要。它总是跳过foreach代码中的第一个。我试过了:

 strpos($ saved_counties,$ one_county ['County'])> = 0 



那个结果相同。



我也试过:

 strpos($ saved_counties,$ one_county ['County' ])> 0 



最终重新选择了所有这些。



$县是一个存储所有83密歇根州的阵列县。 $ saved_counties只是一个逗号分隔的字符串,用于存储用户以前的选择。

解决方案

saved_counties;
echo'< br />< br />所有县的名单:';
foreach(


县作为


one_county):
echo'< br />'.<< blockquote >

This is making my eyes go nuts right now. It should work but it's not. I'm storing the values (county names) the user chose from a multiselect select in a comma delimited string. Then when the form is submitted and rejected I'm using that string to "select" the ones that were previously selected.

What I have tried:

Here's my test code:

<?php 
    echo 'Previous selection: '.$saved_counties;
    echo '<br /><br />List of all counties:';
    foreach ($counties as $one_county):
        echo '<br />'.$one_county['County'];
        if (strpos($saved_counties, $one_county['County'])) {
            echo ' (**** IN PREVOUS SELECTION ***)';
        }
    endforeach;
?>


When I run the code, this is what I get:

Previous selection: Baraga,Barry,Bay

List of all counties:
Alcona
Alger
Allegan
Alpena
Antrim
Arenac
Baraga
Barry (**** IN PREVOUS SELECTION ***)
Bay (**** IN PREVOUS SELECTION ***)
Benzie
Berrien
Branch
Calhoun
Cass
Charlevoix
Cheboygan
Chippewa
Clare
Clinton
Crawford
Delta
Dickinson
Eaton
Emmet

Why is it skipping "Baraga"? It doesn't matter which group I select. It always skips the first one in the "foreach" code. I tried:

strpos($saved_counties, $one_county['County']) >= 0


That rendered the same results.

I also tried:

strpos($saved_counties, $one_county['County']) > 0


That ended up reselecting ALL of them.

$counties is an array that stores ALL 83 michigan counties. $saved_counties is just a comma delimited string that stores the user's previous selection.

解决方案

saved_counties; echo '<br /><br />List of all counties:'; foreach (


counties as


one_county): echo '<br />'.


这篇关于Php strpos没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆