Page 1 of 1

Taking an Array, strip out only prices, then add them?

Posted: Wed Apr 19, 2006 1:30 pm
by mercuryinhell
I am trying to create a quote system.

Pg 1: Pick Vehicle (done)
Pg 2: Pick Accessories using check boxes, and slam them into an array. (done)
Pg 3: Retun the array (done), strip out all the pricing, and add them together, and then create a variable equal to the total.
Pg 4: Make it all look nice for a print out.

I need help on page 3 :)

Here is my code so far:

Code: Select all

<?php
# GRAB THE VARIABLES FROM THE FORM
$expr1 = $_POST['expr1'];
$msrp = $_POST['msrp'];
$type = $_POST['type'];
$make = $_POST['make'];
$expr2 = $_POST['expr2'];
$msrpA = $_POST['msrpA'];
$FitMK = $_POST['FitMK'];
$acclist = $_POST['chkexpert'];
$today = date("F j, Y, g:i a");                
?>
<B>Vehicle: </B><?php echo $expr1;?><BR>  

<?php
$tax = (($msrp+$msrpA)*1.06)-($msrp+$msrpA)
?>
<B>Tax: </B>$<?php
printf("%10.2f\n", $tax ); 
?><BR>

<B>Title: </B>

<?php
if ($type == "MC") {
echo "$150.00";
$titleprice = "150.00";
}
?>

<?php
if ($type == "UTIL") {
echo "$120.00";
$titleprice = "120.00";
}
?>

<?php
if ($type == "ATV") {
echo "$102.50";
$titleprice = "102.50";
}
?>
<BR>
<?php
$total = ($tax+$titleprice+$msrp+$msrpA)
?>
<B>Vehicle Total: </B>$<?php
printf("%10.2f\n", $total ); 
?><BR>
<BR>


<?php 

if(isset($chkexpert)) 
{ 
   print '<B>Accessories:</B> <BR>';
   $n = count($chkexpert); 
   for($i = 0; $i <= $n; $i++) 
   { 
     print "" . $chkexpert[$i] . "<BR>"; 
  } 
} else 
 
?> 


<form method='POST' action='test4.php'>

<input type="hidden" value="
<?php 

if(isset($chkexpert)) 
{ 
   $n = count($chkexpert); 
   for($i = 0; $i <= $n; $i++) 
   { 
     print "" . $chkexpert[$i] . "<BR>"; 
  } 
} else 
 
?> 
" name="acclist">

<?php
print "

	
<table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' width='62%' id='table1'>
		<tr>
		<td colspan='4'><font face='Arial'><b>Customer Information</b></font></td>
	</tr>
	<tr>
		<td width='10%'><font face='Arial' size='2'>Name:</font></td>
		<td><input type='text' name='name' size='43'></td>
		<td width='6%'><font face='Arial' size='2'>Phone</font></td>
		<td><input type='text' name='phone' size='26'></td>
	</tr>
	<tr>
		<td width='10%'><font face='Arial' size='2'>Address:</font></td>
		<td><input type='text' name='address' size='43'></td>
		<td width='6%'><font face='Arial' size='2'>Email</font></td>
		<td><input type='text' name='email' size='26'></td>
	</tr>
	<tr>
		<td width='10%'>&nbsp;</td>
		<td><input type='text' name='city' size='31' value='City'><input type='text' name='state' size='5' value='State'><input type='text' name='zip' size='5' value='Zip'></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td width='10%'><font face='Arial' size='2'>SSN:</font></td>
		<td><input type='text' name='ssn' size='43'></td>
		<td><font face='Arial' size='2'>Salesman</font></td>
		<td><select size='1' name='salesman'>
		<option>BS</option>
		<option selected>Please Select</option>
		<option>MH</option>
		<option>JF</option>
		<option>JM</option>
		<option>DS</option>
		<option>BK</option>
		<option>CR</option>
		</select></td>
	</tr>
	<tr>
		<td width='10%'><font face='Arial' size='2'>DOB:</font></td>
		<td><input type='text' name='dob' size='43'></td>
		<td><font face='Arial' size='2'>Date</font></td>
		<td>
		<input type='text' name='date' size='20' onfocus='this.blur()' value='$today'></td>
	</tr>
	<tr>
		<td width='10%'>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
</table>
  <input type='hidden' value='$expr2' name='expr2'>
  <input type='hidden' value='$msrpA' name='msrpA'>
  <input type='hidden' value='$FitMK' name='FitMK'>
  <input type='hidden' value='$expr1' name='expr1'>
  <input type='hidden' value='$msrp' name='msrp'>
  <input type='hidden' value='$type' name='type'>
  <input type='hidden' value='$make' name='make'>
  
    <input type='hidden' value='$tax' name='tax'>
  <input type='hidden' value='$titleprice' name='title'>
  <input type='hidden' value='$total' name='total'>
  
  <p><input type='submit' value='Submit' name='B1'></p>
</form>"; 
php?>
CHKEXPERT is the array.

Any guidance?

Posted: Wed Apr 19, 2006 1:33 pm
by mercuryinhell
Right now the results for the array look like this:

Accessories:
(7484802) 2.5 Warn Winch $479.99
(7484804) Glacier Plow Frame $99.99

Posted: Wed Apr 19, 2006 2:24 pm
by mercuryinhell
I know nothing about strings, but in a perfect world, I would think we could take the results:

(7484802) 2.5 Warn Winch $479.99
(7484804) Glacier Plow Frame $99.99

Remove all the the left of the $:
$479.99
$99.99

Remove $:
479.99
99.99

Add the results: 579.98

Then create a variable equal to 579.98

Posted: Wed Apr 19, 2006 2:25 pm
by feyd

Posted: Wed Apr 19, 2006 2:49 pm
by pickle
Are the contents of $chkexpert something like this:

Code: Select all

[0] => $129.99
[1] => $49.99
[2] => $2000.87

...etc

Posted: Thu Apr 20, 2006 8:38 am
by mercuryinhell
pickle wrote:Are the contents of $chkexpert something like this:

Code: Select all

[0] => $129.99
[1] => $49.99
[2] => $2000.87

...etc
Nope, the results look like this:
(7484802) 2.5 Warn Winch $479.99
(7484804) Glacier Plow Frame $99.99

I can't get the results I am looking for...

Posted: Thu Apr 20, 2006 9:30 am
by mercuryinhell
I think this is the code I need to put the str_replace() in, but because it is an array, I am running into trouble.

Code: Select all

<?php 
if(isset($chkexpert)) 
{ 
   print '<B>Test</B> <BR>';
   $n = count($chkexpert); 
   for($i = 0; $i <= $n; $i++) 
   { 
     print "" . $chkexpert[$i] . "<BR>"; 
  } 
} else 
 
?>
I tried using this example just to see if it would work:

Code: Select all

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
But it got me 3 lines that looked like this
r
r
y

Which I believe is it saying Array, but taking out the A & a.


This is what I had:

Code: Select all

<?php 
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "$chkexpert");
if(isset($chkexpert)) 
{ 
   print '<B>Test</B> <BR>';
   $n = count($chkexpert); 
   for($i = 0; $i <= $n; $i++) 
   { 
     print "" . $onlyconsonants[$i] . "<BR>"; 
  } 
} else 
 
?>
What am I doing wrong guys?
Sorry, I only started picking up PHP 2 days ago.
Thanks!

Ok, some progress

Posted: Thu Apr 20, 2006 10:08 am
by mercuryinhell
Using both strpbrk & str_replace, I was able to get the results I want on one of the variables, but not the array I need it for.

Code: Select all

<?php

$text = "$expr1";
$vowels = array("$");
$text2 = strpbrk($text, '$');

$onlyconsonants = str_replace($vowels, "", "$text2");
?>
<?php echo $onlyconsonants;?>
This takes the string:
[klx123456789a] : 2005 Kawasaki Ninja 636 (Black) $8999

and returns only the price of 8999.

Now, how do I make it work with my array above?

Here is my plan.

Posted: Thu Apr 20, 2006 10:45 am
by mercuryinhell

Code: Select all

<?php 
if(isset($chkexpert)) 
{ 
   print '<B>Test</B> <BR>';
   $n = count($chkexpert); 
   for($i = 0; $i <= $n; $i++) 
   { 
     print "" . $chkexpert[$i] . "<BR>"; 
  } 
} else 
 
?> 

<?php
$text = "$chkexpert[$i]";
$vowels = array("$");
$text2 = strpbrk($text, '$');
$onlyconsonants = str_replace($vowels, "", "$text2");
$a = array($onlyconsonants);
echo array_sum($a);
?>
The result is 0.
It should be the addition of any of the accessories I choose. Like 150.23 + 479.99 = 630.22
See below:

TEST CODE//
Test
(7484805) Glacier Plow 52in. Straight Blade $150.23
(7484802) 2.5 Warn Winch $479.99

0 //TEST CODE END

Posted: Thu Apr 20, 2006 11:45 am
by feyd
might need/want to look at preg_match() and its siblings.

Think I got it.

Posted: Thu Apr 20, 2006 11:52 am
by mercuryinhell
Because people will only choose about 10 accessories, the array could be static, lol. DUH!
This will do for now I think. Post if you guys can think of a better solution.

THANKS FOR ALL OF YOUR HELP!

Code: Select all

<?php 
if(isset($chkexpert)) 
{ 
   print '<B>Test</B> <BR>';
   $n = count($chkexpert); 
   for($i = 0; $i <= $n; $i++) 
   { 
     print "" . $chkexpert[$i] . "<BR>"; 
  } 
} else 
 
?> 

<?php
$arr0 = "$chkexpert[0]";
$arr1 = "$chkexpert[1]";
$arr2 = "$chkexpert[2]";
$arr3 = "$chkexpert[3]";
$arr4 = "$chkexpert[4]";
$arr5 = "$chkexpert[5]";
$arr6 = "$chkexpert[6]";
$arr7 = "$chkexpert[7]";
$arr8 = "$chkexpert[8]";
$arr9 = "$chkexpert[9]";
$arr10 = "$chkexpert[10]";

$text0 = "$chkexpert[0]";
$text1 = "$chkexpert[1]";
$text2 = "$chkexpert[2]";
$text3 = "$chkexpert[3]";
$text4 = "$chkexpert[4]";
$text5 = "$chkexpert[5]";
$text6 = "$chkexpert[6]";
$text7 = "$chkexpert[7]";
$text8 = "$chkexpert[8]";
$text9 = "$chkexpert[9]";
$text10 = "$chkexpert[10]";

$vowels = array("$");

$str0 = strpbrk($text0, '$');
$str1 = strpbrk($text1, '$');
$str2 = strpbrk($text2, '$');
$str3 = strpbrk($text3, '$');
$str4 = strpbrk($text4, '$');
$str5 = strpbrk($text5, '$');
$str6 = strpbrk($text6, '$');
$str7 = strpbrk($text7, '$');
$str8 = strpbrk($text8, '$');
$str9 = strpbrk($text9, '$');
$str10 = strpbrk($text10, '$');

$onlyconsonants0 = str_replace($vowels, "", "$str0");
$onlyconsonants1 = str_replace($vowels, "", "$str1");
$onlyconsonants2 = str_replace($vowels, "", "$str2");
$onlyconsonants3 = str_replace($vowels, "", "$str3");
$onlyconsonants4 = str_replace($vowels, "", "$str4");
$onlyconsonants5 = str_replace($vowels, "", "$str5");
$onlyconsonants6 = str_replace($vowels, "", "$str6");
$onlyconsonants7 = str_replace($vowels, "", "$str7");
$onlyconsonants8 = str_replace($vowels, "", "$str8");
$onlyconsonants9 = str_replace($vowels, "", "$str9");
$onlyconsonants10 = str_replace($vowels, "", "$str10");

$a = array($onlyconsonants0, $onlyconsonants1, $onlyconsonants2, $onlyconsonants3, $onlyconsonants4, $onlyconsonants5, $onlyconsonants6, $onlyconsonants7, $onlyconsonants8, $onlyconsonants9, $onlyconsonants10);

echo array_sum($a);
?>

Posted: Thu Apr 20, 2006 2:52 pm
by pickle
Oh blech. ;) Writing out all 10 pulls at my optimization heart strings. Give this a try:

Code: Select all

//set up test array
$chkexpert[] = '(7484805) Glacier Plow 52in. Straight Blade $150.23';
$chkexpert[] = '(7484802) 2.5 Warn Winch $479.99';
$chkexpert[] = '[klx123456789a] : 2005 Kawasaki Ninja 636 (Black) $8999';

//define the pattern to extract the price
$price_pattern = '/.*?\$(\d+\.{0,1}\d*)/';

foreach($chkexpert as $item)
{
   //extract the price from each line
   preg_match($price_pattern,$item,$matches);
   $prices[] = $matches[1];
}
//sum the prices
$total_price = array_sum($prices);

echo $total_price;

Thanks!

Posted: Fri Apr 21, 2006 12:31 pm
by mercuryinhell
That seems a lot better. Thanks for all your help guys! I couldn't have done it without your help.