Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Limit "most popular" to 100

5 views
Skip to first unread message

Richard Damon

unread,
Jul 4, 2016, 12:06:41 PM7/4/16
to
On 7/4/16 8:38 AM, super70s wrote:
> (This question is for everyone but R.Wieser)
>
> I have a "Most Popular Pages" page on my site, with the page titles and
> brief descriptions listed based on the number of page views. I need to
> limit the number of page titles to 100 instead of having it display
> every single page title in the database, which is what it's doing now. I
> think this is the pertinent code:
>
> <?while ($rshome = mysql_fetch_array($cmdartcat)) { ?>
>
> Is there an easy way to accomplish what I want by modifying this somehow?
>

This is more of a mysql issue than a php issue. If you add a limit cause
to your query, you can limit how many records you receive.

You could do a php only solution, adding a counter to the loop.
Something similar to:

<?$cnt=0;
while (++$cnt<=100 && $rshome = mysql_fetch_array($cmdartcat)) { ?>

Christoph M. Becker

unread,
Jul 4, 2016, 12:11:19 PM7/4/16
to
On 04.07.2016 at 18:06, Richard Damon wrote:

> On 7/4/16 8:38 AM, super70s wrote:
>
>> (This question is for everyone but R.Wieser)

Nonsense. This is Usenet.

>> <?while ($rshome = mysql_fetch_array($cmdartcat)) { ?>
>>
>> Is there an easy way to accomplish what I want by modifying this somehow?
>
> This is more of a mysql issue than a php issue. If you add a limit cause
> to your query, you can limit how many records you receive.

And that's what should be done for performance reasons.

> You could do a php only solution, adding a counter to the loop.
> Something similar to:
>
> <?$cnt=0;
> while (++$cnt<=100 && $rshome = mysql_fetch_array($cmdartcat)) { ?>

And consider to avoid the mysql extension, which has been deprecated
long ago, and has been removed from the official php.net distribution as
of PHP 7.0.0.

--
Christoph M. Becker

R.Wieser

unread,
Jul 4, 2016, 1:34:26 PM7/4/16
to
super70s,

> Is there an easy way to accomplish what I want by modifying this somehow?

> (This question is for everyone but R.Wieser)

In that case I won't tell you that you could try to add a "LIMIT 100" to the
end of your query string ...
:-)

Regards,
Rudy Wieser


-- Origional message:
super70s <supe...@super70s.invalid> schreef in berichtnieuws
super70s-BB47CD...@boina.datemas.de...
> (This question is for everyone but R.Wieser)
>
> I have a "Most Popular Pages" page on my site, with the page titles and
> brief descriptions listed based on the number of page views. I need to
> limit the number of page titles to 100 instead of having it display
> every single page title in the database, which is what it's doing now. I
> think this is the pertinent code:
>

Lew Pitcher

unread,
Jul 4, 2016, 1:59:13 PM7/4/16
to
On Monday July 4 2016 11:38, in comp.lang.php, "super70s"
<supe...@super70s.invalid> wrote:

> (This question is for everyone but R.Wieser)
>
> I have a "Most Popular Pages" page on my site, with the page titles and
> brief descriptions listed based on the number of page views. I need to
> limit the number of page titles to 100 instead of having it display
> every single page title in the database, which is what it's doing now. I
> think this is the pertinent code:
>
> <?while ($rshome = mysql_fetch_array($cmdartcat)) { ?>
>
> Is there an easy way to accomplish what I want by modifying this somehow?

Others have described the smart, easy way to do this by using the SQL "LIMIT"
clause in your query. So, I'll address a non-SQL alternative, just for
completeness sake:

Instead of

<?while ($rshome = mysql_fetch_array($cmdartcat)) { ?>

you could place a counter in your loop code; something like

<?
for ($count = 0;
$count < 100 && $rshome = mysql_fetch_array($cmdartcat) ;
++$count) {
?>

But, the SQL "LIMIT 100" clause would be your best bet, here.

--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

Christoph M. Becker

unread,
Jul 6, 2016, 8:02:54 AM7/6/16
to
On 06.07.2016 at 05:32, super70s wrote:

> In article <fQvez.67639$F_5....@fx23.iad>,
> Richard Damon <Ric...@Damon-Family.org> wrote:
>
>> You could do a php only solution, adding a counter to the loop.
>> Something similar to:
>>
>> <?$cnt=0;
>> while (++$cnt<=100 && $rshome = mysql_fetch_array($cmdartcat)) { ?>
>
> That seems to have done the trick! Much obliged.
>
> I just didn't want the page to get really huge by displaying all the
> posts in the db.

Good thinking. However, why do you retrieve all posts from the DB then
in the first place?

--
Christoph M. Becker
0 new messages