Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm giving a small PHP course over the next weekend and i would like to present a few questions and exercises for my students, so they can practice with an objective, a fun one. I already presented the basics for them, now it's time for some action.
Finding ways to implement simple algorithms always provides great practice. If you think they're ready for higher level data structures (linked lists, graphs, etc.) then you could give them a Depth-First Search problem. If they're not at that level yet, try working with arrays and for/while loops. You can iterate lots of functions over entire arrays very easily. For example, average the values of an array, sum the values, or create a new array of N-1 elements (where the first array had N elements), each of which is the difference of element N and element N+1 in the original array.
If you want to try any of the examples into the real world, try grade calculation algorithms (given a list of grades, find the GPA) or shopping carts (you bought 1 of item X, 3 of item Y, 2 of item Z... total price?)
You can also make it a bit for complicated by having weighted grades (a B in a 3 hours class and an A in a 1 hour class = a GPA of 3.25)
I would also recommend doing a little bit of work with either databases or files input/output. The ability to save the results of your work and recall them later will GREATLY extend their understanding of complex larger systems like websites.
If you think it's not too complicated (I don't know the level of the students), one assignment I had in a class a couple of years ago (which we did in PERL) could be modified. It involved the following text document:
1 | Billy | Bob | Kentucky | Yale
2 | Sally | Sue| Virginia | Harvard
...
We were told to assume the pattern id | first_name | last_name | state | university, however there could be a variable amount of white-space. There were also some malformated entries, such as:
...
7 | Joe | 3 | Ohio | MIT
...
Clearly 3 isn't a last name. We were told to use regular expressions to verify that the ID was an integer less than 10000, the first and last names consisted only of letters, the state had to start with a capital letter and be followed by some number of lower-case letters, and the university had to consist only of letters. If there were any errors we had to say what the error was and what line of the file it was on. (For example: "Error on line 7: 3 is an invalid last name. Should be only letters")
After this we entered a loop (our program was interactive and ran from shell) where they could enter 1 for id, 2 for first name, 3 for last name, etc. They entered 0 to quit. Whatever they put in, they could then type a string to search for and it would find a student who matched that criteria and display their information. Instead of an interactive loop, if you're teaching PHP for use on a web server, maybe allow them to submit a form and check the $_POST information.
For example u can give them a simple for loop statement and ask them to implement it with while statement or vise versa. and do this for other statements like switch case and if.
Arrays are a stumbling point for most beginners I know. I'd personally run them through single-and multi-dimensional array looping and stepping. With MVC frameworks becoming so prevalent, the foreach loop and array functions become vital to programming success.
Related
I have database of laboratory analysis results. Due to the nature of analysis, the results a just a bit random- it is normal in real world. All result are stored in database in integers (we keep results in ppm - part per million).
The problem is, that I want to find similar (maybe same) material analysed several times.
Database looks like this:
id |v1 |v2 |v3 |v4 |... |vn
----|----|----|----|----|----|----
1 |4560|5600|3333| 56| |0
2 |4575|5583|3354| 67| |0
3 |4565|4333|3332| 60| |0
4 | 0|5583|3333| 60| |0
Material with id 1 and 2 is same, material 3 and 4 are unique.
I want to give tolerance +-30 or something to eliminate result randomines and find same material. It could be nice, that tolerance value could be individual for each column.
To simplify. How to ask mysql to return similar integers with value +- some error value?
You really don't want to do this using SQL.
Essentially what you're doing is calculating the connected components of the graph defined by your database, where two rows have an edge connecting them if they are within the tolerance you've defined.
Assuming that your dataset is small enough to fit in memory, which is likely if it's results of sample tests conducted in your laboratory, then your best bet is to read it all in and then use a suitable library to calculate the connected components. For instance, in Java, you could use JGraphT; if you need to do it in PHP then I'm sure there will be graph libraries available for that too.
At the high end, with massive datasets, there are libraries to do it on a Spark cluster...
I searched in the internet for an answer to select every columns that matches regex pattern. I didn't find one, or maybe I did, but I didin't understand it, because I'm new to DataBases. So here's the sql I was trying to run:
UPDATE `bartosz` SET 'd%%-%%-15'=1
(I know it's bad)
I have columns like:
ID | d1-1-15 | d2-1-15 | d3-1-15 | d4-1-15 ... (for 5 years, every month, and day)
So is there a way to select all columns from 2015?
I know i can loop it in php so the sql would look like:
UPDATE `bartosz` SET 'd1-1-15'=1, 'd1-1-15'=1, 'd3-1-15'=1 [...]
But it would be really long.
Strongly consider changing your approach. It may be technically possible to have a table with 2000 columns, but you are not using MySQL in a way that gets the most out of the available features such as DATE handling. The below table structure will give better flexibility and scaling in most use cases.
Look into tables with key=>value attributes.
id employee date units
1 james 2015-01-01 2
2 bob 2015-01-01 3
3 james 2015-01-02 6
4 bob 2015-01-02 4
With the above it is possible to write queries without needing to insert hundreds of column names. It will also easily scale beyond 5 years without needing to ALTER the table. Use the DATE column type so you can easily query by date ranges. Also learn how to use INDEXes so you can put a UNIQUE index on the employee and date fields to prevent duplication.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Let me start by saying that I am super new to web development so I apologize if this is a stupid question, but I have been looking around for days and haven't been able to find anything.
I am re-working the website for my wedding photography studio with a bunch of photographers in different locations and with slightly different skill sets and prices. I am currently using HTML, CSS, PHP, and mySQLi to build the site.
We are about to start expanding aggressively, so I would like to have our website do some of our work for us before the client ever contacts us. The general idea is that I want clients to be able to come to our site and search through our photographers based on location, skills, price and availability before they contact us to set up a meeting and book their photographer. So, I am setting it up so our photographers can make a profile, upload some general info and portfolio examples etc. into a mySQLi database. Then, clients can quickly search to find the right photographer for their wedding. That part was easy enough. Create a table, each photographer gets a row with individual columns for each piece of profile info.
Where I got stuck is the photographers' schedules. Clients start searching for photographers as much as 2 YEARS in advance and we want to make sure that client searches only bring up photographers who are actually available for their wedding date.
I have been looking around in here for days and from what I have read, trying to save multiple pieces of data in one column (which is to say, all of the individual dates that a particular photographer already booked) is a big no-no because then you can't query the data, which I obviously need to be able to do here. But, I seriously doubt that adding 730 columns (corresponding to one column per day for the next 2 years) to my user table is the solution, especially if we expand to hundreds (maybe even thousands?) of photographers over time.
Can anyone point me in the right direction here?
Thanks in advance!
You're thinking about the problem in the wrong way, what you want to know is if the photographer is booked, not if they are free.
You will probably have one table with all your photographer info, then have another table with bookings.
For example:
photogID | StartDate | EndDate
--------------------------------------------
1 | 9.16.2015 9:00 | 9.16.2015 17:00
2 | 9.17.2015 9:00 | 9.17.2015 17:00
2 | 9.18.2015 7:00 | 9.20.2015 15:00
1 | 10.18.2015 7:00 | 10.18.2015 11:00
1 | 10.19.2015 15:00 | 10.21.2015 19:00
When you query, you will search for the set of all photographers, except the ones who already have a booking that day.
I am starting to think about my new project and I've found a couple of speed issues, so I hope you can help me with selecting a good and elegant way to code it.
Each user has in the database records of "places" he has visited. Each place has "schools" - a number of schools in this particular place. Each school has classes. Each class may end its "learning year" at different times, so it's number should increment if date is >= end of learning year.
So we have such a database:
"places" table:
place | user_id |
-----------------
1 | 4 |
2 | 4 |
User no 4 visited place no 1 and 2
"schools" table:
school | place |
----------------
5 | 2 |
6 | 2 |
Place 2 has two schools - with id 5 and 6.
"class" table:
class | school | end_learning | class_number
---------------------------------------------
20 | 5 | 01.01.2013 | 2
21 | 5 | 03.01.2013 | 3
22 | 5 | 05.01.2013 | 4
School 5 has 3 classes with ids 20, 21, 22. If date is greater than 01.01.2013, the class number of class 20 should be incremented to 3 and end learning date changed to 01.01.2014. And so on.
And now we got into the problem - if there is 1000 places, each with 100 schools, each with 10 classes we got 1000000 records. It's a lot. Because all I have presented is just a simple example I have to consider updating whole database every time user refreshes the page so I'm afraid it might be laggy on that amount of records.
I also can serialize class into one field in school table:
school | place | classes
-------------------------------------------------------------------------
5 | 2 | serialized class 20, 21, 22 with end_learning field and class number
6 | 2 | other serialized classes from school 6
In that case I get 10 times less records but each time I have to deserialize data, check dates and if it's less than now alter it, serialize and save to database. The second problem is that I have to select all records from db to manipulate them not only all those need to be altered.
I am also thinking about having two databases: One with records that might need change in further future, and second that might need change in next 24hrs (near future). Every 24hrs all the classes which end learning in next 24 hrs are moved to "near future" db so every refresh of the page works on thousands of records, not hundreds of thousands or millions. Instead of that it works on millions of records (further future) to create "near future" table only once per day.
What do you think about all those database schemas? Maybe you have a better idea?
I don't quite understand the business logic or data model you outline - but I will assume you have thought this through.
Firstly, RDBMS solutions like MySQL are really, really good at managing large numbers of records, as long as the data you are working with is relational. As far as I can tell, you will be searching across many records, but only updating a few (a user will only be enrolled in a limited number of classes); I don't see this as a huge problem.
Secondly, it's nearly always better to go with the "standard" relational model until you can prove it doesn't meet your performance needs than to go for "exotic" solutions at the start off (I class your serialization and partitioning solution as "exotic" for the purpose of this answer). A lot of time and energy has gone into optimizing performance of SQL; if there were a simple alternative, it would be part of the standard solution. There are, of course, points at which the standard relational model doesn't scale (Facebook-size traffic, for instance), or business domains where the relational model doesn't really fit (documents, graphs). However, all the alternatives have benefits and drawbacks just like "standard" MySQL.
Thirdly, the best way to deal with possible performance issues is, well, to deal with them. In code. Build a test rig, create a schema according to the relational model, populate it with test data (e.g. using DbMonster), throw some load at it (e.g. using JMeter) and tune your schema and queries to prove your situation doesn't fit the standard solution. Only go for something exotic if you really can prove that you can't play nice with standard, relational database stuff.
I'm storing car parts in a database. Obviously, some parts will only fit on certain years of a certain vehicle.
When inputting the parts to MySQL, I'm having the user just use this format for the dates, "41, 42, 43, 45, 46" etc.. I then just implode, explode in PHP. That works, for what I needed to do.
Now I need to pull parts based on the years they are available. I don't want to have to use a multiple select box in my form if I can help it.
Any ideas?
Never, ever store more than one information in a single field, if you want to access them separately. Never.
You will need to rework you DB to have a jointable between model years and parts instead of haveing all model years crammed into a single field of your parts table.
Some tricky textual select will sort of work, but it will definitly kill your performance. If this is about a full parts catalog even for a single model, it will kill your performance to the point of unusability.
Sorry: Better honest than polite, if you really can't have both.
You have a many-to-many relationship...So create another table
Example...
part_id | Year
--------------
1 1995
1 1996
2 1995
2 1997
3 1998
etc etc...
Then you can get all the years a part works with using something like
select *
from parts
join parts_years
on (part_id = parts.id);