• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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

0
rgranlund
Asked:
rgranlund
  • 5
  • 4
  • 4
2 Solutions
 
Ray PaseurCommented:
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?
0
 
rgranlundAuthor Commented:
$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"
0
 
Ray PaseurCommented:
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?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Slick812Commented:
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
0
 
rgranlundAuthor Commented:
@Ray That is correct.

@Slick, I need to show a count of 0-99.  It won't always be two digits.
0
 
Slick812Commented:
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.
0
 
Ray PaseurCommented:
Rather than try to explain what you want your application to do, can you please break this down into a bite-size question?  Just give us a simple example of the input you have and the output you want to get.  The reason that I am asking is because a statement like this one does not lead us to any kind of code solutions.
... 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... then loop the array to determine which images to show
It's easy to break a number into its component digits (cast the number to string and use substr() to find digits) but what array are we talking about?  What images?  I promise you that you will get better help faster if you give us the SSCCE so we do not have to guess at what you're asking for!
0
 
rgranlundAuthor Commented:
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

0
 
Ray PaseurCommented:
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
0
 
Slick812Commented:
OK, I looked at your programming "logic" for the last code, and as far as I can tell your IF( ) for the $intcartcount > 0 should use different a different Image, and seems like it shou show something for a single digit ( count($arrCartDigits) = 1 ), the PHP translator can some times not  tell what to group in multi-condition  IF () , ,so in my IF( ) statements statements, What I do is always put perenticies ( ) around the inner tests, , maybe this will help?-
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 ' . $intcartcount .'">';
    } else {
       echo '<img src="http://staging.mixxcentury.com/store/media/basket/basket-right-' . $arrCartDigits[$i] . '.png" class="layer' . $i .'" alt="Cart Count ' . $intcartcount .'">';
    };
} // you do NOT need a ; here, , the } is an indicator for an END to a code line read

Open in new window


if that does not work you might try -
$right = '';
for ($i=0;$i<count($arrCartDigits);$i++) {
    if ($intcartcount < 10) $right = 'right-';
    echo '<img src="http://staging.mixxcentury.com/store/media/basket/basket-' . $right . $arrCartDigits[$i] . '.png" class="layer' . $i .'" alt="Cart Count ' . $intcartcount .'">';
    $right = 'right-';
}

Open in new window

0
 
Slick812Commented:
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.
0
 
rgranlundAuthor Commented:
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

0
 
Slick812Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now