Jump to content

phppup

Members
  • Posts

    859
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by phppup

  1. I have variables as such

    $var0 = "hello";
    $var1 = "good bye";
    $var2 = "back soon";

    I get appropriate results when I do this

    echo $var0;  echo $var1;  echo $var2;

    Now, I want to get the r same esults using a created string like this

    for($x=0; $x <3; $x++){
      $test = "\$var".$x; echo $test."<br>";
      }

    But this only provides

    $var0
    $var1
    $var2

    How can I generate the respective variable elements so that they reflect their values?

  2. I would choose my query based on a careful evaluation of the data held in the database.

    If all addresses have an accurate city and zip code and your user is searching for a nearby business you may want to prioritize the zip code search since some cities use more than one zip code to cover the geography.

    You then need to know the boundaries. If a large city has six zip codes, are they set up like a 2 column grid where, 1 and 4 are at the top of each column, or in a circle with 1 in the center?

    The USPS may not be as logical as you desire but if the user is in a specific zip code and there's a match with a business in the same zip code: BINGO.

    Otherwise, a citywide result that lets the user determine where he wants to visit may be the best route.

  3. @kicken 

    Quote

    If in the future you need to add something to doStuff that needs access to colZ, you won't have to go around your application updating all the function calls to include colZ.

    The future is NOW (because that was one of my thoughts initially)

    My data is being pulled from $my_array which is outside of the function (as opposed to inside a db).

    So what would be the correct way of addressing the function (as per your last example)?

    Ideally, I would re-populate the array with any amount of values and still be able to use the same function without having to re-work the structure.

  4. I would appreciate some clarification (as I've actually never used arguments this way in functions).

    Experimentation seems to have revealed that this portion of the code needs to include all the related variables.

    function choose($x, $z)

    And that this

    choose($x, $z);

    to call the function must match identically.

     

    However, we have taken the argument a step further, and some clarity would be helpful.

     

    In this example we are assigning the variables in the call, but the function arguments are ALL the sought results?

    Similar to giving an alias tho the variable???

  5. @Barand per reply #1 ... You'll be happy to know that I know that's a mess.  But as you realized, it was meant to demonstrate what I was trying to accomplish.

    As for reply #2, I will have to try it in my script, but I see that it should both solve the issue by calling the function differently in each case AND removed the use of forewarned globals.

    (Somewhat of a tragedy really since I already made the list. LOL  But then, better code is best!)

    Out of curiosity, of I were to attempt to group the clusters as globals, is it possible?

    Again, I see the wisdom and (assumed) success in your method, but an still curious.

    Thanks for the help.

     

     

  6. I have reduced a process to function with a minimal list of global variables that are required.

    For simplicity, I'll offer this:

    If form1 is submitted, then global $a is necessary

    If form2 is submitted, then global $x and global $z are necessary.

    Rather than "register" all three, I wanted to use an IF statement to point to the specific globals necessary for the specified form input.

    My attempts are unsuccessful.

    $a = 123;
    $x = "hello";
    $z = "there";
    
    function choose(){
    global $formSelector;
    
    if($formSelector == "form1") {
    global $a; 
    } else {
    global $x; global $z;
    }
    
    //Test for result
    echo $z;
    if ($a == 123){
    echo "bye bye";
    }
    echo "test result";
    }
    
    //Call function
    choose();

    Can I segregate the globals or is a single comprehensive list the only solution?

  7. I am trying to establish an array that will be the levels deep.

    I want to access all data, but for some reason arrays keep moving to position 1 or more.

    I want to be able to establish a value at [0], at [0][0], and also [0][0][0]

    Shouldn't those be the first values at each level?

    How do I set this up?

  8. I'm trying to build an array as follows:

    Level 1

    Level 2

    Level 3

    Examples seem to be showing similar implementation to this

    $myArr = array( 'level 1',
              array('level 2' ),
        array(
        array( 5, 6 ),
        array( 7, 8 )
      )
    );

    But this seems to move the data one section deeper than necessary.

    In other words, there is nothing at position [0][0] or [0][0][0] (except Array which pushes me down another level).

    Shouldn't my access to the first item on level 1 be $myArr[0]?

    How can I get this lined up accordingly?

     

  9. @requinix Am I understanding it  "conceptually"?

    Explanation to develop my understanding or referenced tutorial, please?

    (I've kind of got the problem solved after removing the FUNCTION() method, but would prefer to achieve my results more systemically.

  10. @Barand Then it appears that I was on the right track (sort of nesting the functions) which then actually makes the "outter" function a parent of the function within it.  Is that making sense? Correct?

    (And eliminating the problem of passing values?)

  11. In somewhat of a continuation of the previous issue:

    function first() {
    //do stuff and result $A
         }
    
    
    function two($externalArray) {
    //do stuff to $A when applicable and result $B
         }

    I have an external array that I have been able to access WITHOUT using the GLOBAL feature, but now I am having a problem passing values.


  12. I want to use elements from an array within a function.

    I had some success using global $array

    But then I read that using GLOBAL should be avoided (the explanation of why was not very clear)
    And that the same result could be achieved by attaching the array to a function (not sure how to do that)

    I was then thinking of creating an array of global variables. Is this even possible?

    What is the best way to access an array outside of a function so that its elements can be useful?

  13. @kicken Sounds reasonable.

    I was planning to have a single submittal for an individual's top 3 flavors so that I can get a broader result of flavors that are most popular, so I guess I'll need 3 more columns that I'll need to merge.

    Obviously I'll create a safeguard so that no flavor can be submitted more than once when the entire form is submitted. All flavor choices must be unique.

    @maxxd That was kinda the reason for my post. Too determine if there's a "preferred" method or more advisable choice.

    I guess it kinda comes down to personal preference.

    Maybe I'll just see who else votes and use this thread as my template. LOL

    Life imitating art.

    And meanwhile, I'll just have a bowl of vanilla caramel swirl.

  14. I want to get the results after asking 100 people to select their 3 favorite flavors of ice cream.

    As I conceptualize the methodology, I can see I'd want an array of flavors to generate a group of checkboxes or a drop-down to avoid the conflict of misspellings.

    But from the database perspective, what would be the Best Practice to implement?

    Do I want a column for every flavor that receives an X when that row is submitted? Or do I want to input a 1 and then somehow total all columns whereby a descending list would run from most favorite to least?

    Would a single column for flavor_01, flavor_02, flavor_03 be advisable? Then assess the votes for each flavor on each column and combine VANILLA from 01 and 02 and 03 etc?

    (This might offer an opportunity to see how many people choose fudge swirl as their SECOND FAVORITE selection separate from the mass of voting, but this is probably not a relevant indicator)

    Guidance, please.

     

  15. I knew it was something like this.

    So I don't need to indicate a form of, just the form tag is enough?

    @mac_gyver I like that idea. And the template can essentially be anywhere on the <body> because the placement is handled by "target", right?

    Although they do need independent names for submission, don't they?

     

  16. I have added a field using JavaScript but it will not post.

    <form method="POST" action="">
      <input type="submit">
    </form>  
      
      <script>
        
    const input = document.createElement("input");
    input.setAttribute("type", "text");
    input.setAttribute("name", "name");
    input.setAttribute("value", "value");
    document.body.appendChild(input);
        
        </script>

    Am I missing a certain command?

     

  17. Is there an advisable/best practice for using multiple includes?

    I've found several instances where the same scripting was being used repeatedly. I also realized that using includes could shrink the overall length of my scripts (making them easier to manage).

     

    //The file MainFile1 contains 4 includes (some are sequentially necessary rather than one larger include)

    MainFile1

     - include external01

     - include external02

     - include external03

     - include external04

    OR this

    MainFile1

     - include external01

     

    //The file external01 contains an include

    external01

     - include external02

     

    //The file external02 contains TWO includes

    external02

     - include external03

     - include external04

     

    Is there a benefit in processing speed or efficiency to be gained?

     

  18. It may just be a result of my development. I've found it easier to put JS on the same page as my HTML during development because it simple to see results and make adjustments.

    It seemed a little more cumbersome but (until now) I had no reason to avoid inline scripts.

    In fact, I've got instances where the JS only worked at the end of a form (ie: event listeners). How are those handled of I introduce the associated file in the HEAD of the HTML?

    What about AJAX?

  19. Are inline scripts inherently bad structure?

    Is the value gained from the meta tag worth the hassle of re-organizing my JavaScripts? Or can I get the same level of security from other approaches?

    I always thought that HTML was the easiest option of a webpage for a malicious actor to exploit.  What makes the HTML meta tag any different?

  20. Stumbled across this tag

    <meta http-equiv="Content-Security-Policy">

    and was interested in learning whether the tag is worthwhile, how effective it is, and how to best test its usefulness.

    Some reading seemed to indicate that once set, you cannot UNSET or lower restrictiveness, so I was confused as to how to test it appropriately.

    Any information, worthwhile links or advice pertaining to using this tag in association with pages that would interact with PHP and JavaScript code would be appreciated.

    Thank you.

  21. @mac_gyver I appreciate the additional insight but I generally like to understand the code that I use.  Thus, even if I were "handed" a solution, I wouldn't use it unless I would be comfortable with it.

    I thought my initial post would kick right in and function as planned, but SURPRISE... LOL

    Meanwhile, I've modified what I had and will manually cut & paste the result to save some keystrokes (unless I find some quick reference to enlighten me).

    Ironically, I've probably spent MORE keyboard pounding and time overload TRYING to make this work, than the potential time that the dynamic process would ever save.  LOL

    But I suppose that is what makes this so much fun.

    Thanks to everyone that offered their advice and insight.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.