Jump to content

phppup

Members
  • Posts

    885
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by phppup

  1. @Phi11W  To expand on your reply, I understand that NULL and "" are different entities, but would one be preferred as a value?

    My (limited) experience has been that strings stored in a table (name, address, etc) are relieved and displayed "as is" (more or less).

    But numbers (even if inaccurately stored as VARCHAR) can be used within computations to generate totals (new numbers).

    Question:

    If a row in a table visually has a value of 2 in field1, a 4 in field2, and field3 appears empty, how will it effect the result of a math equation with regard to whether field3 is actually "" versus NULL?

    Will field1 + field2 + field3 equal 6 in both instances? Or will NULL cause a recognizable error?

    What about multiplication? Is either "" or NULL defined as zero? (I'm guessing NO, but....)

    I appreciate everyone's help.

    Thanks.

  2. @Barand  Thanks for two genuinely educational and understandable responses.

    Did some more reading too.

    For clarification, do you technically need

    quantity INT NOT NULL DEFAULT 0;
    TYPE VARCHAR(10) NOT NULL DEFAULT "SUMMER";

    Can the NOT NULL be omitted?

    Is there a reason to implement the (seeming) redundancy [since the default essentially auto-remedies the value from being null]?

  3. I have a table with 8 field titles and have successfully inserted data using a prepared statement targeting all 8 fields.

    I want to change my data collection strategy and INSERT into only 3 fields (where the other fields will populates via an UPDATE).

    This code gave me an error

    $sql = "INSERT INTO $table (id, first_name, company_name) VALUES (?,?,?)";
    if($stmt = mysqli_prepare($link, $sql)){
     
        mysqli_stmt_bind_param($stmt, "sss", $id, $first_name, $company_name);
    
    etc.

    starting that: Field 'field4' doesn't have a default value

    Am I obligated to provide a value for every field in the table, even if some are empty, or can I address ONLY the pertinent fields?

    Additionally, what is the "best" way to address the id field? Since it's an auto increment field and it seems like providing a false value is wrong. Yet a value appears to be required.

  4. Great info, @gizmola.

    But perhaps I should rephrase.

    Staying with the same premise, I'm really ONLY interested in tracking the history of inventory (at this juncture).

    We only sell one type of truck and one type of car (vehicle history is the only focus for now).

    I'm going to track when we got the car and the wholesale price with shipping and fees, and, of course, sales data.

    All the same info is applicable to trucks EXCEPT that trucks are sold on consignment. So, I need to include a "sell by date" and, if not sold, a "date of return".

    Do I include these 2 columns in a single table, and only fill them with data when applicable?

    Or do I use one table for cars, and a separate table for trucks ( where the two tables are identical except for two columns?

  5. In a hypothetical situation, suppose I want to record sales data for items in two broad categories: cars and trucks.

    There are obviously different makes and models within each category, but all have the same accessories (for the sake of discussion).

    However, trucks have a few accessories not available to cars, and vice versa.

    The inclusion of these accessories will reflect in the final sale price, which will be determined identically for both categories, and recorded along with basic info (date of order, date of pickup, payment method, etc.).

    What would be the correct/best database setup?

    One table that incurs some NULL entries of unavailable accessories?

    Two tables; one for trucks and one for cars?

    Something more?

    Reasoning? Pitfalls?

  6. No, I don't @requinix

    I want someone that looks at the form to see the drop-down apparatus with the phrase "Car choices" (similar to a text field placeholder).

    it can be visible once the menu drops open, but should NOT be selectable (like an optgroup).

  7. I thought of that, but am missing a connection.

    The radio selection kicks off a script that populates an empty <div> and displays a form.

    At that point, doesn't the form exist?

    Shouldn't the forms be accessible?

    This portion of scripting is AFTER and below all other code.

  8. Same issue.

    For some reason, I cannot get a response when I try to access by the inputs or form ID.

    It seems pretty straightforward, so I wasn't sure if having JavaScript create the forms was creating an obstacle that I wasn't unaware of.

  9. This is what I've done in the past, but it still allowed the NULL category to be a viable option (especially on a mobile device).

    I want something similar to an <optgroup> that is viewable prior to closing the drop-down.

  10. I have a lot of working code that is essentially doing this:

    <div id="mydiv">
      radio buttons 
     </div>
    <div id="div2">
      form populates here
     </div>
    
    <script>
      //listen for a radio to be clicked and then display the appropriate form in div2  
     if(radio1){ document.getElementById("div2").innerHTML = blah blah blah   
     if(radio2){  
    document.getElementById("div2").innerHTML = blah blah blah   
    etc.

    Now I want to listen for a change in any input on the form to trigger a response.

    However, I am having difficult accessing the form inputs.

    document.getElementById("name").addEventListener("click", displayDate);
    function displayDate() {
    alert('Hurray');
    }
    
    //does not work and essentially disables the JavaScript that follows

    I managed to gain success with

    document.getElementById("div2").addEventListener("click", displayDate);

    But this triggers the associated function with any click at any location in the div.

    How can I "drill down" to the specific inputs inside the form?

  11. This will give me a dropdown menu

    <select name="cars" id="cars">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select>

    and by default, the first choice, Volvo, will be visible to the user.

    This will let me categorize the menu for the user

    <select name="cars" id="cars">
      <option value="" >Car choices</option>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
    </select>

    but the user CAN select the "Car choices" as an option.

    Using an <optgroup> tag gets me CLOSER, but it is NOT visible until after the user clicks the drop-down.

    How can I create a visible category title that is not selectable in my drop-down?

  12. @kicken , I implemented your suggested CSS solution and I like it.

    It does what I intended and saves me the next step of dealing with reverting the colors onblur.

    EXCELLENT!!

    On a related note, after abandoning the specific JavaScript effort and using the CSS solution, I discovered a new conflict.

    Aside from the DIV changing color on focus/blur, I have a button to make the div a third color.

    Essentially, highlight on/off when DIV is selected, click button to highlight as a "reminder." (Only one section allowed)

    The issue that arose is that when a DIV is effected by the JS script it seems to lose the CSS toggle ability afterward.

    Each effect works okay independently, but together there is conflict.

    Is there an additional CSS solution.

    Is there a reason that the JS overrides the CSS?

    I have tried using the JS to "restore" the DIV,

    //After script had completed affecting the variable called element
    
    //changed the color
    element.style.background = "blue"
    //still lost the toggle effect
    element.tabindex="-1";

    but it didn't work. Solution?

     

  13. 3 hours ago, kicken said:

    Use a multi-line closure and log the value

    Yes, @kicken , that's the first thing I did; although I don't use a console, I used

    alert ("d is " +d);

    and the script froze at this point with no alert prompt visible.

    Perhaps I'll try the option you offered. Will it revert to the initial color on blur?

  14. A few questions have come up recently.

    Essentially, I have a webpage with several DIVs and I want the div that is in FOCUS to change color and thereby become highlighted.

    Additionally, I would want other DiVs to return to their original color (probably white) at the same occurrence.

    Using ONBLUR seemed "old school", so in the name of progress, I've created my problem. LOL

    Using this code

    const divs = document.querySelectorAll('div');
    
    divs.forEach(e => {
      e.addEventListener('click', (e) => {
      
        // remove the class from the others
        divs.forEach(d => d.classList.remove('active'));
        
        // add the class to the current one
        e.target.classList.add('active');
      });
    })

    with correlating CSS to change the DIV background color has its own issues (as it seems to be working on <h1> and <h2>inside the div, but not the full div itself).

    Things began to work when I added

    document.getElementById(e.target.id).style.background = "blue";

    to replace the ACTIVE class..

    My questions:

    How can I "see" the variable d value?

    I've tried lots of alterations and cannot see its value, ID, etc. (Mostly the script FAILS at that point when I try to "see" d.)

    My assumption is that knowing d will allow me to make further adjustments (to clear the background onblur [thus, a different focus]).

    Also, I want to clear all DIVs of the highlight with a click on a non-DIV element.

    Any helpful info or explanations are appreciated.

  15. On this site, we do not normally open attachments. In order to post code, use the <> button located in the toolbar.

    As for your issue, the PHP script will be read by the server when loading the webpage.

    It is currently doing what you have directed it to do (and has proven that it functions adequately).

    If you want to change your instructions to not run unless the submit button is clicked, then you have created a new CONDITION that needs to be added to the code.

    There are several ways to handle this:

    //let's say this is your button
    <input type="submit" name="my_submit" value="runPHP">
    
    //Assuming the button is inside a <form method="POST"> tag, you now have several alternatives or combinations to use as conditions 
      <?php if(isset($_POST['my_submit'])){
      //code to run if the CONDITION is met hours here
      } else {
      //what to do if condition is NOT met
      echo "Sorry, the button was not clicked";
      }  //end the condition
    
      //OR do this if you want to meet a greater condition 
      if(isset($_POST['my_submit']) && $_POST['my_submit'] == "runPHP"){
      //code to run if ALL the CONDITIONs are met
      } else {
      //what to do if conditionS are NOT met
      echo "Sorry, the button was not clicked";
      }  //end the conditions statement 
      
      ?>

    Hope this helps.

    You can research ISSET and see other examples online.

    • Great Answer 1
  16. @Barand

    Quote

    The checkboxes are names "vote[]" so they are posted as an array.

    Ok. I had initially built my form in a similar manner with the values and labels coming from an array (which I expected to eventually generate from a table) anyway.

    Quote

    $pdo

    Always confuses me. I know there are advantages to PDO (which I'll likely never be ready for), but I use procedural.

  17. @Barand Got a little busy, but as always, your help is greatly appreciated.

    It always looks so simple when you show the path.  And it seems you anticipated my next phase, which would be something along the line of "Choose 3 cities from the checkbox group that you would be most interested in visiting."

    If I'm understanding your example, I can easily expand the "vote" table to accept more INSERTed data with each form submission

    (This approach will NOT be inserting zero/null for unselected checkboxes, but without using 3 separate dropdown menus, what is the best/most correct way to format a smooth accumulation of selected itemsto be inserted into the table?)

    I haven't had the time to test my actual code, but it would seem modifying it to operate in a normalized methodology will be easier than jumbling through my "dog's breakfast of a table" set-up.  LOL

    Also from your reply, you use the character   "o"   , but I'm not sure I'm following its origin or meaning.  Can you clarify or link me to an explanation?

    Thanks!!

  18. I'm trying to workout the logic before writing my code and I've hit a bump.

    I'm setting up a table to receive votes and want to display the rankings.

    Example: what's the best party item?

    Each row will correlate to the person that submitted a vote.

    The essential columns would be balloons, soda, ribbons, bows.

    Voting for an item would INSERT a "1" (unless you're of a certain political party. LOL) into a cell.

    Now, I want to SUM() each column [I think I can do that] and list them in descending order.

    Can I do this in a statement? Or do I need to put the summed data into an array and then sort the array?

    What's the best approach?

  19. Anybody have any recommendations for a web-hosting provider that will offer PHP, SQL, email, and other assorted features that would be beneficial (like the ability to run CRON jobs, etc.)?

    @ginerjm I noticed in a recent thread that you cautioned against GoDaddy. Who would you suggest as an alternative?

  20. @ginerjm 

    Quote

    I would NEVER use W3 as a resource.

    That's good to know, but they come up on top of many searches and you failed to offer any constructive alternative as a recommendation.  Should I use nothing at all!!?!

     

    @mac_gyver Thanks for the helpful information.

    Much appreciated.

    PS: So, if I'm digesting this properly (and I do agree with your methodology) a value should NOT be subjected to htmlentities/htmlspecialchars UNLESS being used within an HTML context.  Tinkering with the value, even as a variable going into a database is essentially unnecessary?

     

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