You are here: Re: ok, I'm stuck: recursive function restting vars and not returning when expected « PHP Programming Language « IT news, forums, messages
Re: ok, I'm stuck: recursive function restting vars and not returning when expected

Posted by Sjoerd on 09/07/07 13:41

On Sep 7, 11:20 am, J. Frank Parnell <p...@edgecity.ufo> wrote:
> Condo for rent has 3 price tiers (for different times of the year):
If this is an application for real use, consider how likely it would
be that someone decides that there will be a fourth price tier, and
how easy your application can adapt to that.

> For every 7 nites they stay, they get 1 free, and that free one should be the
> cheapest night (1 value nite, 6 premium nites, they should get the value nite
> free)
If nites is how you spell nights, it's fine with me. If another
English programmer will work on your code, (s)he will make mistakes
very soon. However, if you are using both "nights" and "nites", as you
do, you are asking for bugs. Even if you are the only one working on
the program.

> $totalnites = $_POST['vnites'] + $_POST['rnites'] + $_POST['pnites'];
You can use array_sum($postNights) instead.

> if (($totalnites > 6)AND($totalnites < 14)){$mult = 1;}
> if (($totalnites > 13)AND($totalnites < 21)){$mult = 2;}
> if (($totalnites > 20)AND($totalnites < 28)){$mult = 3;}
> if (($totalnites > 27)AND($totalnites < 35)){$mult = 4;}
> if (($totalnites > 34)AND($totalnites < 42)){$mult = 5;}
Use math instead of this. It's simpler and it works on any number of
nights. Something like
$mult = floor($totalnites / 7);

> $temptotalnites = $totalnites;
Anything with "temp" is typically a bad variable name.

> if ($totalnites > 6){ // call the func:
Always call the function, no matter how many nights are booked. This
allows easier change if it is decided that every third night is free.

I have something here which does not use recursion. This is not a
recursive problem anyway:


$_POST['vnites'] = 2; $_POST['rnites'] = 0; $_POST['pnites']=12;

$types = array('vnites', 'rnites', 'pnites');

foreach ($types as $type) {
$postNights[$type] = $_POST[$type];
}

$totalNights = 0;
foreach ($postNights as $type => $nights) {
$totalNights += $nights;
}

$freeNights = floor($totalNights/7);
for ($i = 0; $i < $freeNights; $i++) {
foreach ($types as $type) {
if ($postNights[$type] > 0) {
$postNights[$type] --;
$postNights['freenites'] ++;
// do not consider other types, continue with for loop
continue 2;
}
}
}

print_r($postNights);

?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация