
-----------------------------------
raidercom
Sun Apr 27, 2008 9:34 am

Had a small problem with a PHP script
-----------------------------------
I hate to do this on my first post, but I needed some help, and I remembered this board from High School.

My problem is that I have information stored in a database with 3 fields
Number, Name, and Got
It is a list of items, the number related to that item, and whether I have it or not.

ex: 
1 Series 1
2 Series 1
3 Series 1
4 Series 0
5 Series 1
6 Series 1

In this example, I there are 6 books in the series, and I have 5 of them.
I want my PHP to output that I have
Have: 1-3,5-6
Need: 4

So far, I can make it display 1,2,3,5,6, but that looks really ugly when I have around 50-60 items in a list.  I was wondering if anyone could help me create a function for this.  I have tried to think about how to do this, but I cannot think of a way.

Any help that could be offered would be appreciated.

Thanks
Jeff

-----------------------------------
jernst
Sun Apr 27, 2008 9:45 am

Re: Had a small problem with a PHP script
-----------------------------------
You could use a loop to check consecutive numbers in an array, print the first number, a dash and then the last consecutive number. Then every time you get a missing number, you add that to a separate list or separate array or whatever to be printed later as the missing ones. Then resume checking for the remaining portion of the array for the consecutive numbers again.

-----------------------------------
raidercom
Sun Apr 27, 2008 9:55 am

Re: Had a small problem with a PHP script
-----------------------------------
Thanks, I will do that.  I cannot believe I didn't think of that...

Thank you for your fast reply.

-----------------------------------
raidercom
Sun Apr 27, 2008 8:55 pm

Re: Had a small problem with a PHP script
-----------------------------------
This is what I have right now, but I keep getting a lot of fluky results.
 $result = array (1); //test1
 $result = array (1, 2);//2
 $result = array (1, 2, 3);//3
 $result = array (1, 3);//4
 $result = array (1, 3, 4);//5
 $result = array (1, 2, 4, 5);//6
 $result = array (1, 2, 4, 5, 8);//7

 $Continuous = 3;
 $LastNum = 0;
 $TotalRuns = count($result);
 for ($a = 0; $a < $TotalRuns; $a ++)
 {
  if ($a == 0)
  {
   $Continuous = 1;
   echo $result [$a];
   $LastNum = $result [$a];
  }
  elseif ($a == $TotalRuns - 1)
  {
   if ($result [$a] - 1 == $LastNum)
   {
    $Continuous = 1;
    echo '-' , $result [$a];
   }
   else
   {
    echo ',' , $result [$a];
    $LastNum = $result [$a];
    $Continuous = 0;
   }
  }
  else
  {
   if ($LastNum + 1 == $result [$a])
   {
    $Continuous = 1;
    $LastNum = $result [$a];
   }
   else
   {
    if ($Continuous == 1)
    {
     if ($a > 1)
     {
      echo '-' , $LastNum;
      echo ',' , $result [$a];
      $LastNum = $result [$a];
     }
     else
     {
      echo ',' , $result [$a];
      $LastNum = $result [$a];
     }
    }
    else
    {
     echo ',' , $result [$a];
     $LastNum = $result [$a];
    }
    $Continuous = 0;
   }
  }
 }

I'm uncertain as to how to do this better, because this way doesn't appear to work.

Any help would be appreciated.

Jeff

-----------------------------------
raidercom
Sun Apr 27, 2008 9:06 pm

Re: Had a small problem with a PHP script
-----------------------------------
Also tried something along the lines of:

 $TotalRuns = count($result);
 for ($a = 0; $a < $TotalRuns; $a ++)
 {
  if ($a == 0)
  {
   echo $result [$a];
  }
  if ($result [$a] + 1 == $result [$a + 1])
  {
   $Continuous = 1;
   //echo "asdsada" . $a . "asdsad";
  }
  else
  {
   if ($Continuous == 1)
   {
    echo '-' , $result [$a];
   }
   else
   {;
    if ($a  0)
    {
     echo ',' , $result [$a];
    }
   }
   $Continuous = 0;
  }
 }

-----------------------------------
raidercom
Sun Apr 27, 2008 9:09 pm

Re: Had a small problem with a PHP script
-----------------------------------
Think I fixed it now:

If anyone can see any problems with my code, I would appreciate feedback.

$TotalRuns = count($result);
 for ($a = 0; $a < $TotalRuns; $a ++)
 {
  if ($a == 0)
  {
   echo $result [$a];
  }
  if ($result [$a] + 1 == $result [$a + 1])
  {
   if ($Continuous == 0)
   {
    if ($a  0)
    {
     echo ',' , $result [$a];
    }
   }
   $Continuous = 1;
   //echo "asdsada" . $a . "asdsad";
  }
  else
  {
   if ($Continuous == 1)
   {
    echo '-' , $result [$a];
   }
   else
   {;
    if ($a  0)
    {
     echo ',' , $result [$a];
    }
   }
   $Continuous = 0;
  }
 }

Thanks

Jeff
