Solved

Adding up an array PHP convert to string.

Posted on 2011-02-16
5
239 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hacked File Timestamps 4 50
html input type 3 22
Could you possibly point why this Codeigniter method is been called twice? 7 25
mysql update statement 2 0
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now