Author |
Message |
micah
Hangin' Around

Joined: May 25, 2006
Posts: 40
|
Posted:
Fri Mar 12, 2010 11:25 am |
|
Hey all,
i am trying to select the 2nd from max value from a table. I know that MAX () will select the max but how do I select the 2nd from max?
example of what I am doing
if ($sid == max()) {
include("me.htm");
}
thanks,
Micah |
|
|
|
 |
djmaze
Subject Matter Expert

Joined: May 15, 2004
Posts: 727
Location: http://tinyurl.com/5z8dmv
|
Posted:
Fri Mar 12, 2010 1:50 pm |
|
Code:SELECT id FROM table ORDER BY id DESC LIMIT 1 OFFSET 1
|
|
_________________ $ mount /dev/spoon /eat/fun auto,overclock 0 1
ERROR: there is no spoon
http://claimedavatar.net/ |
|
|
 |
micah

|
Posted:
Fri Mar 12, 2010 3:08 pm |
|
thanks djmaze for the tip but is there an easier way to pull this variable so that I can include this line in
if ($sid == max()) {
include("me.htm");
} |
|
|
|
 |
Palbin
Site Admin

Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania
|
Posted:
Fri Mar 12, 2010 3:55 pm |
|
Is this in RN / *nuke or just PHP in general? |
_________________ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. |
|
|
 |
micah

|
Posted:
Fri Mar 12, 2010 3:58 pm |
|
HI palbin, this is in phpnuke. |
|
|
|
 |
Raven
Site Admin/Owner

Joined: Aug 27, 2002
Posts: 17088
|
Posted:
Fri Mar 12, 2010 10:22 pm |
|
Code:$sql = 'SELECT id FROM table ORDER BY id DESC LIMIT 1 OFFSET 1';
$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['id']) {
include("me.htm");
}
|
This assumes that you are using the $db->sql_* syntax and not the antiquated $dbi syntax for data base access  |
|
|
|
 |
micah

|
Posted:
Fri Mar 12, 2010 10:39 pm |
|
Hi Raven,
I kinda see what you did there but part of this may be my problem
instead of using mysql can we not use an if command to pull the 2nd or 3rd from max sid "story id) to include a file between stories the stories in the news module.
If I change max () to a story number the file me.htm prints between the stories like a hot darn.
If I use max () I can print the file to the top article.
I guess my thing is that I can not figure out how to get the file "me.htm" to display below the 2nd story without having to ente teh story number
my origional thought that did not work was
if ($sid == max -2 ()) {
include("me.htm");
}
This displays the "me.htm" article below the 12252 story on the news module
if ($sid == 12252) {
include("me.htm");
}
Thanks very much |
|
|
|
 |
Raven

|
Posted:
Fri Mar 12, 2010 10:58 pm |
|
The SQL that you were given needs you to replace id and table with the actual values that are in your database/table. So, you need to actually write your sql statement like
$sql = 'SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 1';
or
$sql = 'SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 2';
When you say second from I'm not sure which value you are actually after but its controlled by the offset. |
|
|
|
 |
Raven

|
Posted:
Fri Mar 12, 2010 10:59 pm |
|
So your code should now look like
Code:SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 1
$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['sid']) {
include("me.htm");
}
|
|
|
|
|
 |
micah

|
Posted:
Fri Mar 12, 2010 11:32 pm |
|
HI raven ... I still can't get it as the screen goes white when I place that code.
Can I pay you to do this for me.
Micah |
|
|
|
 |
Raven

|
Posted:
Sat Mar 13, 2010 1:52 am |
|
Sorry! Try this instead.
Code:$sql = 'SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 1';
$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['sid']) {
include("me.htm");
}
|
|
|
|
|
 |
Guardian2003
Site Admin

Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam
|
Posted:
Sat Mar 13, 2010 3:30 am |
|
You'll also need somewhere on your page
if you don't already have it.
If your code is inside a function that line should be inside the function too, before the $sql |
|
|
|
 |
micah

|
Posted:
Sat Mar 13, 2010 8:35 am |
|
awesome ... thanks ... wow
your help was above and beyond once again
Very appreciated |
|
|
|
 |
Palbin

|
Posted:
Sat Mar 13, 2010 8:37 am |
|
Code:
global $db, $prefix;
$sql = 'SELECT sid FROM ' . $prefix . '_stories ORDER BY sid DESC LIMIT 1 OFFSET 1';
$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['sid']) {
include("me.htm");
}
|
Just wanted to note that you should have the $prefix in there. |
|
|
|
 |
Raven

|
Posted:
Sat Mar 13, 2010 9:22 pm |
|
micah wrote: | awesome ... thanks ... wow
your help was above and beyond once again
Very appreciated |
 |
|
|
|
 |
|