I'm looking for advice on writing a good function name as part of a web page I'm developing. It's coded in PHP and the function basically reassembles array data holding customer attendance information to a music venue (example time, date, entrance, etc) . The function takes in array data and returns the information formatted as a string that includes HTML structuring.
For instance:
//function formats array
...
$returnStr = "<span class='bold'>Entrance</span>customerData['entrance']";
The reason I ask is that any function name I come up with seems either too verbose or isn't completely clear about what it means. I have to maintain a lot of code, so I'm trying to choose effective names such that when I revisit code, I can quickly grasp what is going on.
Any online resources or personal insight would be appreciated.
There's no black or white in this case. But I believe that the best practice should be:
Logical - Describe what the function does
Comfortable - short and to the point
So you won't have to think on "Wait, what was the name of the function that does X and Y?" and you won't have to write too much code, like: printMusicVenueFromArray.
Both the "logical" and "comfortable" aspects are subjective and might be different from one person to another, so as long it's only you that work on that project - do what feels right.
When you have a team of developers working on a single project, consider drawing some guidelines before.
Start by describing the function in words, what's the input, what's the output.
Consider other functions that already exists in your code with similar names (you don't want to get confused).
According to your description:
function basically reassembles array data holding customer attendance
information to a music venue (example time, date, entrance, etc) . The
function takes in array data and returns the information formatted as
a string that includes HTML structuring.
Input
array data holding customer attendance information to a music venue
Output
(return) the information formatted as a string that includes HTML structuring.
Usually when I write functions that return something I'll start with get, but your function returns HTML string so it's more a view function so you can ignore it.
Now you should think on what describes the returned string the most, in my opinion, something like "MusicProfile" or "MusicDetails".
BTW, your quotes conflict.
Related
I would like to make full use out of MySQL for the purpose of a (web) application I have developed for a chiropractor.
So far I have been storing in a single row for [every year] for what are called progress notes. The table structure looks something like this (progress_note_id, patient_id, date (Y-0-0), progress_note). When the client wishes to append for the year of the current progress notes, he simply clicks at the top of a textarea (html), which I use TinyMCE JavaScript library, to make a new entry date along with the shorthand notes to go at the beginning of the column (progress_note). So far its been working ok, if there are 900+ clients (est.) there could potentially be 1300+ progress notes, for each year since the beginning of the application (2018).
Now the client wishes to be able to see previous progress notes (history), but is unable to modify any previous notes, while still be able to write new ones. The solution I have come up with is to use XML inside the textarea, and use PHP to decipher the new notes from the old ones.
My problem however is if I should have to convert my entire table from a yearly to a daily, that it could take up a lot of time and energy to convert multiple notes into each single rows, (est. 10x) Which could end up being 13,000+ rows. I realize that no matter what method I choose to do is going to be a lot of work. Another way around this perhaps I found was to use XML column type in MySQL to potentially store multiple records, and if I wish to append it, all I would need is PHP to interpret the entire XML and add a new child node, to the beginning. Each progress note is 255 - 500 chars. And in worst case scenario, if the patient was to be 52 times a year (1 for every week), there shouldn't be a large enough overhead.
Is this the correct way to solving this problem? I do wish to keep with MySQL DB and I realize that MySQL is not an intended for XML. And for some clarification, what I hope to accomplish is the same thing I intended to do with current progress notes, but with XML. I believe in ascending order (newer -> oldest).
<xml_result>
<progress_note>
<date>2020-08-16</date>
<content></content>
</progress_note>
<xml_result>
Thank-you for any of your time and for any suggestions.
Firstly, 13000+ is not a problem for mysql. In most case for web application, mysql can handle more than 10m+ records for a single instance with a good performance.
Secondly, you can use either XML or JSON format in a text field and handle the decoding in your application.
I'm in the early stages of developing a site that will have a webpage for each database entry. My question is - what are the pros and cons to having either (1) a single php page with a $_GET returning the specific database item or (2) individual webpages for each item?
For instance, example 1 would be like this -
mysite.com/alphabet.php?letter=a
mysite.com/alphabet.php?letter=b
mysite.com/alphabet.php?letter=c
Example 2 would be -
mysite.com/alphabet/letter_a.php
mysite.com/alphabet/letter_b.php
mysite.com/alphabet/letter_c.php
The only site I built heavily off of MySQL, I used example 1. Given that the code to display these would function the same for each entry, I'd assume that example 1 would be the best practice, but when I go to similar sites, they seem to favor example 2.
I've tried to search this site as well as the web for this answer, but it seems to be an awkward search phrase, as I've returned empty handed. Any insight would be much appreciated.
Actually both usages are old school for last years. Following formats used mainly:
mysite.com/alphabet/letter/a
mysite.com/alphabet/letter/b
mysite.com/alphabet/letter/c
This format is called friendly url or pretty url and in most cases this points to Alphabet controller's letter action with one argument a,b or c which will be like:
Class AlphabetController {
public function letter($letter) {
$DbModel->getListStartingWith($letter);
}
}
Working on a project that requires incoming email to be parsed, and certain information be extracted and stored in the database. We're using postmarkapp to extract the body content of the email so we only have the text only guts of it, but I'm currently a bit stuck on how to parse the email the most efficient way.
Over time we'll be adding more 'accepted' formats of incoming mail, but to start off with we'll have probably 4 common emails coming in, that is, they'll follow the same format and the information that we want to extract (contact details, id's, links, bio) will be in the same place, (per supported format).
I'm thinking that we'll have an interface that will handle the common tasks, and each supported format will implement that, however just how to get that information is where I'm stuck.
Open to any thoughts and ideas on different methods / technologies to do this, ideally PHP, but if we need to use something else, that's fine.
There is a similar feature on a site that I developed. Our users get emails from their suppliers with pricing. They copy and paste the body of the email into a textarea on our site and click a button. Then we parse the text to find products and prices and stick the info into a database.
To do the parsing, we first have to determine the supplier, like you'll need to do to determine which template was used. We look for certain strings in the text - the supplier's name usually, or a line that's unique to their emails. We do that in a method called something like getParserForText(). That method returns a Parser object which implements a simple interface with a parseText() method.
There's a Parser implementation class for each format. The parseText() method in each class is responsible for getting the data out of the text. We looked for ways of making these elegant and generic and have simply not found a really good way to do that. We're using a combination of regular expressions, splitting the string into smaller sections, and walking through the string.
Pseudocode:
$text = $_POST['emailBody'];
$parser = getParserForText($text);
$result = $parser->parseText($text);
if(count($result["errors"]) > 0)
{
// handle errors
}
else
{
saveToDatabase($result["prices"]);
}
We have no control over the formats the suppliers use, so we have to resort to things like:
split the text into an array of strings around each line with a date (prey_split())
for each element in that array, the first line contains the date, the next three to six lines contain products and prices
pull the date out and then split the string on new lines
for each line, use a regex to find the price ($000.0000) and pull it out
trim the rest of the line to use as the product name
We use a lot of prey_split(), preg_match_all() and explode(). While it doesn't seem to me to be particularly elegant or generic, the system has been very robust. By leaving a little wiggle room in the regular expressions, we've made it through a number of small format changes without needing to change the code. By "wiggle room" I mean things like: Don't search for a space, search for any whitespace. Don't search for a dollar sign and two numbers, search for a dollar sign and any number of numbers. Little things like that.
EDIT:
Here's a question I asked about it a few years ago:
Algorithms or Patterns for reading text
Since it's generated email, It most likely comes in an easily parsable format, such as one line per instruction; key=value. You can then split the lines on the first =-sign and use the key-value pairs that this gives you.
Regular expressions are great for when you don't have control over the incoming data format, but when you do, it's easier to make sure it is parsable without a regexp.
If the format is too complex for such simple parsing, please give an example of a file using the format, so I can make the answer more specific. Same thing if this isn't an answer to what you mean to ask: please give an example of the sort of answer you want.
I have a list of strings (names) which I would like to match to the database containing the same or variances of these names.
For each of the strings I want to match I can query the database, but this doesn’t seems to be efficient since the database is a fix set of names.
I was wondering if it was possible to have this match being done within PHP. I can use the levenshtein function in PHP, but I was wondering if there is anything more efficient.
The example I want to get to. On the left are all the strings I want to see if I have this in the database (or a small variance). Next to each I would like to have a pull down list containing the options that match closely.
String 1 – pull down
String 2 – pull down
String 3 – pull down
What is the best approach to this? I have about 500-1000 strings for which I would like to get a suggestion/pull down menu.
With kind regards
Ralf
Perhaps have a look at MySQL's full text search feature. I found this article on DevZone: http://devzone.zend.com/26/using-mysql-full-text-searching/
If you want to do it client-side, jQuery UI Autocomplete is what you want. Not only that is very easy to configure it for your needs, but you can do it with only 1 query that would get all the strings and save it into a local list, the jQuery Autocomplete data source.
You can then register an onkeyup event for an input and the jQuery plugin will query the existing cached datasource(no more pressure for your server).
Check it out:
http://jqueryui.com/autocomplete/
I have a MySQL Database of more or less 100 teachers, their names, and their phone numbers, stored in tables based upon their department at the school. I'm creating an iPhone app, and I've found that UITableViews and all the work that comes with it is just too time consuming and too confusing. Instead, I've been trying to create a web page on my server that loads all the data from MySQL and displays it using HTML, PHP, jQuery, and jQTouch for formatting.
My concept is that the separators will be by department, and the staff will be sorted alphabetically under each department. On the main page, each person's name will be clickable so they can go to ANOTHER page listing their name, email address, and telephone number, all linked so that the user can tap on the email or number and immediately email or call that person, respectively.
HOWEVER, I am completely at a loss for how I should start. Can anyone point me in the right direction for displaying the data? Am I going about it wrong in using PHP? Should I opt for something COMPLETELY different?
PHP to manage the database interaction and generate HTML is fine. There are heaps of tutorials on how to do that (e.g. http://www.w3schools.com/PHP/php_mysql_intro.asp) How to make it look nice is beyond the scope of this answer, and I'd recommend you search for table/CSS examples to get some ideas of what looks good and how they're implemented. If you need interactivity such as expanding rows or changing colors, then jQuery would be an appropriate next step, though you certainly don't need more than HTML + CSS for a nice looking table representation.
What I don't know about is the auto email/call functionality you're after, and whether you can get that "for free" from whatever is rendering the HTML. That's iPhone specific, not PHP/jQuery/etc... And I'd second Alex's advice that if UITableView is the right tool for the job then you will definitely be better off in the long run just buckling down and learning it. (And going through that will probably make pickup up other parts of the API much easier to boot.)
Instead of loading my PHP in my <body>, I created a function that retrieved the data via mysql_fetch_assoc(), which added all the information and created each individual div of data AS WELL AS injecting a <script> to $.append() the list item content for each item retrieved via the mysql_fetch_assoc(). Thanks for the responses anyway!