Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Index Being Recalculated

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

    Default Index Being Recalculated

    I define $menuLabelIndex and then use it as an index item in the (highly excerpted) array that then follows:
    PHP Code:

    $lemma 
    rand (0count($quotations) -1);
    $menuLabelIndex $lemma;
    unset (
    $lemma);

    $menuItems = array(
      
    'Wire Fraud',
      
    'Case Review',
      
    'Super Links',

      
    $quotations [$menuLabelIndex],

      
    'Directions',
      
    'Parking',
      
    'PGP Public Key',
      
    'The Bottom Line'
    ); 
    Later in the script, I loop through the $menuItems table on two different occasions. On the second occasion, [$menuLabelIndex] is recalculated, so that $quotations [$menuLabelIndex] yields a different result from the $quotations array than from the first looping. You can see the disparate results in the left-hand and bottom menus on any page of www.MarainLaw.com. (It's the entry towards the bottom of the menue that immediately follows the "Super Links" item.)

    Is there a straightforward way to keep [$menuLabelIndex] from being recalculated?

    A.

  2. #2
    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

    Is this the post you're talking about? If so, I would need to see the full code, or at least the fuller code. I would need enough to both run these two loops that you are talking about and in such a way that I could repeat the error/problem on a scratch page I could setup and run on wamp.

    What exactly is the problem? Is it that you get a different random item each time? First time I got the same one. Second time I got two different ones.

    I can tell you this, if you keep going:

    Code:
    $lemma = rand (0, count($quotations) -1); 
    $menuLabelIndex = $lemma; 
    unset ($lemma);
    each time, the likelihood of getting a different result is high, because each time you do this, you potentially get a different random number.
    - John
    ________________________

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

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

    Default Index Being Recalculated

    That IS the post in question. I can provide the full code if you like, although it exceeds 500 lines. (A fair number of those lines are comments.) I could likely pare it down, although that would take a bit of time.

    You have accurately discerned the problem: What I was attempting to do was to obtain an initial random number, save it, and use the saved random number the next time I wanted the element from the table. I initially did not believe the unset command was needed. But in trying to solve the problem, even using unset did not work. Rather than "Index Being Recalculated," this thread could perhaps been more accurately entitled, "Unset Not Working".

    Regardless, this bepuzzlement is now predominately of academic interest since, upon reflection, I'm not too unhappy with what the code is actually doing.

    A.

    Quote Originally Posted by jscheuer1 View Post
    Is this the post you're talking about? If so, I would need to see the full code, or at least the fuller code. I would need enough to both run these two loops that you are talking about and in such a way that I could repeat the error/problem on a scratch page I could setup and run on wamp.

    What exactly is the problem? Is it that you get a different random item each time? First time I got the same one. Second time I got two different ones.

    I can tell you this, if you keep going:

    Code:
    $lemma = rand (0, count($quotations) -1); 
    $menuLabelIndex = $lemma; 
    unset ($lemma);
    each time, the likelihood of getting a different result is high, because each time you do this, you potentially get a different random number.

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

    Default

    The only way the index value would be recalculated during one execution/request of the script is if you are calling or including/requiring the posted section of code more than once, you have some other code that's modifying the value (perhaps a conditional test that's setting the value rather than comparing the value), you have a logic error or scope problem that's using a different value, you have code that always uses the first/last value, and a few more possibilities.

    As already mentioned, it would take having enough of your code that reproduces the problem in order to specifically help you. There's no point in taking the time to paring down the code you post.

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

    Default Index Being Recalculated

    Quote Originally Posted by DyDr View Post
    The only way the index value would be recalculated during one execution/request of the script is if you are calling or including/requiring the posted section of code more than once, you have some other code that's modifying the value (perhaps a conditional test that's setting the value rather than comparing the value), you have a logic error or scope problem that's using a different value, you have code that always uses the first/last value, and a few more possibilities.

    As already mentioned, it would take having enough of your code that reproduces the problem in order to specifically help you. There's no point in taking the time to paring down the code you post.
    I do not believe any of the situations you describe are happening, but I could be wrong. (I've been maintaining this code for years, but I was not its original author.) Hesitant to post a file of that size here, I've uploaded the full code to http://www.marainlaw.com/pageContent/menuCode.txt .

    A.

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

    Default

    Note, the second quotation is always the next entry in the array from the first quotation. The problem is due to this - $i++, on about line 358 in the posted code.

    Almost all of this code will go away if you do two things -

    1) Make a single array with the key being the current $menuLinks values and the value being the $menuItems values. You can then just use a foreach(){} loop to loop over the single array. This will make it easier to maintain the data since the key/values will be defined together.

    2) You are currently building each line down the page, taking into account what may be on the left and right side of each line to control how the page is laid out. Use css to layout the page. The left-menu should be a div of content. The main-content should be a div of content. The bottom-menu should a div of content. Each section of content should be produced as an independent section.

  7. #7
    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

    Quote Originally Posted by DyDr View Post
    Note, the second quotation is always the next entry in the array from the first quotation.
    No, it is not. Sometimes it is the same, sometimes it is another one not anywhere near being next in the array.
    - John
    ________________________

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

  8. #8
    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

    OK, my diagnosis is that DyDr was right at first when he speculated: "you are calling or including/requiring the posted section of code more than once"

    My reasoning is that I ran the code that you linked to and it only made one menu. In order for it to make two menus, it must be being used more than once, and that of course would make it be a different random number the next time.
    - John
    ________________________

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

  9. #9
    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

    That's it. The code is being used twice. Once for the side menu when $menuType is 'button' and once for the bottom menu when $menuType is not 'button'. So to 'fix' it you would have to generate $menuLabelIndex on the calling (including) page or script and do it just one time per page.

    Or perhaps - but this may or may not work, may or may not have unintended consequences (works here in testing though):

    Code:
    $lemma = rand (0, count($quotations) -1);
    $menuLabelIndex = isset($menuLabelIndex)? $menuLabelIndex : $lemma;
    unset ($lemma);
    BTW unsetting $lemma was never going to fix this. So of course unset is working fine.
    Last edited by jscheuer1; 05-27-2016 at 04:57 PM.
    - John
    ________________________

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

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

    Default

    Okay, so it's not always the next one now.

    The supplied code cannot be producing the linked to web page. The supplied code is looping once. It is echoing a left-hand link syntax, followed by a bottom link, with | characters between items, in each pass through the loop. Even if the OP removed the end of the first loop/start of the next in the supplied code, the order of the left-hand links/bottom links in the output doesn't match the code.

    In the view source of the output, the markup for the entire bottom menu, with the | separators, is first, followed by the markup for the left-hand link menu. The exact output and the order of the output cannot be different than when it was produced and sent to the browser.

    I'm going to guess that the OP's real code is including this code twice, with some not-shown logic to control what it does, or there are actually two separate pieces of code, one for the left-menu and one for the bottom menu, and the random number code is producing two different numbers because it is running twice.

    @OP, we cannot specifically help you with anything your code is doing when you don't supply accurate code.

    Edit: I see there have been more posts while I was writing this. The $menuType value is 'button' for the links in both menus. See the class name in the view source in the links.

    The only thing we can both tell is the code is being used twice and the OP is posting adulterated, time wasting, information.
    Last edited by DyDr; 05-27-2016 at 05:44 PM.

  11. The Following User Says Thank You to DyDr For This Useful Post:

    marain (05-27-2016)

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
  •