I have 3 td's in a portion of a table. Sometimes, the data coming in from the database is less than 3. I need a method that will lay-out the data precisely compared to a rank in the table.
Ideal situation:
array
|-0
|-name => Joe
|-rank => 0
|-1
|-name => Dick
|-rank => 1
|-2
|-name => Mike
|-rank => 2
Yields:
<tr>
<td data-rank="0">
Joe
</td>
<td data-rank="1">
Dick
</td>
<td data-rank="2">
Mike
</td>
</tr>
Problem situation:
array
|-0
|-name => Dick
|-rank => 1
|-1
|-name => Mike
|-rank => 2
Yields:
<tr>
<td data-rank="0">
Dick
</td>
<td data-rank="1">
Mike
</td>
</tr>
Need:
<tr>
<td data-rank="0">
</td>
<td data-rank="1">
Dick
</td>
<td data-rank="2">
Mike
</td>
</tr>
This is the for loop I am running. I think I need the array function prev to move the pointer back but can not get my head around it.
for ($i=0; $i<3; $i++) {
$a = array("class"=>"genre", "data-rank"=>$i,"data-method"=>"update");
$o .= $this->td_new($a);
if ( array_key_exists($i, $r1) && ($r1[$i]['rank'] == $i) ) {
$o .= $r1[$i]['name'];
} else {
$o .= " ";
prev($r1);
}
$o .= $this->td_close();
}//for
return $o;
With the above, I am ending up with 3 blank td's. Sub-functions like $this->td_open and $this->td_close work perfectly. Detail about exact output had been left out too keep things brief and focussed on where I need help. Instances the array is 3 keys, output is perfect.
Thanks for taking the time to read this.