Jump to content

Archived

This topic is now archived and is closed to further replies.

paul2463

Function problems

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[code]
$pDates = sortdates($result,p);
$bDates = sortdates($result,b);
[/code]
Where do you define the constants [i]p[/i] and [i]b[/i]?

Share this post


Link to post
Share on other sites
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]<?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')));
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[code]<?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')));
?>[/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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.