dongnong3799
2016-10-11 12:19
浏览 100
已采纳

Foreach循环:如果字符串等于前一个字符串

So, I am creating a history of changes page from database that has one row for each update. I need to represent only values that have changed. I represent those values in foreach loop (to make it more simple I reduced it to 1 value: work stations):

$sth = $pdo->prepare("
SELECT * FROM user_customers_history
");

$sth->execute();

foreach ($sth as $key => $row) {

$crm_workstations = $row['crm_workstations'];

if (($crm_workstations > 0) && ($crm_workstations != $crm_workstations)){

        echo '<br/><br/>' . $crm_workstations;

}

I defined $key as an index, but I don't know how to use it. I know that I want to represent it this way: for each index check if the value is equal to previous index value. If they are not equal, show it.

图片转代码服务由CSDN问答提供 功能建议

因此,我正在从数据库创建一个更改历史记录页面,每个更新都有一行。 我只需要表示已更改的值。 我在foreach循环中表示这些值(为了使它更简单我将它减少到1个值:工作站):

  $ sth = $ pdo-&gt; prepare(“\  nSELECT * FROM user_customers_history 
“); 
 
 $ sth-&gt; execute(); 
 
foreach($ sth as $ key =&gt; $ row){
 
 $ crm_workstations = $ row ['  crm_workstations']; 
 
if(($ crm_workstations&gt; 0)&amp;&amp;($ crm_workstations!= $ crm_workstations)){
 
 echo'&lt; br /&gt;&lt; br /&gt;'  。  $ crm_workstations; 
 
} 
   
 
 

我将$ key定义为索引,但我不知道如何使用它。 我知道我想以这种方式表示它:对于每个索引检查该值是否等于先前的索引值。 如果它们不相等,请显示它。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtao9095 2016-10-11 12:32
    已采纳

    Define a previous value for your variable to compare with the current one:

    $sth = $pdo->prepare("
    SELECT * FROM user_customers_history
    ");
    
    $sth->execute();
    
    $prev_workstations = '';
    
    while ($row = $sth->fetch()) { 
    
        $crm_workstations = $row['crm_workstations'];
        if (($crm_workstations > 0) && ($crm_workstations != $prev_workstations)){
    
            $prev_workstations = $crm_workstations;
            echo '<br/><br/>' . $crm_workstations;
    
        }  
    }
    

    They ask me to edit more then 6 characters I needed to write somthing :D

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题