first entry in a joined list variable being skipped

9 posts by 2 authors in: Forums > CMS Builder
Last Post: July 25, 2013   (RSS)

By gkornbluth - July 24, 2013

Hi All,

In the code below I’m trying to display images for all colors that are available for a particular item.

Here’s my approach:
 
First I created a variable ($colors1) which contains a comma separated list of all of the colors that are available for the item. (This works.)

Next I uploaded images representing all of the available colors in a multi-image upload field and entered the color of the image in the info1 field for each image. So far so good...

Then in a foreach loop, I determine if the info1 value exists in the $colors1 variable and if it does, display that thumbnail image.

Problem is, the image corresponding to the first color in the $colors1 variable is always skipped, (it's always the first one) and I can’t figure out why.

You can see a mockup of the page at  http://www.rcartierperry.com/storeadetail.php?1 (The Blue image is missing. The space is because I'm using colspan="2".)

Thanks for looking...

Jerry Kornbluth

<table>
 <tr>
<td colspan="2">
<?php $colors1 = join(', ', $store_inventoryRecord['colors:labels']); ?>
<span class="navigation_font">Available Colors: <?php echo $colors1 ?></span><br/>
</td>
</tr>
<tr>
<?php foreach ($store_inventoryRecord['image'] as $upload): ?>
<?php $info1 = $upload['info1'] ?>
<?php if(strpos($colors1, $info1)):?>
 <td align="center">
<span class="sub_heading_font"><?php echo $upload['info1'] ?></span><br />
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" />
</td>
<?php endif ?>
<?PHP $maxCols=2; if (@++$count % $maxCols == 0): ?></tr><tr><?PHP endif; ?>
<?php endforeach ?>
</tr>
</table>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gregThomas - July 24, 2013

Hi Jerry,

I can't see anything obviously wrong with your code, would you mind running a showme function on your store_inventoryRecord variable so I can see how the data is set up? 

showme($store_inventoryRecord);
exit;

You need to run this after the $store_invetoryRecord is created in the top of your PHP file. Then if you could copy the array that is output into a post for me I can see what the problem is.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - July 24, 2013 - edited: July 24, 2013

Here you go: If I've missed anything, you can see the entire output at http://www.rcartierperry.com/storeadetail-showme.php?Clothespin-Pattern-1-Stone-Colors-1-1 and I've attached the page code.

Array

(

[_filename] => Clothespin-Pattern-1-Stone-Colors-1

[_link] => /storeadetail.php?Clothespin-Pattern-1-Stone-Colors-1-1

[_tableName] => store_inventory [artwork_for_sale_on_line] => 1

[artwork_for_sale_on_line:text] => Yes

[colors] => 2 5 1 3

[colors:labels] => Array

(

[0] => Blue

[1] => Gold

[2] => Red

[3] => Platinum )

[colors:values] => Array

(

[0] => 2

[1] => 5

[2] => 1

[3] => 3

)

And thank you

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

storeadetail.php 18K

By gregThomas - July 25, 2013

Hi Jerry,

There isn't an image key in the array that you posted. Yet your getRecords function is loading uploads, and your page is displaying the images correctly so it looks as if they should be in there. Was this the contents of the entire array? 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - July 25, 2013

Hi Jerry,

I'm still not sure what is causing the problem, but it might because Blue is the first item in the string, and so doesn't have space and comma first. Perhaps you could try using this to detect if the color has been selected or not:

<table>
  <tr>
    <td colspan="2">
      <?php $colors1 = join(', ', $store_inventoryRecord['colors:labels']); ?>
      <span class="navigation_font">Available Colors: <?php echo $colors1 ?></span><br/>
    </td>
  </tr>
  <tr>
    <?php foreach ($store_inventoryRecord['image'] as $upload): ?>
    <?php $info1 = $upload['info1'] ?>
    <?php if(in_array($store_inventoryRecord['colors:labels'], $info1)):?>
      <td align="center">
        <span class="sub_heading_font"><?php echo $upload['info1'] ?></span><br /> 
        <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" />
      </td>
    <?php endif ?>
    <?php $maxCols=2; if (@++$count % $maxCols == 0): ?></tr><tr><?PHP endif; ?>
  <?php endforeach ?>
  </tr>
</table>

So instead of checking if the color is in a string, we check if it's in the labels array instead. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - July 25, 2013 - edited: July 25, 2013

Very perceptive, but when I try the code you suggested, I get the errors:

Warning: in_array(): Wrong datatype for second argument in /hsphere/local/home/c382004/rcartierperry.com/storeadetail.php on line 169 Warning: in_array(): Wrong datatype for second argument in /hsphere/local/home/c382004/rcartierperry.com/storeadetail.php on line 169 Warning: in_array(): Wrong datatype for second argument in /hsphere/local/home/c382004/rcartierperry.com/storeadetail.php on line 169 Warning: in_array(): Wrong datatype for second argument in /hsphere/local/home/c382004/rcartierperry.com/storeadetail.php on line 169

Interesting that there are 4 errors and 4 images

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gregThomas - July 25, 2013

Oops! 

I've got the two variables in the in_array the wrong way around. I'm going to blame PHP's inconsistent use of needle/haystack in functions. ;-) 

Try this instead:

<table>
  <tr>
    <td colspan="2">
      <?php $colors1 = join(', ', $store_inventoryRecord['colors:labels']); ?>
      <span class="navigation_font">Available Colors: <?php echo $colors1 ?></span><br/>
    </td>
  </tr>
  <tr>
    <?php foreach ($store_inventoryRecord['image'] as $upload): ?>
    <?php $info1 = $upload['info1'] ?>
    <?php if(in_array($info1,$store_inventoryRecord['colors:labels'])):?>
      <td align="center">
        <span class="sub_heading_font"><?php echo $upload['info1'] ?></span><br /> 
        <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" />
      </td>
    <?php endif ?>
    <?php $maxCols=2; if (@++$count % $maxCols == 0): ?></tr><tr><?PHP endif; ?>
  <?php endforeach ?>
  </tr>
</table>

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - July 25, 2013

Hi Greg,

That seems to have solved the issue, so I guess that your guess was a good guess.

Thanks,

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php