Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding up an array PHP convert to string.

Posted on 2011-02-16
5
Medium Priority
?
244 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 668 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 668 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 664 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 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 …
Suggested Courses

610 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