paul2463 Posted June 20, 2006 Share Posted June 20, 2006 Hello again the faithfull problem giver is back....... can someone explain to me why in my code only one call to the function actually fires, both of them work in their own right, I can comment one of the calls out and the other works perfectly but both together only the first call fires...[code]<?php//databse is connected to and the data is being pulled ok$query = 'SELECT adate,status_2 FROM availabilty';$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());$pDates = array();$bDates = array();function sortdates($res,$val){ $arr1 = array(); while($row = mysql_fetch_assoc($res)) { if($row['status_2']==$val) { $arr1[]=$row['adate']; } }return $arr1;}$pDates = sortdates($result,p);$bDates = sortdates($result,b);print_r($pDates);echo "<br/><br/>;print_r($bDates);?>[/code]its as though once $result has been passed to the first call to the function it is used up and cannot be used again. Quote Link to comment https://forums.phpfreaks.com/topic/12449-function-problems/ Share on other sites More sharing options...
trq Posted June 20, 2006 Share Posted June 20, 2006 [code]$pDates = sortdates($result,p);$bDates = sortdates($result,b);[/code]Where do you define the constants [i]p[/i] and [i]b[/i]? Quote Link to comment https://forums.phpfreaks.com/topic/12449-function-problems/#findComment-47660 Share on other sites More sharing options...
kenrbnsn Posted June 20, 2006 Share Posted June 20, 2006 Besides answering thorpe's question, you answered your own question:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]its as though once $result has been passed to the first call to the function it is used up and cannot be used again.[/quote]The variable $result is a pointer to the dataset containing the records selected and once the while condition is statisfied, you can't do another while statement using that pointer unless you re-issue the query.Why can't you combine the two functions calls into one?[code]<?phpfunction sortdates($res,$vals){ $retarr = array(array(),array()); while($row = mysql_fetch_assoc($res)) { for ($i=0,$i<count($vals);$i++) if($row['status_2']==$vals[$i]) $retarr1[$i][]=$row['adate']; }return $retarr;}list($pDates, $bDates) = each(sortdates($result,array('p','b')));?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/12449-function-problems/#findComment-47668 Share on other sites More sharing options...
paul2463 Posted June 20, 2006 Author Share Posted June 20, 2006 [code]<?phpfunction sortdates($res,$vals){ $retarr = array(array(),array()); while($row = mysql_fetch_assoc($res)) { for ($i=0,$i<count($vals);$i++) if($row['status_2']==$vals[$i]) $retarr1[$i][]=$row['adate']; }return $retarr;}list($pDates, $bDates) = each(sortdates($result,array('p','b')));?>[/code]Ken[/quote]I dont understand this code( which is not surprising I know), to answer Thorpe the 'p' and 'b' are the values that may appear in the status_2 field of the rows pulled from availability. to make it a bit easier to understand the adate value is either 'booked' or 'pending'. what I required from the $pDates and $bDates is two seperate arrays, one with the pending dates in and one with the booked dates. I cant seem to get my head around the above code and what it is trying to do, can someone please explain? Quote Link to comment https://forums.phpfreaks.com/topic/12449-function-problems/#findComment-47751 Share on other sites More sharing options...
mainewoods Posted June 21, 2006 Share Posted June 21, 2006 you don't need to reissue the query(that would be slower), just reset the pointer to the result:[code]mysql_data_seek($result, 0); //resets the pointer to the first row[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12449-function-problems/#findComment-47890 Share on other sites More sharing options...
paul2463 Posted June 22, 2006 Author Share Posted June 22, 2006 [!--quoteo(post=386249:date=Jun 20 2006, 09:47 PM:name=mainewoods)--][div class=\'quotetop\']QUOTE(mainewoods @ Jun 20 2006, 09:47 PM) [snapback]386249[/snapback][/div][div class=\'quotemain\'][!--quotec--]you don't need to reissue the query(that would be slower), just reset the pointer to the result:[code]mysql_data_seek($result, 0); //resets the pointer to the first row[/code][/quote]thanks a lot this works brilliantly.all is happy in the "lack of Knowledge" camp at this time. Quote Link to comment https://forums.phpfreaks.com/topic/12449-function-problems/#findComment-48451 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.