Welcome Guest, Not a member yet? Register   Sign In
Ordered list for league standings - what about ties?
#1

[eluser]RayDube[/eluser]
Hey Gents and ladies,

http://sundaycurling.raying.ca/index.php/standings

is the page I'm working on at the moment.

http://sundaycurlinggang.raying.ca/standings.php

is the page I'm basing things on.

As you can see, the difference is how the position is showing, the second page shows that ties are ranked at the same position, but the first doesn't show this.

Now, in the data retrieval loop from the second page is a check for the number of points. If the points are tied, we have to check the number of wins, if those are tied, the the position counter is *not* incremented, otherwise it's incremented. here's how I had that logic:
Code:
if ($PrevPoints == $Points)
  {
    if ($PrevWins == $Wins)
    {
        $pos = $pos;
        $Position = $Position + 1;
    }
    else
    {
        $Position = $Position + 1;
        $pos = $Position;
    }
  }
  else
  {
    $pos = $Position + 1;
    $Position = $Position + 1;
  }

Before the loop, the variables PrevWins, PrevPoints and pos are all set to 0.

It's easy in "streaming" mode to simply echo out the position and move on through the loop, but in mvc, I've got to place the pos in to an array, and then pull the correct array info, right?

so I thought I could do this:
Code:
if($query->num_rows()>0)
    {
      foreach ($query->result() as $row)
      {
        $data[]=$row;
        $this->Points = $row->Points;
        $this->Wins = $row->Wins;
        if ($this->PrevPoints == $this->Points)
        {
          if ($this->PrevWins == $this->Wins)
          {
            $this->data['pos'] = $this->pos;
            $this->Position = $this->Position + 1;
          }
          else
          {
            $this->Position = $this->Position + 1;
            $this->data['pos'] = $this->Position;
          }
        }
        else
        {
          $this->data['pos'] = $this->Position + 1;
          $this->Position = $this->Position + 1;
        }
      }

But, obvioiusly I cannot, I get this error for each position:
Undefined property: stdClass::$pos

Help? Please? Smile

Ray
#2

[eluser]Ben Edmunds[/eluser]
You're getting that error because $this->pos seems to be empty.

What is up with the $this->Position = $this->Position + 1; ?
#3

[eluser]RayDube[/eluser]
Thanks for the look Ben.

The pos is empty, but why is it empty.

In the view I've got this:
Code:
<?php foreach($records as $row) :?>
        <tr>
          <td>&lt;?php echo $row->pos; ?&gt;</td>
          <td>&lt;?php echo $row->Team_Name; ?&gt;</td>
          <td>&lt;?php echo $row->Wins; ?&gt;</td>
          <td>&lt;?php echo $row->Losses; ?&gt;</td>
          <td>&lt;?php echo $row->Ties; ?&gt;</td>
          <td>&lt;?php echo $row->Points; ?&gt;</td>
        </tr>
        &lt;?php endforeach; ?&gt;
Guess I should have posted that before too...

So the pos is pulled from the dataset that's returned form the model, or at least I expected it to be... But it isn't. Sad

Maybe the "$this->data['pos']" should be simply "$data['pos']" ?
--edit--
No, that made it much worse... Smile
--end edit--

As for the Position bit, it's just a counter, but I got "$this" happy.. Smile

Ray
#4

[eluser]Ben Edmunds[/eluser]
Because $this->pos is empty.

Did you mean to use $row->pos ?

It kind of seems like you are over complicating it. Post your model methods and then explain exactly what you want the view to display.
#5

[eluser]RayDube[/eluser]
Thanks again Ben, but I think we are at odds a bit, just not understanding one another..

The query generates 18 rows(ish),those 18 rows are added to $data[] one at a time in the foreach loop, correct?

Code:
foreach ($query->result() as $row)
      {
        $data[]=$row;
      }

Now I want to add the position info to that $data[] object.

It *seems* to me that this should be possible by simply doing this:
Code:
$this->data['pos'] = $this->Position + 1;
in the foreach loop, using the logic posted earlier.

From what I think I know about php, you add a new key/element by doing so:
array['key'] = 'element';

Am I wrong? If not, what am I doing wrong, if so, what should I be doing?

If you *really* want me to, I'll post up the controller, complete model and view, but I'm not really sure that's necessary. Smile

Ray
#6

[eluser]Ben Edmunds[/eluser]
OK see if this code helps?

Code:
if($query->num_rows()>0)
    {
      $this->data = $query->result_array();
      foreach ($this->data as $key => $row)
      {
        if ($this->PrevPoints == $row['Points'])
        {
          if ($this->PrevWins == $row['Wins'])
          {
            $this->data[$key]['pos'] = $this->pos; //where is this coming from?  should it be $row['pos']?
            $this->Position++;
          }
          else
          {
            $this->Position++;
            $this->data[$key]['pos'] = $this->Position;
          }
        }
        else
        {
          $this->Position++;
          $this->data['pos'] = $this->Position;
        }
      }
      
      echo '<pre>';
      print_r($this->data);
      echo '</pre>';
}


You can simplify it even more if you aren't using $this->Position anywhere else.
#7

[eluser]RayDube[/eluser]
Oops, that code is a bit off (my fault completly) the pos is the actual position in the ranking, and Position is simply a counter. here's better code:
Code:
class standings_model extends Model{

  var $counter;
  var $ps;
  var $PrevPoints = 0;
  var $PrevWins = 0;
  var $pos = 0;
  var $Points;
  var $Wins;
  var $Position;

  Function get_standings()
  {
    $query = $this->db->query($sql);

    if($query->num_rows()>0)
    {
      foreach ($query->result() as $row)
      {
        $data[]=$row;
        $this->Points = $row->Points;
        $this->Wins = $row->Wins;
        if ($this->PrevPoints == $this->Points)
        {
          if ($this->PrevWins == $this->Wins)
          {
            $this->pos = $this->pos;
            $this->data['pos'] = $this->pos;
            $this->Position = $this->Position + 1;
          }
          else
          {
            $this->Position = $this->Position + 1;
            $this->pos = $this->Position;
            $this->data['pos'] = $this->pos;
          }
        }
        else
        {
          $this->pos = $this->Position + 1;
          $this->data['pos'] = $this->pos;
          $this->Position = $this->Position + 1;
        }
      }
      $query->free_result();
      return $data;
    }
  }
}

$Position could be $counter for all it matters. Smile

$pos is the actual position, starting at 0, which never remains at 0, but is increased to 1 on the very first loop. "if ($this->PrevPoints == $this->Points)" will never be true on the first go, so the first position set is 1

And yes, it could be cleaned up and have position removed entirely... I was just to lazy to redo it properly. Smile

But I do notice a problem, my prev variables are not set at the end of the loop.

I'm going to make a few adjustments and then be back to post any new findings.

Ray
#8

[eluser]RayDube[/eluser]
here's the cleaner code.

Code:
if($query->num_rows()>0)
    {
      foreach ($query->result() as $row)
      {
        $data[]=$row;
        $this->Points = $row->Points;
        $this->Wins = $row->Wins;
        if ($this->PrevPoints == $this->Points)
        {
          if ($this->PrevWins == $this->Wins)
          {
            $this->data['pos'] = $this->pos;
          }
          else
          {
            $this->pos++;
            $this->data['pos'] = $this->pos;
          }
        }
        else
        {
          $this->pos++;
          $this->data['pos'] = $this->pos;
        }
        $this->PrevPoints = $this->Points;
        $this->PrevWins = $this->Wins;
      }
      $query->free_result();
      return $data;
    }

Looks better, but still doesn't work... :-(

Ray
#9

[eluser]Ben Edmunds[/eluser]
Dude, did you see what I was doing in the code?

The $data array is what you are returning but your overwriting $this->data['pos'] on each loop and you are not returning the Points or Wins.

It doesn't matter if you use $data or $this->data you want to return whatever contains all of the data you need...


Try this:

Code:
if($query->num_rows()>0)
    {
      $data = $query->result_array();
      foreach ($data as $key => $row)
      {
        if ($this->PrevPoints == $row->Points)
        {
          if ($this->PrevWins == $row->Wins)
          {
            $data[$key]['pos'] = $this->pos;
          }
          else
          {
            $this->pos++;
            $data[$key]['pos'] = $this->pos;
          }
        }
        else
        {
          $this->pos++;
          $data[$key]['pos'] = $this->pos;
        }
        $this->PrevPoints = $row->Points;
        $this->PrevWins   = $row->Wins;
      }
      $query->free_result();
      return $data;
    }


You can do a print_r() to look at the array.
#10

[eluser]RayDube[/eluser]
No, I guess I missed that bit.

But I *am* returning the Wins and Points, they're updating correctly.

And $data['pos'] doesn't exist in the array when the row is added:
Code:
foreach ($query->result() as $row)
      {
        $data[]=$row;

there is no "pos" in the result set, I'm trying to add that to the data array.

Looking at what you've given me, I'm going to have problems I'm afraid. The row is already an array of keys and values, as an object, that is being assigned to $data as an array. Changing the way the info is sent to the data array (return_array) will mean changing the way the info is called in the view as well.

I think we're over-analyzing this though, the code (cleaned or not) works in all respects but one, the "ranking" in the standings is not showing. "Undefined property: stdClass::$pos" in the view where it is called:
Code:
&lt;?php foreach($records as $row) :?&gt;
        <tr>
          <td>&lt;?php echo $row->pos; ?&gt;</td>
          <td>&lt;?php echo $row->Team_Name; ?&gt;</td>
          <td>&lt;?php echo $row->Wins; ?&gt;</td>
          <td>&lt;?php echo $row->Losses; ?&gt;</td>
          <td>&lt;?php echo $row->Ties; ?&gt;</td>
          <td>&lt;?php echo $row->Points; ?&gt;</td>
        </tr>
        &lt;?php endforeach; ?&gt;

specifically:

Code:
<td>&lt;?php echo $row->pos; ?&gt;</td>

Here's a link to the site:
http://sundaycurling.raying.ca/index.php/standings

All I need to do is find a way to pass that info along...

Now a thought occurs to me, data is a multidimenstional array, and row, which is an array of sorts, is probably where I need to add pos, and that may solve all of my problems...

Back to code...

Ray




Theme © iAndrew 2016 - Forum software by © MyBB