dtmjqyfz21793 2018-08-01 23:24
浏览 131

php - 字符串连接给出了奇怪的结果

I have built an OpenCart module and tested it on a few shops and it works well. One client just reported to me a problem and after inspecting the html I saw this:

My code in the .tpl template file:

<table class="table table-bordered table-hover" > 
 <tbody> 
   <?php foreach($row['strings'] as $row_string){   

     $key = $row_string['key']; 
     $id = $row['simpleFilePathEscaped'].$key;  ?>

     <tr>      

       <td class="text-center"> 
         <label><?php echo $key; ?> </label> 
       </td> 

       <td class="text-center">
         <div>
           <input  id="text_value_<?php echo $id; ?>" type="text" value="<?php echo $row_string['value']; ?>" placeholder="<?php echo $key; ?>" class="form-control" />
         </div>
       </td> 

       <td class="text-center" >
         <button  id="save_icon_<?php echo $id; ?>" onclick="addLiteralToFile('<?php echo $row['secondary_file_path_escaped'] ?>', '<?php echo $key; ?>', '<?php echo $id;?>');" class="btn btn-primary"><i id="save_inner_icon_<?php echo $id; ?>" class="fa fa-save"></i></button>
         <div id="loading_icon_<?php echo $id; ?>" <div  class="loader"></div> </div>  
       </td> 

     </tr>  
   <?php } ?> 
 </tbody>
</table>

The result for an example row in this client's shop is this:

<tr>                     
    <td class="text-center" > 
     <label>direction</label> 
    </td> 

    <td class="text-center">
     <div>
       <input id="text_value_" type="text" value="ltr" placeholder="direction" class="form-control">
     </div>
    </td> 

    <td class="text-center">
     <button id="save_icon_" onclick="addLiteralToFile('----home----admin----domains----itrend.si----public_html----test----admin----language----english----en-gb.php', 'direction', '');" class="btn btn-primary"><i id="save_inner_icon_" class="fa fa-save"></i></button>
     <div id="loading_icon_" <div="" class="loader"></div>   
    </td> 

</tr>

If you notice you will see that all instances of <?php echo $id; ?> give an empty string. so id="save_icon_<?php echo $id; ?>" becomes id="save_icon_"

That is extremely strange because $id is a concat of $row['simpleFilePathEscaped'] and $key. Even if $row['simpleFilePathEscaped'] is empty I know for sure that $key has a value...Because it is echoed and in this example it is "direction" (in the label tag)

Need some help finding out why this is happening...

$row = {  "strings" => array of  {  "key"=> string,  "value"=> string }, 
          "simpleFilePathEscaped" => string, 
          "secondary_file_path_escaped" => string, 
          "primary_file_path"=> string 
} 
  • 写回答

2条回答 默认 最新

  • dongyi9023 2018-08-02 00:31
    关注

    Sorry if i misunderstood the way your $row is build.

    if you make an example like

    $row['strings'] = ['key' => 10];
    $row['strings'] = ['key' => 110];
    
    foreach($row['strings'] as $row_string)
    $key = $row_string['key'];
    
    print_r($key); //will give you an empty value
    

    but print_r($row); // will give Array ( [strings] => Array ( ['key'] => 110 ) )`

    you need to change your foreach.

    foreach($row['strings'] as $row_key => $row_string)
    $key = $row_key;
    

    then it will give the 'key' on echo.

    or you can do

    foreach($row['strings'] as $row_string)
    $key = key($row_string);
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码