Solved

Adding up an array PHP convert to string.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

706 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

18 Experts available now in Live!

Get 1:1 Help Now