Solved

Adding up an array PHP convert to string.

Posted on 2011-02-16
5
240 Views
Last Modified: 2012-05-11
Hello experts,

Im having a few problems with my code. It works correctly apart from one line that does not work correctly and returns 0 this is the total box. im adding items to my shopping cart using a counter so they can also be removed. They are stored in an array as you can see from my code..

All i need help with is getting the $totalPrice variable to display the total of the $prices variable.. that is all and i cant seem to do it..

 Thank you for any help it is always appreciated.

please see lines 18 and 31 of code.

Thanks

<?php
      $descs= split('#', $_SESSION['Desc']);
      $sizes= split('#', $_SESSION['Size']);
      $prices= split('#', $_SESSION['Price']);
      $imgs= split('#', $_SESSION['Img']);
      $counter=0;
      $totalPrice = 0;
      while ($counter <= count($sizes) - 2) {
	$counter++;
      ?>
       <form method="post" action="mybag.php">
       <tr align="center">
       <input type="hidden" value="<?php print $counter ; ?>" name="itemNumber" />
        <td width="14%" valign="top" bordercolor="#666666"><input type="image" src="images/Remove.jpg" value="Remove" /></td>
        <td width="18%" valign="top" bordercolor="#666666"><img src="<?php print "$imgs[$counter]"; ?>" width="74" height="100" vspace="10" border="0" /></td>
        <td valign="top" bordercolor="#666666"><?php print "$descs[$counter]"; ?></td>
        <td width="14%" valign="top" bordercolor="#666666"><?php print "$sizes[$counter]"; ?></td>
      <td width="15%" valign="top" bordercolor="#666666"><?php print "$prices[$counter]"; $totalPrice = $totalPrice+intval($prices[$counter]); ?>
      
		 </td>
      </tr>
      </form>
      <?php 
	  } 
      ?>
        <td colspan="5" align="right">Delivery Charges</td>
        <td align="center">£3.60</td>
      </tr>
      <tr>
        <td colspan="5" align="right">Total</td>
        <td align="center"><?php  print '' . "$totalPrice"; ?></td>
      </tr>
      <tr>
        <td colspan="6" align="right"><?php echo '<a href="Mybag.php"><input type="image" value="Checkout" /></a>';?></td>
        </tr>
      
    </table>
      
<div align="center"></div></td>
</td>
  </tr>
</table>

        </div>
    </div>
    <script type="text/javascript">
<!--
ScrollLoad ("scrollholder_5", "scroll", true);
//-->
  </script>

Open in new window

0
Comment
Question by:NeoAshura
5 Comments
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 167 total points
ID: 34905548
It seems that maybe the intval() is unnecessary. It would sum only the integer values - what about decimal points in prices. Also make sure your $_SESSION['Prices'] has the format PRICE1#PRICE2#.....

It worked for me when I set the session manually, total price was calculated

// just for testing purposes
$_SESSION['Price'] = '22.40#45.23#33.33';
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34905556
Can you post the result of the
print_r($prices);
?
0
 
LVL 6

Assisted Solution

by:manuel_m
manuel_m earned 167 total points
ID: 34905563
Did you print out the value of $totalPrice in line 18 to check if the $totalPrice variable will be filled with the correct value?
Try to print the value in the next line to see if it will be filled.

<td width="15%" valign="top" bordercolor="#666666"><?php print "$totalPrice"; ?>

regards
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 166 total points
ID: 34905568
What is contained in $prices? Is there data in there? What does

print_r($prices);

show when it is run? Next you could use array_sum to sum up all the array values in one operation http://www.php.net/array_sum

$totalPrice = array_sum( $prices );

0
 
LVL 6

Author Comment

by:NeoAshura
ID: 34905693
My own fault.... I had the pound sign written in the database as pound&; thats why it didnt work once i changed it to the standard 10.00 insted of the pound&;10.00 my code worked.

All answers given are valid if i had an integer problem so points will still be awarded for participation.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

778 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