Jump to content

Recommended Posts

Hi all,

 

I am looking for some help on selecting variables from MySQL, but leaving out the oldest (or smallest numerical) value. 

 

Let's say I have several values (which are auto-incremented) in the DB.  For example:

 

32, 34, 35, 36, 38, 40.

 

When I select these ID's from the Database, I'd like to select and echo all of them except the one which was first created.  In the example above, it would be "32".

 

How would I go about achieving something like this?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/135948-solved-mysql-select-hopefully-simple/
Share on other sites

In your sql query do an order by ID, then the following should help you with the logic

 

$i=0;
while ($row = mysql_fetch_assoc($result)) {
    if ($i == 0) {
        $i++;
        continue; // dont print the first id
    }
    
     echo $row['id'];
}

 

Hope that helps ya.

@Flames:

 

The reason was because those numbers are dynamically generated.  Thus, I won't always know what the number is.  I was only using "32" as an example.  Sorry for not being clear.

 

 

@premiso:

 

That did it, thanks for getting me in the right direction.  However, I have one caveat, (and maybe I'm asking too much).

 

In order to pull it off, I had to ORDER BY ASC.  But, I'd actually like to display the variables in a descending fashion. 

 

Is this possible in my circumstance?

 

 

@Flames:

 

The reason was because those numbers are dynamically generated.  Thus, I won't always know what the number is.  I was only using "32" as an example.  Sorry for not being clear.

 

 

@premiso:

 

That did it, thanks for getting me in the right direction.  However, I have one caveat, (and maybe I'm asking too much).

 

In order to pull it off, I had to ORDER BY ASC.  But, I'd actually like to display the variables in a descending fashion. 

 

Is this possible in my circumstance?

 

 

 

Just reverse the logic:

 

$i=1;
$cnt = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
    if ($i == $cnt) {
        continue; // dont print the first id
    }
   
     echo $row['id'];
     $i++;
}

 

Should do the trick.

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.