0

I am not sure exactly how to word it, but I am trying to input a caption along with photograph. The for() autoincreases and coincides with the field name.

Here is the code I have so far:

for ($i=1;$i<=21;$i++) {
        if (file_exists(ABSPATH.'wp-content/uploads/fsrep/houses/additional/small/'.$ListingDetails->listing_id.'-'.$i.'.jpg')) {
            if (($ListingDetails->listing_caption."_".$i) != '' ) {$ListingCaption = ($ListingDetails->listing_caption.'_'.$i);} else{$ListingCaption = 'View Slideshow';}
            $AdditionalImages .= '<td align="center" valign="center"><a href="'.get_option('home').'/wp-content/uploads/fsrep/houses/additional/large/'.$ListingDetails->listing_id.'-'.$i.'.jpg" title="'.$ListingCaption.'" rel="lightbox[slideshow]" onmouseover="document.getElementById(\'fsrep-main-image-img\').src=\''.get_option('home').'/wp-content/uploads/fsrep/houses/additional/'.$ListingDetails->listing_id.'-'.$i.'.jpg\'; document.getElementById(\'fsrep-main-image-a\').rel=\'lightbox[slideshow]\'" onmouseout="document.getElementById(\'fsrep-main-image-img\').src=\''.get_option('home').'/wp-content/uploads/fsrep/houses/'.$ListingDetails->listing_id.'.jpg\'; document.getElementById(\'fsrep-main-image-a\')"><img src="'.get_option('home').'/wp-content/uploads/fsrep/houses/additional/small/'.$ListingDetails->listing_id.'-'.$i.'.jpg" class="full" /></a></td>';
            if ($i == 7 || $i == 14|| $i == 21) {
                $AdditionalImages .= '</tr><tr>';
            }
        }
    }

I am tying to get it to output the $listing_caption correctly, but it isn't reading the field name as for example $ListingDetails->listing_caption_1 instead I think it is reading it as $ListingDetails->listing_caption, but I am not sure.

The fields in the mysql database are for example:

listing_caption_1, listing_caption_2, listing_caption_3, etc.
3
  • what is $ListingDetails? is it row from mysql_fetch_row? Commented Oct 26, 2011 at 2:02
  • This in fact has nothing at all to do with photos, or auto-increasing numbers, or databases. It's just a question about how to use a variable with a dynamic name. Please narrow down your question to a minimal issue next time! Commented Oct 26, 2011 at 2:30
  • @TomalakGeret'kal To quote the first thing I wrote "I am not sure exactly how to word it". Thank you for your contribution to my question. Commented Oct 26, 2011 at 23:38

2 Answers 2

3

Try:

$capt_num = "listing_caption".'_'.$i;
$ListingCaption = $ListingDetails->$capt_num;
...
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure what your $ListingDetails is, but if it represents a database, and you populate it with say, mysql_fetch_assoc(), you can turn:

$ListingCaption = ($ListingDetails->listing_caption.'_'.$i);

into:

$ListingCaption = $ListingDetails['listing_caption_'.$i];

Assuming that's where you're having trouble.

Comments

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.