Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Place Variable into an Array then String

I'm not 100% sure what I am doing wrong here:

Instructions:
1) create a single "tag" image for each number from 0-9 (basket-0.gif, basket-1.gif, basket-2.gif, etc.)
2) placed in the header template $intcartcount$this->helper('checkout/cart')->getSummaryCount();
3) take that number, split it into an array. so 25 would become [2,5], or 12 would become [1,2] then use those to determine which image to show in the basket count.
4) then loop the array to determine which images to show in the basket, like:
               
for ($i=0;$i<$array;$i++) {
                                echo "<img src='images/basket-" . $array[$i] . ".gif' class="layer' . $i .'">';
                };

Open in new window


Here is what I have so far. (I'm a newbie still learning:)
<?php $intcartcount = $this->helper('checkout/cart')->getSummaryCount();
$array = array($intcartcount);
echo $array[1];
for ($i=0;$i<$array;$i++) {
echo "<img src='images/basket-" . $array[$i] . ".gif' class='layer'" . $i ." alt='Cart Count'>";
}
?>	

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

This seems to be several questions!  You might want to take these issues one at a time.  It will undoubtedly be easier that way because the collective descriptions don't make sense.

I can help with #1.  Please see http://www.laprbass.com/RAY_temp_rgranlund.php
<?php // RAY_temp_rgranlund.php
error_reporting(E_ALL);

/* SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28150287.html
 *
 * 1) create a single "tag" image for each number from 0-9 (basket-0.gif, basket-1.gif, basket-2.gif, etc.)
 */

$nums = range (0,9);
foreach ($nums as $num)
{
    $out[$num] = 'basket-' . "$num" . '.gif';
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($out);

Open in new window

What would you like to do next?
Avatar of Robert Granlund

ASKER

$intcartcount returns a number, any where from 0-99.

what ever that number is I need to represent with a image of that number.  I have 10 images, 0-9.

so I need to have the array split the number to show the two different images.  Like "25"
Let me see if I can understand this part and paraphrase.  If $intcartcount == 25, you need to identify two data strings, something like this:

basket-2.gif
basket-5.gif

Is that on target?
you might try something like this -
$intcartcount = $this->helper('checkout/cart')->getSummaryCount();
$intcartcount = "".$intcartcount; // Change the Integer to a string
$array = array($intcartcount[0],$intcartcount[1]);// make array with first and second string characters
echo $array[0];
for ($i=0;$i<$array;$i++) {
echo '<img src="images/basket-' . $array[$i] . '.gif" class="layer' . $i .'" alt="Cart Count">';
}

Open in new window


This will only work every time IF the getSummaryCount() always returns an integer over 9
@Ray That is correct.

@Slick, I need to show a count of 0-99.  It won't always be two digits.
OK, I tried this on my server an it works for 2, 47 and 99 -
$intcartcount = 47;//$this->helper('checkout/cart')->getSummaryCount();
$intcartcount = "".$intcartcount;

for ($i=0; $i < strlen($intcartcount); $i++) {
echo '<img src="images/basket-' . $intcartcount[$i] . '.gif" class="layer' . $i .'" alt="Cart Count' . $i .'"><br />';
}

Open in new window

you might should re-code this some and experiment, to see if you can understand what it is doing.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What I am doing is creating a visual counter that displays how many items are in a shopping cart.  I am retrieving the total amount of items in the cart with:

$intcartcount = $this->helper('checkout/cart')->getSummaryCount();

Then what I need to do is show numbers 1-9 and then if it is higher show a different set: of images. I have attached an image to show you what I mean.  The following script works if the count is 10 or higher.  Im not sure why it is not showing 1-9.

<?php 

$intcartcount = $this->helper('checkout/cart')->getSummaryCount();
$strcartcount = "".$intcartcount;
$arrCartDigits = str_split($strcartcount);

for ($i=0;$i<count($arrCartDigits);$i++) {
    if ($intcartcount > 9 && $i==0) {
       echo '<img src="http://staging.mixxcentury.com/store/media/basket/basket-' . $arrCartDigits[$i] . '.png" class="layer' . $i .'" alt="Cart Count' . $i .'">';
    } else {
         echo '<img src="http://staging.mixxcentury.com/store/media/basket/basket-right-' . $arrCartDigits[$i] . '.png" class="layer' . $i .'" alt="Cart Count' . $i .'">';
    };
};

?>

Open in new window

How many images, and what image file names do you want to show in each of the following circumstances:

Zero Items in the cart
One Item
Eight Items
Nine Items
Ten Items
Thirty-six Items

Once we have those 6 examples clarified it should be easy to get the programming correct.

Standing by, ~Ray
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll just mention, that PHP strings are actually equivalent to "an Array of Characters", so you can access any SINGLE character in a PHP string with the [ ] array access for instance -
$str = "a2X!";
echo $str[0]; // will output "a" to browser
echo $str[1]; // will output "2" to browser
echo $str[strlen($str)-1]; // will output "!" to browser

Open in new window


this is NOT a hack, or ODD code practice, I use this extensively, and it ALWAYS works, in any PHP version, on any server that I have ever used. Because the C code underneath PHP uses a similar method for "string" character access.
Thank you all for your help.  I appreciate the encouragement and patience.  I think I was not looking at my issue clearly.  With your help, this is what I came up with:

<?php 

$intcartcount = $this->helper('checkout/cart')->getSummaryCount();
$strcartcount = "".$intcartcount;
$arrCartDigits = str_split($strcartcount);

for ($i=0;$i<count($arrCartDigits);$i++) {
    if (($intcartcount <= 99) && ($i==0) ) {
       echo '<img src="http://staging.mixxcentury.com/store/media/basket/basket-' . $arrCartDigits[$i] . '.png" class="layer' . $i .'" alt="Cart Count ' . $intcartcount .'">';
    } else {
       echo '<div class="basket-number-alt">
       			<img src="http://staging.mixxcentury.com/store/media/basket/basket-right-' . $arrCartDigits[$i] . '.png" class="layer' . $i .'" alt="Cart Count ' . $intcartcount .'">
			</div>';
    };
} 

?>	

Open in new window

OK, I looked at your last code, and in that comment, you do not say if that was "working" or not?, I can Not understand why you use this IF statement -
if (($intcartcount <= 99) && ($i==0) ) {

the  ($intcartcount <= 99)  part does NOT do anything, because the  $intcartcount can not be more than 99, and even if it can exceed 99, It would not effect a difference in Single Digit and Double Digit display?
Reading this with your NEW <DIV> as  "<div class="basket-number-alt">",  has me some what confused as to offering what suggestions to have effective display for this? ?
Also in your last code you still have a  ;  after a }  this will do no harm  , but is NOT NEEDED for proper PHP
Sorry but this next code is just me stumbling on no information, to offer help , the MAIN addition here is still another way to add the "right-" to the PNG name on the Rioght Side, by testing for the Second character in the  $arrCartDigits  array, and adding the "right-" to it. -
$intcartcount = $this->helper('checkout/cart')->getSummaryCount();
$strcartcount = "".$intcartcount;
$arrCartDigits = str_split($strcartcount);

if (isset($arrCartDigits[1])) {
    $arrCartDigits[1] = 'right-'.$arrCartDigits[1]; // add the 'right-'  to the array string
    }

for ($i=0;$i<count($arrCartDigits);$i++) {
    if ($i == 1) echo '<div class="basket-number-alt">
        ';  // you can leave OUT this echo for div if you need to, I do not know what it is for.
    echo '<img src="http://staging.mixxcentury.com/store/media/basket/basket-' . $arrCartDigits[$i] . '.png" class="layer' . $i .'" alt="Cart Count ' . $intcartcount .'" />';
    if ($i == 1) echo '
    </div>';
    }

Open in new window

This uses a single ECHO for every image and does NOT need to test for the length of the array inside the FOR
I have also added the "/>"  at the end os the Image Tag, this is the new standard for web page DHTML as XHTML, you should learn it.