doudan4834
2015-08-11 11:44 阅读 43
已采纳

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 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 = ',';
        }
    }               
    
    点赞 评论 复制链接分享

相关推荐