473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ok, I'm stuck: recursive function restting vars and not returning when expected

arrrrrg:
Condo for rent has 3 price tiers (for different times of the year):
value
regular
premium

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)

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

$totalnites = $_POST['vnites'] + $_POST['rnites'] + $_POST['pnites'];
echo 'totalnites= '. $totalnites.'<BR><BR>';
$postNights = array('vnites' =$_POST['vnites'] , 'rnites' =$_POST['rnites']
, 'pnites' =$_POST['pnites']);
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;}

$temptotalnites = $totalnites;

if ($totalnites 6){ // call the func:
$discountnites = subtractFreeDays($postNights ,$temptotalnites, $mult);

echo '<PRE>after func $discountnites=';
print_r($discountnites);
echo '</PRE>';
}

func:
<?php
function subtractFreeDays($fpostNights, $ftotalnites, $fmult){
echo 'in func mult: '.$fmult.'<BR>';
echo 'in func $fpostNights[vnites]: '.$fpostNights['vnites'].'<BR>';
echo 'in func $discvnites='.$discNights['vnites']."<BR><BR>";

if ($fmult 0){

if($fpostNights['vnites'] 0){//echo'1';
$discNights['vnites'] = $discNights['vnites'] + 1;
echo '$discvnites='.$discNights['vnites']."<BR>";
$fpostNights['vnites'] = $fpostNights['vnites'] - 1;
echo '$fpostNights[vnites]='.$fpostNights['vnites']."<BR><BR>";
}
elseif($fpostNights['rnites'] 0){
$discNights['rnites'] = $discNights['rnites'] + 1;
$fpostNights['rnites'] = $fpostNights['rnites'] - 1;
}
else{
$discNights['pnites'] = $discNights['pnites'] + 1;
$fpostNights['pnites'] = $fpostNights['pnites'] - 1;
}
//echo 'before minusing fmlut='.$fmult."<BR>";
$fmult = $fmult - 1;
if ($fmult >= 1){
echo 'before recurse $fmult= '.$fmult.'<HR>';
subtractFreeDays($fpostNights, $ftotalnites, $fmult);
}else{
echo '<PRE>returning:';
print_r($discNights);
echo '</PRE>';
return $discNights;
}
}
//shouldnt get here
return array('shouldnt','get','here');
}
?>

-- This works on less than 14 days.
Over 14 days, it breaks:
1. $discNights['vnites'] is being reset to nothing every recursion, what gives?
2. I get to
echo '<PRE>returning:';
print_r($discNights);
echo '</PRE>';
return $discNights;
and it *does* print_r (wrong value, see #1), but it doesnt seem to return, and
that really chaps my hide. It will keep going, eventually returning the
array('shouldnt','get','here').

Thanks for your time,

Sep 7 '07 #1
1 1408
On Sep 7, 11:20 am, J. Frank Parnell <p...@edgecity.ufowrote:
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);

?>

Sep 7 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: actuary77 | last post by:
I am trying to write simple recursive function to build a list: def rec(n,alist=): _nl=alist print n,_nl if n == 0: print n,_nl return _nl else:
2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
3
by: samuelberthelot | last post by:
Hi, I'm trying to write a recursive fucntion that takes as parameters a html div and an id. I have to recurse through all the children and sub-children of the div and find the one that matches the...
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
3
by: monojohnny | last post by:
Hi, I know you can do stuff with introspection to gather up passed-in args for a Javascript function and that you can list all defined functions like: function a(abc,xyz,zzz) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.