Solved

Adding up an array PHP convert to string.

Posted on 2011-02-16
5
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unwanted output from my query 5 41
Multi line FPDF footer: 3 26
PHP encrypted string and passing to a ASP Page 12 46
How to get chosen background-color on every line? 10 20
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

749 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