Jump to content


  • Posts

  • Joined

  • Last visited

  • Days Won


Posts posted by phppup

  1. @Barand So I guess at this point my question is, what is the correct syntax to use within the option tag while creating it with PHP?

    It seems simple enough under "normal conditions", but I am echoing

    echo "select name='amt_".$i."'>".$quantity."</select>";

    the drop-down and using PHP to populate the options through a loop.

    I can't put an echo inside an echo, can I?

    I understand that some re-working will be necessary, but my goal was to create the for as a large PHP echo without direct HTML usage.

  2. It looks like you have to find the LENGTH of each string to start.

    Then create an array for each variable that will hold the values of each position.

    Then use a loop to assemble a new STRING created from $a[0]$b[0]$a[1]$b[1].... etc.

  3. Quote

    That way I don't have to write the same 6-10 lines of code to handle those two scenarios for every input field.

    @Psycho Thanks for the outline of information. I've begun to understand and get comfortable with the effectiveness of  arrays (I think... LOL).

    This is an attempt at using arrays meaningfully.

    Thanks for the guidance.


    @Barand  I think I follow what you are doing here, and I'm trying to confirm it to my code.

    However, my understanding of PHP-shorthand is not very good. Can you elaborate/simplify what this


    <?= qtyOptions($_POST['qty'][1] ?? 0)?>

     actually means, please.

    I realize that:

    qtyOptions is calling the function and

    $_POST['qty'][1] will provide a value as $current

    What is <?= doing?

    What do the ?? mean?

  4. I'm not sure if I've got a syntax or an implementation problem, but I've created a form entirely in PHP.

    Essentially, each item is pulled from an array and named with the associated key index.

    Each item is attached to a drop-down menu containing integers 0 thru 10 (indicating the quantity for an order).

    On submission of the form, I want the selected values to be retained.

    If other fields of the form are not complete, error messages will display, and I want these values to be retained rather than resetting.


    for($i = 0; $i < 11; $i++){
    $quantity .= "<option>".$i."</option>";
    for($i = 0; $i < count($arr); $i++){
    echo "select name='amt_".$i."'>".$quantity."</select>";

    How can I have each drop-down retain it's value after a (failed) submit effort?

  5. Thank you @Barand @kicken @requinix

    It's a lot to sift through.

    i had another thought and would appreciate some feedback on it.

    Previous lessons have indicated that trusting user data can be unwise. And in this instance, the possibility of duplicate file names from users submitting images of BIRDS is probably high. Therefore, renaming the files is very likely.

    If a user is required to authenticate before upload, and is then entered into a separate table with an auto increment, can I use the following methods successfully:

    auto increment field is 'order' 

    rename image as order + TIMESTAMP (perhaps only month.day.time) [within calendar year]

    In this way, having the order as the leading digit will automatically set a very large range for each user.

    My thinking is that it will also create a set of numbers that will be sequential as a group.

    Overkill or good idea? Pitfalls?


    (If I use this, it is technically a hybrid of everyone's assistance. Who should get the solution mark?)

    *Perhaps we can create a "community achievement trophy" for teamwork within a thread*

  6. How about a database table with a column called "logins".

    Add 1 to the user each time there is a successful login.

    Then, if logins == 3, create a warning message that indicates they've reached the limit.

    (Heck, you can even use this to create a message at every visit to indicate how many visits are remaining for that user.)

  7. @kicken How would I do that?

    I ultimately want to be able to refer back to the sets of images and know that this set were from Bob and this set were from Joe (as a neat sequenced set within a sequential group).

    In this way, if images 8,9, and 10 are outstanding, I can easily give credit to whomever is responsible for image 7 through 12 because there are no shuffled results

  8. I skimmed this thread from the beginning, and while I am not nearly as experienced as those who've been assisting you, I have encountered many issues in coding.

    My suggestion to you would be to create a NEW simple, form with ONLY one field.

    <input type="text" name="first">
    <input type="submit">

    Add a very basic PHP script to create a table with the one field. Load the code with ERROR detection to give you clues to every potential defect.

    Once you are able to successfully populate the database table with a piece of data from the form, you can THEN expand the code to include additional fields, validation, etc.

    Create a new version with each effort so that you always have a working template to return to and then just continue to build on the foundation until you have what you wanted.

    It may take longer, but you will learn and get a better understanding for the future.

    Good luck.





  9. I want to allow users to upload images to a directory.

    I want the images renamed sequentially and lumped together with each upload.

    Eg: If Bob uploads 3 images to the BIRD directory, the files will be named BIRD1, BIRD2, and BIRD3.

    When Tom then uploads 6 images to this directory, they will be sequential from 4 to 9.

    And when Jane adds 5 the numbering will run from 10 to 14.

    I suspect that in each instance I will need to SELECT an "image quantity" column and initiate a number sequence from the sum at that point.

    But my question is more about maintaining the integrity of the sequences.

    If Tom and Jane upload somewhat simultaneously, how can I control the outcome to prevent Tom from having numbers 4,5,8,11,13 and 14 with Jane's images interspersed in the sequence?

    Obviously the two uploads will be competing based on image sizes, network speed, etc.

    How can I keep the separate batches organized as a group?


  10. how can I retrieve the single variable that I want from the function?

    A similar example for what I'm attempting to accomplish

    function doThis($num=4){
    for($i=0; $i<$num; $i++){
       echo $i."<br>";
        $seq .= $i;
    echo $seq;
    echo $seq;  //get variable here

    How can I access the variable that is created inside the function, and use it on the outside?

  11. 21 minutes ago, Psycho said:

    And, if you like your code, why are you asking?


    21 minutes ago, Psycho said:

    But, it is the right way to do it

    Thanks for the explanation.

    And that's why I asked. Just because I "like" my code doesn't mean it's doing everything that it should be doing as effectively as it could be doing it. And I want to learn and stride to do it the right way.


    Aside from instances with a unique parameter,should I be doing this (try & catch) regularly with all my INSERT statements?

    It would probably reduce the amount of code and PHP checks (ie: input length) while putting a heavier burden on the SQL database. Is that advisable? Preferable?

    Learned allot. Thanks again everyone.

  12. 16 minutes ago, mac_gyver said:

    the initial SELECT query, which can tell your code the wrong answer.

    So, if I'm comprehending this correctly, it not do much about determining whether the unique element is available (which the SELECT will accomplish) but MORE about determining whether the value is INSERTable (which is defined in absolute terms by the field being unique).

    • Like 1
  13. Thanks for the input from everyone.

    I'm trying to digest all the info, but I'm still a bit uncertain as to why this is better than my code that checks (and prevents) the acceptance of a name that already exists.

    Isn't something like

    $sql="SELECT username from table where username = $username";
    if($result = mysqli_query($link, $sql)){
        if(mysqli_num_rows($result) > 0){
            //BINGO! it already exists               //take appropriate action
         } else {
    //proceed with the insert 

    doing the same thing?

  14. Finally found success after my last debacle.

    Now it's time to INSERT my data into a table.

    My data is $firstname, $lastname, $username

    Since the $username is UNIQUE, it seems advisable to use a try & catch statement.

    How do I isolate the $username to do this effectively?
    How is this method better than using IF statements?

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


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

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

  17. @kicken 


    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.

  18. 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???

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