PHP Question

RJM62

Touchdown! Greaser!
Joined
Jun 15, 2007
Messages
13,157
Location
Upstate New York
Display Name

Display name:
Geek on the Hill
I had a few professional coders tell me this was a convoluted and expensive way of styling the link for the current page in a database-driven pagination menu. I find it hard to understand how it could be much easier.

The current page and its database id are known, so I set the id for the row corresponding to that URL as "$thisPage." There's also a CSS class "current" to style that link. So when the iterator comes around to that page, it echoes "<li class = "current">" rather than "<li>".

PHP:
if($i == $thisPage) {
        echo " <li class=\"current\"><a href=\"" . $friendlyURL . "\" title = \"". $title . "\">" . $i . "</a></li>\n";
        } else {
        echo " <li><a href=\"" . $friendlyURL . "\" title = \"". $title . "\">" . $i . "</a></li>\n"; }
    $i++;
    }

I've been doing it this way for years, and I really don't think it's all that expensive. Am I wrong?

Thanks,

Rich
 
I think it's fine. I think fashion in PHP (which is itself a lol-worthy concept) would be to ditch the if/else and just have a single echo with a ternary expression for the li class. something like this:

echo "<li" . ($i == $thisPage) ? " class=\"current\"> : ">" . "<a href...

but I might be misunderstanding your i++ in a greater context and a ternary is not workable. <shrug!>

:) Don't listen to me, though, I've used allmon braces my entire career and gotten into all sorts of fashion arguments. At the end of the day, I can win the argument by saying "if I cause a bug, you know who to blame just by looking at the indents"
 
What's being suggested is that I should assign the CSS class for the current page prior to the database pull and have it concatenated into a placeholder for that page (<li class = "current"><a href = "#" ... >). The adjacent pages pulled from the db would not have the class assigned and would be built on either side of the link for the current page prior to echoing them. The advantage would be that the links wouldn't have to be parsed and evaluated during every iteration.

I guess that would make sense if a huge number of links were being displayed. For the typical dozen or so, I think the difference would be trivial.

Rich
 
Okay, I understand why they said that. Both of the coders are building links for huge numbers of pages (up to all of them in the db), but using CSS to style the paginator's display depending on the viewport. So more links would be rendered on a desktop than on a phone. In that case, the extra parsing might be expensive.

That gives me some ideas, actually. Maybe style a horizontal scrollbar to look more like a slider control, and save a list of all the URLs in a db table or XML file that's updated every time a new page is created; then serve all the links on every request and style them so the user can slide through them to see the links hidden by the overflow, with little or no JS needed. (I'm not a big fan of JS.)

I'm sure many others have already done this sort of thing. I'm semi-retired with time on my hands, so I enjoy reinventing wheels in my spare time.

Rich
 
Don't listen to what they say, use what works for you. Any "expense" is trivial unless you're dealing with Amazon type volumes.

This week I'm working on a system where someone made some decisions based on what "professional" IT folks thought would be good, even though it deviates from a configuration that has years of proven success. On Monday the individual machines of the server cluster lost track of each other and on Tuesday SQL Server shredded itself and had to be brought down in the middle of the day. I think we legitimately lost data from the SQL Server because now the recovered database has random rows of data missing in the weirdest places.

Sometimes there's a reason we don't do it the "new" way.
 
Back
Top