Jump to content

Reverse Auto Numbering


manalnor

Recommended Posts

Hello dear friends,

 

if we have following lines

 

line x

line y

line z

 

we call it from database as $lines

 

by help of "Pikachu2000" he mention the following code for auto-numbering the lines

 

$i = 1;
while( $array = mysqli_fetch_assoc($result) {
     echo $i . " - " . $lines;
     $i ++;
}

 

now it will shows as

1- line x

2- line y

3- line z

 

 

the question here is there anyway to reverse it with keeping the order of lines, i mean to be

 

3- line x

2- line y

1- line z

 

Hope you got my questions

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/197702-reverse-auto-numbering/
Share on other sites

You just need to figure out where to start the numbering with a simple query to set the initial value of $i .

$query = "SELECT index FROM tbl_name WHERE some_field = 'some_value'";
$result = mysqli_query($dbc, $query);
$i = mysqli_num_rows($result);
while( $array = mysqli_fetch_assoc($result) ) {
     echo $i . " - " . $array['index'];
     $i --;
}

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.