I'd suggest a simple variable that increments with each iteration of the foreach loop. With something like the code below, you can access the number of the element with the counter variable.
// +--
// | Loop through the products.
// +--
$counter = 0; //Init the counter
foreach ($prodlist as $num => $prod) {
//Leave the below line at the top of the loop
$counter++; //Increment the counter with each product
//The counter starts at 1 and counts up
// +--
// | Print a table row opener.
// +--
print '<tr class="' . $trclass . '">' . $eol . $eol;
// +--
// | Print the tds and their content.
// +--
$prodname = $this->xhtml_encode($prod['name']);
$prodid = $this->xhtml_encode($prod['id']);
$prodlink = $this->link_namespace($app,'prodshow',$prod['id']);
$proddesc = $this->xhtml_encode($prod['descshort']);
$imgname = $prod['imgsm'];
$add_ok = 1;
if (($prod['useinv']) && (!($prod['invlevel'] > 0))) {$add_ok = 0;}
if ($prod['pricestatus'] == 'D') {$add_ok = 0;}
$quandisp = 0;
$quanform = $this->globals('khxc_cgi.' . $formid . '--' . $prodid . '--quantity');
if (isset($quanform)) {$quandisp = $quanform;}
if ($add_ok) {
print '<td class="' . $tdclass . '">' . $eol . $eol;
print '<label for="' . $formid . '--' . $prodid . '--quantity';
print '" class="hidden">Quantity</label>' . $eol;
print '<input class="khxc_formfield" type="text" name="';
print $formid . '--' . $prodid . '--quantity';
print '" id="' . $formid . '--' . $prodid . '--quantity';
print '" value="' . $quandisp . '" size="5" maxlength="5" />' . $eol;
print '</td>' . $eol . $eol;
} else {
print '<td class="' . $tdclass . '"> ' . …