Reputation: 444
I have a db where I manually add data. Some of the fields are:
ID AA Name Percentage(will be changing once a week)
1 2. Me 10%
2 1. You 40%
When I call the db with my dynamic list I get all names sorted By Perentage. The AA column I need it in order to get this result:
1. You 40%
2. Me 10%
It's easy to manually play with AA column (since my entries are no more than 10-15) and match it with the Percentage so I get the correct display that I need.
But if my entries become e.g 30 it will be very difficult to change all the time the AA column to match with the Percentage one.
So is there any modification I can do to my dynamic_list.php so I get the 1. 2. generated automaticaly? Here is my dynamic_list.php:
$sql = mysql_query("SELECT * FROM clients WHERE Category='Deksioseis' ORDER BY percentage DESC");
$productCount = mysql_num_rows($sql);
// count the output amount
if ($productCount > 0) {
$i=0;
$dynamicListDeks = '<table id="pl_list" class="list">';
while($row = mysql_fetch_array($sql)){
$id = $row["ID"];
$aa = $row["AA"];
$client_name = $row["Client_Name"];
$short_name = $row["Short_Name"];
$details = $row["Details"];
$percent = $row["Percentage"];
$dynamicListDeks .= ($i==0) ? '<tr>':'';
$dynamicListDeks .= '<td class="lst_aa">
' . $aa . '
</td>
<td class="lst_l">
<img src="../rooms/' . $short_name . '.png" alt="' . $client_name . '" />
</td>
<td class="lst_nam">
<a id="' . $short_name . '" name="' . $short_name . '" class="room">' . $client_name . '</a>
</td>
<td class="lst_det">
' . $details . '
</td>
<td class="lst_per">
' . $percent . '%
<br />
</td>';
$dynamicListDeks .= ($i==1) ? '</tr>':'';
$i++;
($i==2) ? $i=0:'';
}
$dynamicListDeks .='</table>';
} else {
$dynamicListDeks = "";
}
mysql_close();
Upvotes: 0
Views: 102
Reputation: 91983
Keep a counter in your PHP code that you increment for each run of the loop:
$aa = 0;
while($row = mysql_fetch_array($sql)){
$id = $row["ID"];
$aa++;
Upvotes: 2
Reputation: 2789
Instead of saving AA in database you may add variable which will keep value of AA which u can display later.
$sql = mysql_query("SELECT * FROM clients WHERE Category='Deksioseis' ORDER BY percentage DESC");
$productCount = mysql_num_rows($sql);
// count the output amount
if ($productCount > 0) {
$i=0;
$aa=0;
$dynamicListDeks = '<table id="pl_list" class="list">';
while($row = mysql_fetch_array($sql)){
$id = $row["ID"];
$aa++;
$client_name = $row["Client_Name"];
$short_name = $row["Short_Name"];
$details = $row["Details"];
$percent = $row["Percentage"];
$dynamicListDeks .= ($i==0) ? '<tr>':'';
$dynamicListDeks .= '<td class="lst_aa">
' . $aa . '
</td>
<td class="lst_l">
<img src="../rooms/' . $short_name . '.png" alt="' . $client_name . '" />
</td>
<td class="lst_nam">
<a id="' . $short_name . '" name="' . $short_name . '" class="room">' . $client_name . '</a>
</td>
<td class="lst_det">
' . $details . '
</td>
<td class="lst_per">
' . $percent . '%
<br />
</td>';
$dynamicListDeks .= ($i==1) ? '</tr>':'';
$i++;
($i==2) ? $i=0:'';
}
$dynamicListDeks .='</table>';
} else {
$dynamicListDeks = "";
}
mysql_close();
Upvotes: 0