Jump to content

Php mysql fetch last result


doublea2k7

Recommended Posts

Im not sure where to post this but since it includes php il post it here instead of in the mysql forum.

 

ok so, i have a table and i get the values using

while($row = mysql_fetch_array($result)){

and then echo them in rows.

 

that works fine but i need to add a class to the last row of my table. I would need somehow to fetch the last row of the array and make it echo something different.

 

Any help is appreciated

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/212534-php-mysql-fetch-last-result/
Share on other sites

thers a syntax error here is a snipet of my code. Its something wrong with the endwhilte but i dont know wat is it.

 

      <?php
$row2 = mysql_fetch_array($result);	  
while($row = mysql_fetch_array($result)){ ?>
<tr id="<?php echo $row['statusid']?>" class="status">
<td><?php echo $row['date']?></td>
<td id="<?php echo$row['statusid']?>" style="text-align:left;" class="editStatus">
<?php echo htmlspecialchars($row['statusTxt'], ENT_QUOTES)?></td>
<td style="width:1%;">
<form action="">
<input type="image" src="img/cancel.png"  onclick="makeVariable('<?php echo $row['statusid']?>')" class="deleteRowButton"/>
</form>
</td>
</tr>
<?php $row2 = $row;?>
<?php
}
end while?>
<tr id="<?php echo $row['statusid']?>" class="statusLast">
<td><?php echo $row['date']?></td>
<td id="<?php echo$row['statusid']?>" style="text-align:left;" class="editStatus">
<?php echo htmlspecialchars($row['statusTxt'], ENT_QUOTES)?></td>
<td style="width:1%;">
<form action="">
<input type="image" src="img/cancel.png"  onclick="makeVariable('<?php echo $row['statusid']?>')" class="deleteRowButton"/>
</form>
</td>
</tr>

<?php } ?>

remove end while 
it's just remnark in pseudo code[code=php:0]<?php
$row = mysql_fetch_array($result);	  
while($row2 = mysql_fetch_array($result)){ ?>
<tr id="<?php echo $row['statusid']?>" class="status">
<td><?php echo $row['date']?></td>
<td id="<?php echo$row['statusid']?>" style="text-align:left;" class="editStatus">
<?php echo htmlspecialchars($row['statusTxt'], ENT_QUOTES)?></td>
<td style="width:1%;">
<form action="">
<input type="image" src="img/cancel.png"  onclick="makeVariable('<?php echo $row['statusid']?>')" class="deleteRowButton"/>
</form>
</td>
</tr>
<?php $row = $row2;?>
<?php
}
$row=$row2;
?>
<tr id="<?php echo $row['statusid']?>" class="statusLast">
<td><?php echo $row['date']?></td>
<td id="<?php echo$row['statusid']?>" style="text-align:left;" class="editStatus">
<?php echo htmlspecialchars($row['statusTxt'], ENT_QUOTES)?></td>
<td style="width:1%;">
<form action="">
<input type="image" src="img/cancel.png"  onclick="makeVariable('<?php echo $row['statusid']?>')" class="deleteRowButton"/>
</form>
</td>
</tr>

<?php } ?>

but i don't see any diferents for last row

Hello Doublea2k7.

 

Try the following

<?php
  $row = mysql_fetch_array($result);  
  $count = mysql_num_rows($result);
  $countCheck = '0';
  while($row2 = mysql_fetch_array($result)){ $countCheck++;

  if($count == $checkCount) { $class='statusLast'; } else { $class='status'; }
?>

<tr id="<?php echo $row['statusid']?>" class="<?PHP echo $class;?>">
  <td><?php echo $row['date']?></td>
  <td id="<?php echo$row['statusid']?>" style="text-align:left;" class="editStatus">
   <?php echo htmlspecialchars($row['statusTxt'], ENT_QUOTES)?></td>
  <td style="width:1%;">
   <form action="">
   <input type="image" src="img/cancel.png"  onclick="makeVariable('<?php echo $row['statusid']?>')" class="deleteRowButton"/>
   </form>
  </td>
</tr>

<?php } ?>

 

Instead of

      <?php
$row2 = mysql_fetch_array($result);
while($row = mysql_fetch_array($result)){ ?>
<tr id="<?php echo $row['statusid']?>" class="status">
<td><?php echo $row['date']?></td>
<td id="<?php echo$row['statusid']?>" style="text-align:left;" class="editStatus">
<?php echo htmlspecialchars($row['statusTxt'], ENT_QUOTES)?></td>
<td style="width:1%;">
<form action="">
<input type="image" src="img/cancel.png"  onclick="makeVariable('<?php echo $row['statusid']?>')" class="deleteRowButton"/>
</form>
</td>
</tr>
<?php $row2 = $row;?>
<?php
}
end while?>
<tr id="<?php echo $row['statusid']?>" class="statusLast">
<td><?php echo $row['date']?></td>
<td id="<?php echo$row['statusid']?>" style="text-align:left;" class="editStatus">
<?php echo htmlspecialchars($row['statusTxt'], ENT_QUOTES)?></td>
<td style="width:1%;">
<form action="">
<input type="image" src="img/cancel.png"  onclick="makeVariable('<?php echo $row['statusid']?>')" class="deleteRowButton"/>
</form>
</td>
</tr>

<?php } ?>

 

Tell me how it goes :)

 

Paul.

There's a typo or two in the code PaulRyan posted.

 

$countCheck = '0'; // The single quotes aren't needed.

// AND //

if($count == $checkCount) { $class='statusLast'; } else { $class='status'; } // ******** CHANGE $checkCount to $countCheck 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.