-
Posts
6,906 -
Joined
-
Last visited
-
Days Won
99
Posts posted by ginerjm
-
-
Sorry about my previous post. For some reason the forum is acting strangely today. I did not spread my code out like that intentionally.
-
$first = true; while($row = mysqli_fetch_assoc($result)) { if ($first) { $first = false; continue; } ... ... ...
-
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.
-
YOu need to show us some contiguous code here. A radio button only returns one selection so there is no need for a loop. Show us the whole code where you build your html (the query that produces the data would be nice) and then how you completely receive the data.
We can't possibly understand what you mean by having multiple ids in one radio button.
-
I'd like to see the code that you actually used to receive the input values from your html. PS - why do you use a loop on the radio button input when there will only be one?
-
Strange? What happened when you set your column widths as I suggested?
-
The url that would be used is:
domain.com/mypage.php?quitdate=x
To receive that value in your script you need:
if (isset($_GET['quitdate']))
$quit_date = $_GET['quitdate'];
else
(handle a missing argument however you wish)
Of course you then need to validate that value to be sure it is something you expect
-
If you want:
21, 33, b4, 42
You need to output that:
echo "$key$val, ";
If you don't want that, I'm not sure what you are asking for.
-
When you declare your table, add specific col widths to it so that the format isn't determined by the browser. Look up the <col> tag
-
If you did all this, WHERE IS THE CODE?
-
So - where is your attempt at getting to your goal? You wrote all the php code (which you really need to post properly next time) but you didn't write the easy part.
-
Without looking at your code, I am curious about your question. Whatever do you mean by asking what the "correct method of sharing a database object/connection"? A script opens a connection, uses it and it goes away when the script terminates. What is the "sharing" you are referring to?
-
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.
-
I agree with QOC - why bother with trying to do some hanky-panky with sessions (?) when a db is so simple and direct?
-
Benanamen,
The db parm is there because the same included file is used in all my scripts. Obviously I don't write all my scripts to use the same dbname, so that is why. Make sense now?
-
Then the MySQL functions from my dev.mysql.com are not accurately described for my feeble mind..
-
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.
-
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
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?
-
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?
-
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.
-
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.
-
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.
-
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?
-
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.
PHP code writing format?
in PHP Coding Help
Posted
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().