kurrent@gmail.com wrote:
Quote:
i'm still new to php and programming and was hoping to get some help
on a few questions i haven't had success in answering myself. I
successfully created my first very simple script to accomplish a task
I wanted, but not completely. see my code below:
>
<?php
>
//my first ever php program simply designed to automatically adjust
new prices of my properties
>
//current property prices
$prices = array('250000', '250000', '350000');
>
//percentage of how much to add to current prices
$increase_rate = '0.015'; // 1.15 %
>
//add new percentage to each price
foreach ($prices as &$newprice) {
$newprice = $newprice + ($newprice * $increase_rate);
}
>
//print out all new prices
for ($key = 0; $key < 3; $key++) {
print "$prices[$key]<br />";
}
>
<pre>
<?php
$prices = array('250000', '250000', '350000');
$increase_rate = '0.015';
function increase_price(&$item,$key,$rate){
$item = number_format(floatval(str_replace(',','',$item)) * (1 +
$rate),0,'',',');
}
$oldprices = $prices;
array_walk($prices,'increase_price',$increase_rate );
$changes = array_map(null,$oldprices,$prices);
print_r($changes);
?>
</pre>
Quote:
e.g. -echo the following structure:
set an array of values, then do a function to
manipulate the values, then show the old
values in comparison of the new values
(basically, show that $253,750 used to be
$250,000)
print_r() will show you the arrays, if it's just to check for the
programmer, for a form/UI offcourse it will be better to give it some sort
of layout, vprintf() is very handy for this.
Quote:
2.) a) What if i have commas in my prices by default?
>
e.g. = array('349,502' '234,995');
Removed by str_replace();
Quote:
b) How would I go about processing comma values
received via a form that a user entered or from .CSV
file that looked like (132,500, 123,340, 345,959,
244,459)?
str_replace could be used for this, but if you're not absolutely sure about
your data, I'd use $price = preg_replace('/[^0-9]/','',$price); (on the
condition you never use decimals, otherwise you might want to use
'/[^0-9\.]/' instead).
Quote:
c) How would I add a comma into my printed outputed values?
number_format(), or maybe in this case money_format();
Quote:
3.) There must be a better way to print out all the values
from an array but constructing a loop came to mind and
beats doing it one by one. Is there better way?
Why not a foreach() loop there?
Anyway, print_r() and var_dump() are your friends while building, for a
form/html-page for other users you almost certainly will have to use a loop
or a str_repeat()/vprintf() combo.
Grtz,
--
Rik Wasmus