Jump to content


Photo

Function problems


  • Please log in to reply
5 replies to this topic

#1 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 20 June 2006 - 09:13 AM

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...

<?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);

?>

its as though once $result has been passed to the first call to the function it is used up and cannot be used again.
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 20 June 2006 - 01:48 PM

$pDates = sortdates($result,p);
$bDates = sortdates($result,b);
Where do you define the constants p and b?

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 02:11 PM

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?
<?php
function 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')));
?>

Ken

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 20 June 2006 - 06:07 PM

<?php
function 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')));
?>

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?

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 21 June 2006 - 02:47 AM

you don't need to reissue the query(that would be slower), just reset the pointer to the result:

mysql_data_seek($result, 0); //resets the pointer to the first row


#6 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 22 June 2006 - 02:15 PM

[!--quoteo(post=386249:date=Jun 20 2006, 09:47 PM:name=mainewoods)--][div class=\'quotetop\']QUOTE(mainewoods @ Jun 20 2006, 09:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
you don't need to reissue the query(that would be slower), just reset the pointer to the result:

mysql_data_seek($result, 0); //resets the pointer to the first row
[/quote]

thanks a lot this works brilliantly.
all is happy in the "lack of Knowledge" camp at this time.
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users