Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Index Being Recalculated

  1. #11
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This will probably fix it (as highlighted, also as explained in my previous post):

    Code:
    $lemma = rand (0, count($quotations) -1);
    $menuLabelIndex = isset($menuLabelIndex)? $menuLabelIndex : $lemma;
    unset ($lemma);
    Otherwise, you will need to set it in the calling script (also as mentioned in my previous post).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. The Following User Says Thank You to jscheuer1 For This Useful Post:

    marain (05-27-2016)

  3. #12
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    John,

    Your suggested code does indeed fix it. (As indicated in post #3, I'll now need to decide whether I want it to stay "fixed." Regardless, I thank both you and DyDr for your patience, and your efforts.) I'll keep http://www.marainlaw.com/pageContent/menuCode.txt up for a few days, in case anyone else wants to look at it. I also want to study the solution in more depth to get a fuller understanding. I will do that, but probably will not get to it this weekend.
    Last edited by marain; 05-27-2016 at 11:14 PM. Reason: Original message, replaced in its entirety, now obsolete.

  4. #13
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It's not really that complicated, if in fact my suggestion worked without causing any additional problem(s), the most likely possible problem being that the random link would then become the same throughout an entire session, though I tend to doubt that's possible.

    Assuming all is well, here's the explanation -

    The code in menuCode.txt is run twice per page load. Once (presumably the first time, but that might not be crucial) with $menuType set as 'button'. That produces the left side menu. Then another time with $menuType not set as 'button' (but, a fine point admittedly but perhaps of note, presumably set to something), that produces the bottom menu. If we make sure to only set $menuLabelIndex one out of these two times (as my code suggestion virtually guarantees), then you will have the same random link in both iterations. And, as I say, the most likely complication would be if $menuLabelIndex somehow survives into subsequent page load(s) in the session. That's doubtful, but to know that for sure I would need to see more code and/or run the pages numerous times and see what happens after the change. Anyways, if that were to be the case (unintended persistence beyond a single page), then the random link would persist throughout a given user's session - presumably until they left the site, or perhaps until they closed their browser.

    As I say though - that's unlikely. If the new code doesn't present any persistent problems like that in - say 5 experimental tries, it's most likely a non-issue.

    All that said, and even if it works as desired without any issues, I understand how leaving it as it was might be preferable. After all, as it was before we started investigating this it produced 2 random links per page - which isn't the worst thing, might actually be making things more interesting than the consistency you originally sought.

    Edit: Was just looking at the pages again, looks like it's working with no problems.
    Last edited by jscheuer1; 05-28-2016 at 05:53 AM. Reason: add edit, later - clarity about $menuType
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #14
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    It works as desired with no discernible issues.

    What befuddled me was that if the unset ($lemma); command is actually inside of a loop, it is not obvious--at least not obvious to me. It was for that reason that I dismissed DyDr's initial suggestion that the code was being executed more than once per page load. Turns out his (and your) analysis was correct. That is what I need to study in more depth. (That is likely to happen not soon.)

    Regardless, y'all have pointed the way so that I can complete the analysis on my own. Now I just need to decide on whether or not to retain the consistency you helped me achieve.

    A.

    P.S. I am unfamiliar with DyDr's OP and @OP abbreviations, or acronyms.
    Last edited by marain; 05-28-2016 at 02:13 PM. Reason: Refinement of original post.

  6. #15
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Quote Originally Posted by marain View Post
    .
    .
    .
    P.S. I am unfamiliar with DyDr's OP and @OP abbreviations, or acronyms.
    OP is a common forum acronym for Original Poster (the member who started the thread) and @OP is simply directing a comment to that member.

    Hope that helps.

  7. The Following User Says Thank You to styxlawyer For This Useful Post:

    marain (05-28-2016)

  8. #16
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I setup a test page I named: menuitems_h.php - that I put the code from menuCode.txt in. I ran that, and it only made one menu. So it had to be running twice. So I made another page. I named it menuitems_1_h.php - and it I put just:

    PHP Code:
    <?php
    $menuType 
    'button';
    include 
    'menuitems_h.php';
    $menuType 'bob';
    include 
    'menuitems_h.php';
    ?>
    I did that because the type of menu (left side or bottom) that's made is controlled by whether or not $menuType is set to 'button'. (setting it to 'bob' the second time was arbitrary - it had to be something or else there would be an error, and it couldn't be 'button' or else it would make another side menu) So you can pretty easily find where the two places are that call the menu code by looking for instances of $menuType in the calling code (on the page above menuCode.txt).

    Now, this (the way it is now) is pretty inefficient because the menu arrays are being defined twice. I would be tempted to do all that, including choosing the random item one level up, before calling menuCode.txt. That would also solve the problem. What I did simply ensures that the second time around, the initial random value is used instead of generating a new one. The more elegant solution would be to only have it happen once.

    I think that - defining the menu arrays only once is the way to go even if you want it to be random again for the second menu. It would still be more efficient to only generate another random entry the second time around and insert it at the desired index in the $menuItems array (replacing the previous random one), rather than generate an entire new set of the 3 menu related arrays.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #17
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    It would have taken me forever, or longer, to figure that out.

    I vastly prefer elegance to inefficiency. In this case, that preference, of course, competes with "if it ain't broke, don't fix it." Perhaps I'll elegantize it at some point. For now, however, I've no more excuses to keep me from doing the other work I'd planned for the weekend.

    Thanks again.

    A.

  10. #18
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    Quote Originally Posted by marain View Post
    "if it ain't broke, don't fix it."
    A.
    Speaking of which, there is a typo error in some of the repeated logic, that's causing different spacing between the categories in the menus. The code is outputting a <br> after the last item in each menu category. In one place it is incorrectly testing if a value is $menuLinks[$i] === 'pgp_key.php' in another place it is properly testing if a value is $menuLinks[$i] === 'pgp_key'

    One point of good programming practice is to not repeat yourself. The only things that are logically different between the production of the two menus is the separator character between items (a <br> vs a |) and if the category heading is output. There should be one set of code to do this with logic only to handle the things that are different. This would eliminate duplicate coding, which would fix this particular problem, since the value being tested would only exist once in the code.

    And, as already mentioned somewhere in this thread, you can eliminate most of this code by using a different data structure. By storing the menu items in an array of arrays, with the category name as the main array index, and the menu items as a sub-array under each category, you could simply loop over the main array, outputting the category heading when needed, then loop over each sub-array. You can simply handle the different separator character between items by storing each link in an array, then implode() that array with the separator character that currently being called for.

Similar Threads

  1. Changing index.html to index.php TO-DO list
    By BillTheBuilder in forum Other
    Replies: 0
    Last Post: 03-09-2012, 09:37 PM
  2. Static Index page, with changing iframe pages w/o leaving index help
    By Wolfman72 in forum Looking for such a script or service
    Replies: 3
    Last Post: 04-12-2011, 03:19 PM
  3. Replies: 11
    Last Post: 06-01-2009, 12:12 AM
  4. index.php?display=... > index.php/view/id
    By jmh1988 in forum Looking for such a script or service
    Replies: 1
    Last Post: 10-21-2008, 04:19 PM
  5. Replies: 0
    Last Post: 08-05-2007, 09:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •