doudan4834 2015-08-11 11:44
浏览 44
已采纳

PHP循环不适用于使用MySQL变量的CSV输出

I created a variable from a MySQL query that gives a total number value of photos available for a record. This along with a few other variables from the query I can use to construct a dynamic URL in a CSV file I am exporting.

This works great:

  foreach($rowr as $name => $value)

  {
      if($name=='MatrixImage') {$jpg_name=$value;}
      if($name=='PhotoCount') {$count_name=$value;}     
      if($name != 'estate_property_gallery')
            $csv_output .= $value."|";


      if($name == 'estate_property_gallery')
    {
      $csv_output .= "http://url/feeds/".$value."/rets_images/".$jpg_name."_".$count_name.".jpg";
    }               

  }

I need to be able to loop through and create multiple URLs using this PhotoCount variable starting from 1 to the final value found in PhotoCount seperated by a comma but I can't seem to get it right.

Not working:

  foreach($rowr as $name => $value)

  {
      if($name=='MatrixImage') {$jpg_name=$value;}
      if($name=='PhotoCount') {$count_name=$value;}     
      if($name != 'estate_property_gallery')
            $csv_output .= $value."|";


      if($name == 'estate_property_gallery')
    {
      //$csv_output .= "http://url/feeds/".$value."/rets_images/".$jpg_name."_".$count_name.".jpg";
      for( $pic_start=1; $pic_start<=$count_name; $pic_start++ ){
    echo '$csv_output .= "http://url/feeds/' . $value . '/rets_images/' . $jpg_name . '_' . $pic_start . '.jpg,"';
}
    }               

  }   

The example above outputs the entire line including the $csv_output to the screen instead of putting each URL into the column of the CSV and delimiting with a comma.

To clarify: I need to be able to write this echo into the $csv_output .= portion of the code successfully so I can write multiple URLs separated by a comma within that column in my csv output:

echo 'http://www.myagentsbuddy.com/feeds/' . $value . '/rets_images/' . $jpg_name . '_' . $count_name . '.jpg';

  • 写回答

1条回答 默认 最新

  • dongman5539 2015-08-11 13:49
    关注

    If the first example you showed works great, and does not include an echo, and the second example you showed (with an echo) does not work, it seems like it could be fixed by removing the echo and going back to just appending the URLs to the $csv_output like you're doing in the working example.

    if($name == 'estate_property_gallery') {
        $comma = '';
        for( $pic_start=1; $pic_start<=$count_name; $pic_start++ ) {
            $csv_output .= $comma.'http://url/feeds/'.$value.'/rets_images/'
                          .$jpg_name.'_'.$pic_start.'.jpg';
            // doing the comma like this will prevent a trailing comma after the last entry
            $comma = ',';
        }
    }               
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料