0

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';

10
  • 1
    this => echo '$csv_output single quotes don't get parsed. Then a comma in '.jpg," if that is intentional or not, no idea. Then closing in a single quote. Fix that, code should work. Commented Aug 11, 2015 at 11:47
  • so basically echo $csv_output .= "http://url/feeds/' . $value . '/rets_images/' . $jpg_name . '_' . $pic_start . '.jpg,"; Commented Aug 11, 2015 at 11:52
  • @Fred-ii- still outputs to screen when I update it with this: echo "$csv_output .= 'url' . $value . '/rets_images/' . $jpg_name . '_' . $pic_start . '.jpg|';"; Commented Aug 11, 2015 at 12:16
  • if echo "$csv_output .= 'url' . $value . '/rets_images/' . $jpg_name . '_' . $pic_start . '.jpg|';"; there's an extra ; in there after '. - but you say it outputs to screen, what exactly do you mean by that? Commented Aug 11, 2015 at 12:17
  • output to screen shows a mangled column too instead of a URLs delimited by a comma. Commented Aug 11, 2015 at 12:18

1 Answer 1

1

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 = ',';
    }
}               
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Thanks so much for the help, basically looks like I over-complicated it LOL!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.