Jump to content

Function problems


paul2463

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.
Link to comment
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
Link to comment
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?
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.