Jump to content

ginerjm

Members
  • Posts

    6,906
  • Joined

  • Last visited

  • Days Won

    99

Posts posted by ginerjm

  1. as Maxdd says commenting a much ignored attribute of programmers.  Sometimes when we get deep into a process and develop some code to do some tricky maneuver we lose all sense of it just a couple of months later when we come back to 'tweak' it a bit.  Comments about your thought process when you write something are essential!

     

    Unfortunately, I can't agree with Maxdd on his use of camelcase for naming things.  It's bad enough that JS endorses it - there is no reason for you to introduce it in your own code.  A needless distraction in my opinion since it leads to many errors during development where you inadvertently leave out a capital letter which PHP will not catch and WILL give you an error or worse.  IMHO - stick to all lowercase for your vars.  Another of my preferences is to use initial caps on my own function names just to make them stand out.  MyFunctionName() versus myFunctionName() or myfunctionname().

  2. Great question!  More people should think like you!

     

    There are lots of preferences on "how to indent".  So what I'm showing you is just my personal choice.  I believe it gives one an easier view of the code and makes it much quick to browse thru code looking for "blocks" of statements.

     

    I indent any block of code that is wrapped in curly braces (  { }).  I indent any single lines that are part of an if else statement.  I indent all function code, again because it is inside those braces.  I indent if statements that are inside other if statements (of course, I try not to nest too many if statements).

     

    Below is a sample ( I hope the site shows it accurately) of what my code would look like:

    php line
    php line
    php line
    if (condition)
    {
         tabbed line
         tabbed line
    }
    else
        tabbed line
    php line
    php line
    php line
    //******************
    //******************
    function MyFunction()
    {
         php line
         php line
         php line
    }
    

    ( I see that the forum added blank lines between my original input.  Oh, well....)

    (note that this code was posted using the proper code tags for this forum ('php' and '/php' wrapped in square brackets)

     

    Also it is encouraged to separate your main html code away from your php code.  Too many people think in a straight line as they write a script and begin by beginning the html document before they do any php.  Start your script with the initialization you need (a session_start is always a good start), determine what you need to do in your php, then do it and save any dynamic information to be displayed in php vars and at the end of your processing output the html document in its entirety, including those php vars where the data they contain should be displayed.  Some html may be generated by the php process, such as building a drop-down box or an html table but that should all be in a loop that is producing the data to be contained by them and therefore makes sense.

  3. Why did you think that using Session vars was the way to go.  If you have a 'set' of data, you store it in a table (one data item per field) and use some unique value for that set of data, ie, session, to be the index of the records in the table.  Then when you want to get the data for a particular "session" you query for the corresponding key/index.

  4. I put my connection logic into a module by itself as a function.  When I'm writing a script that requires db access I make sure to include that file at the top of my script and call the function (in it) when I need to open a connection.  I use the dbname as an argument in the call to that function.  The function returns the connection handle which I can then use in my script.

     

    My connection code:

    //   Filename:  sql_connection.php
    function PDOConnect($dbname)
    {
    // my connection code follows
    ...
    }
    

     

    And when I need it in my script I do this:

     

    <?php
    ..
    ..
    ..
    require($path_to_php."/sql_connection.php");
    $pdo = PDOConnect('my_db_1');
    $q = "select.......";
    $qresults = $pdo->query($q);
    

     

    Of course you only have to make the connect call once in your script, unless you deliberately close it or need another handle to the driver.

  5. I did RTFM and am pointing out that it says it creates a datetime field, or a DATE or TIME field and  not a string

     

    STR_TO_DATE(str,format)

    This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.

     

    So his original query was trying to compare the string (type) value that curdate provides against a date(type) value from str_to_date.

     

    Have I read RTFM incorrectly?

  6. A good way to solve this problem and many other future ones is to design your script to do ALL of your php work at the top and ALL of your html work at the bottom.  That way everything that you want to appear on the page is ready when the html part is sent to the client.  It also makes it easier to read and interpret and perform maintenance on at a later date.

     

    In this case - simply set your $pagetitle var once you grab the query result record.  PS - why do a loop instead of just a simple fetch since your query appears to only be seeking a single record?

  7. I may be confused.  You say that you are required to post your source code on your 'partners' server.  Is that your client that you are writing this code for?  Does it have to be posted as you write it or is that just a requirement of the project's completion?  Can you not do your development in a similar environment separate from that server so that you don't show anything until completion and the client is happy and you have been paid?

     

    You can never keep the client from seeing your source code once you have turned it over to him.  But at that point, why would you care?  OTOH - if your concern is NOT the client, but the users, putting it outside of the web-accessible tree as I mentioned earlier would be the way to go.  If your client allows users to have ftp access to those areas, it is out of your control and the client is assuming all of the risks in that situation.

     

    As I said before - your concern is not clear to me.  Who and 0what is involved needs clarifying.

  8. Did you display the sql error message?  It might have pointed you at the problem if you only bothered to do these kinds of things as a beginner.

     

    Always code to check things instead of just assuming that they are going to run for you.  Check if a query ran and if not output the message.  Check if your db connection succeeded and send an echo if it doesn't.  Check if the external file you attempted to open actually was opened.  Enable php error checking always when doing development so that you can see and clean up errors as they occur.

     

    Programming is about putting together a fail-safe solution to a problem.  Kind of like baby-sitting a two year old - you can't assume anything!

     

    Hint:  check for unneeded commas.

  9. Barand gave you the syntax for the comparison.  I'm trying to tell you that str_to_date will not work  Those are the two pieces of your puzzle that you need to work on.  Either write a query to convert your table completely or use the correct function in place of str_to_date to get a datetime result for your compare.

     

    Since you apparently don't like to do your own research ( a bad tendency for a future programmer!), here's a link that you should find useful:

     

    http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html.

  10. Read the function name again.  "string to date".  It produces a datetime, not a date string like yyyy-mm-dd.  Basically it is not a human-readable value.

     

    You say that the input for the completion date field was not in a recognizable format, yet you are trying to use it with a MySQL function.  Couldn't you have done that when you imported it and avoided this complexity?

  11. Is Completion Date a string or a date field?  If it is NOT a string, why are you using str_to_date on it?  If it is a string, why isn't it a true datetime field in your database?  Would make it much easier to use instead of the format that your format-string implies. 

     

    Also - your str_to_date call produces a datetime format value.  The curdate function returns a string value.  Kinda hard to compare the two.

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