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 = ',';
        }
    }               
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失