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
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 --;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.