Closed Thread Icon

Topic awaiting preservation: PHP Shoutbox (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13119" title="Pages that link to Topic awaiting preservation: PHP Shoutbox (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP Shoutbox <span class="small">(Page 1 of 1)</span>\

 
marf
Bipolar (III) Inmate

From: Canada
Insane since: Oct 2001

posted posted 04-03-2004 17:19

Ok, I am coding a php shoutbox. Everything works fine. But when im displaying the shoutbox, I only want the 5 Most recent entries to show.

So As you can see I am doing

SELECT * FROM shoutbox order by date desc LIMIT 0, 5

Thats basically what im getting from the MySQL.

However When it prints the 5 most recent posts, it prints them newest to oldest.

But I wish it to print the 5 most recent posts, from oldest to newest.

code:
$q="SELECT * FROM shoutbox order by date desc ";
$result=mysql_query($q)or die("Could not execute query: $q." .mysql_error());

$rows_per_page=5;//Only 5 per row
$page=0;//
$start=$page*$rows_per_page;
$q .=" LIMIT $start, $rows_per_page";



Any Help?

I'm thinking I have to get the number of rows in the MySQL table, then subtract 5, and make that my $start (starting position). Or when i sort them in date desc, then i start with the bottom one.

Thanks

Marf

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 04-03-2004 17:36

I don't know how your limit applies anyhow. You're appending it to the query string
after you've already executed the query. That will never work. I suppose you just made that mistake in the code you posted, though.

You won't have any luck doing this in sql, since order by can only be applied once - and it's needed to let mysql select the 5 newest/oldest entries in the first place.

If you want to switch the order that you print them, I suggest you read them in an array,
and then use array_reverse() to turn that array around, and loop through said array.

code:
$strQuery =[...order by date desc];
$hRes = mysql_query($strQuery);
$arrRows = array();
while ($arrRow = mysql_fetch_array($hRes)
{
$arrRows[] = $arrRow;
}
$arrRows = array_reverse($arrRows);
foreach ($arrRows as $arrRow)
{
[...]
}



so long,
TP

[This message has been edited by Tyberius Prime (edited 04-03-2004).]

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-03-2004 18:18

I don't have MySQL installed currently so I am not sure if this option will actually work, it might be breaking the rules completely.

SELECT * FROM shoutbox ORDER BY date DESC LIMIT COUNT(date)-5, 5

If you can actually do a row could here you will be able to set your offset to the size off. What this would do would be to count the total rows and set your limit offest to 5 rows from the end. And then select the last 5 rows. Think of the numbers below as representing dates, it would then select

10
9
8
7
6
5
4
3
2
1

The last 5 ordered 5, 4, 3, 2, 1 which would be your desired functionality.

<edit>
I posted too damn fast. It turns out that LIMIT required two INTEGER CONSTANTS which COUNT is not one, and as count is only allowed in the select area you can't use it. However, you might be able to change your syntax to use row_count this would make your query.

SELECT * FROM shoutbox ORDER BY date DESC LIMIT row_count-5,5

Still don't know if that will work, but it might.
</edit>

<edit number="2">
I am a complete moron. row_count is not a valid variable. It was used as a refernece for an INTEGER CONSTANT. Use TP's method because I can't come up with a pure SQL one.
</edit>


Dan
CodeTown.org

[This message has been edited by WarMage (edited 04-03-2004).]

marf
Bipolar (III) Inmate

From: Canada
Insane since: Oct 2001

posted posted 04-03-2004 22:07

Ok I got it working. The shoutbox loads into an I Frame. When the IFrame loads I want it to scroll to the bottom, to the latest post. How can I scroll an I Frame to the bottom of a window?

"If rabbits' feet are so lucky, then what happened to the rabbit?"

« BackwardsOnwards »

Show Forum Drop Down Menu