Solved

Place Variable into an Array then String

Posted on 2013-06-06
13
234 Views
Last Modified: 2013-06-09
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
Comment
Question by:rgranlund
  • 5
  • 4
  • 4
13 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39227572
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
 
LVL 7

Author Comment

by:rgranlund
ID: 39227628
$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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39227694
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 34

Expert Comment

by:Slick812
ID: 39227725
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
 
LVL 7

Author Comment

by:rgranlund
ID: 39227789
@Ray That is correct.

@Slick, I need to show a count of 0-99.  It won't always be two digits.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 39227927
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 39228652
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
 
LVL 7

Author Comment

by:rgranlund
ID: 39230653
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39230779
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
 
LVL 34

Accepted Solution

by:
Slick812 earned 350 total points
ID: 39231934
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
 
LVL 34

Expert Comment

by:Slick812
ID: 39231947
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
 
LVL 7

Author Comment

by:rgranlund
ID: 39232399
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
 
LVL 34

Expert Comment

by:Slick812
ID: 39233256
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question