I'm currently having difficulty finding a way of making my loops work.
I have a table of data with 15 minute values. I need the data to pull up in a few different increments
$filters=Array('Yrs','Qtr','Day','60','30','15');
I think I have a way of finding out what I need to be able to drill down to but the issue I'm having is after the first loop to cycle through all the Outter most values (ex: the user says they want to display by Hours, each hour should be able to have a "+" that will then add a new div to display the half hour data, then each half hour data have a "+" to display the 15 minute data upon request.
Now I can just program the number of outputs for each value (6 different outputs) just in-case... but isn't there a way I can make it do the drill down for each one in a loop? so I only have to code one output once and have it just check if there are any more intervals after it and check for those? I'm sure I'm just overlooking some very simple way of doing this but my brain isn't being clever today. Sorry in advance if this is a simple solution.
I guess the best way I could think of it as a reply on a form. How you would check to see if it's a reply of a reply, and then if that reply has any replys...etc for output.
Can anyone help or at least point me in the right direction? Or am I stuck coding each possible check?
Thanks in advance!
It's called "recursive" programming. Find a common theme in your values and look into writing a function that can call itself.
Check out this tutorial:
http://www.programminginterview.com/content/recursion
It basically involves taking a value, checking it conditionally, and then calling back on the function to do so again (or, in some cases, do something else with the value). It's a great way to drill down through multidimensional arrays that don't have any sort of fixed depth.
Related
I wrote a PHP that is currently looping through data, validating and importing them into a database. This can take some time, depending on the amount of data to run through.
My next step is to make this "user-friendly" so others can use it, not just me. I was looking at the components I want to implement, including a progress bar so the user knows the script isn't just stuck but is doing something.
Now the question comes up: should I use a JavaScript loop through the data, have them validated one by one and imported to the database with the help of an AJAX call to my php script? This would be pretty useful as well to update the progress bar after each iteration.
Or is it—performance-wise—better to do a single request to the php script which then loops through the data? In that case: how could I implement the updating progress bar?
These are a bunch of questions. The overall purpose is to see what my options are and what arguments speak for each. Thanks in advance!
Well if you want to have a progress bar, then you will need to do it 1-by-1, you would count all the records and then very time one calls back as completed, you would increment the progress bar by a certain percentage.
100 / amount of records = percentage to increase progress bar.
However, the issue with that approach is that if somewhere along the line it fails, you will need to know where it did so, or devise a way to make sure duplicates are not added to the database (If that's even an issue).
It would be better to add the data at once, faster and cleaner, but it would remove the possibility of having a progress bar that actually gives you a correct progress report.
I am creating a bit of software where people have birthday lists and they can put items in their list, and it’s viewable by other people. I have a table in my database where everything is organized called birthday lists. It is arranged like this:
An example row would be:
The person field is who owns the item, and all the items for everybody are in the same table. The id, name, link, and description are pretty self explanatory. The number field is how it is shown when you view the list page, so number 10 would appear above 0, and the priority is irrelevant to this question. This is an example of what happens when I view Jimmy’s list in my browser. (don’t worry, I will arrive at my point eventually)
I’m adding a lot of pictures because I never make any sense and this is the best way I can explain things :)
Anyway, here is another picture showing an example database.
If I viewed Russell’s list in the browser, it would look like this:
Notice how the $$$$ item is above the stapler because it has a higher number. That’s what it’s for. If I was logged in as Russell viewing this list, I would see buttons saying Move Up and Move Down that increment and decrement the number property on that item. I shall demonstrate my problem with an example scenario.
Let’s say Jimmy logs on and he has a lot of list items. He moves some up quite a few times, so it gets that the number value on one item is way above that of all the others. He then moves another item up, reloads the page, and thinks it didn’t do anything, because the number value of the second item is still less than that of the first. I hope that makes sense. Another problem is that if the number value on two items is the same, it is a huge issue. The point is, the way I have it isn’t going to work.
Does anyone know of a way I could get this number thing to work properly, and tell me how I could make it so users could move items up and down on their list? (also, if my database is organized badly, I’d love to know too :))
Thanks a bunch in advance!
Ending information, in case you need it:
I am building this with PHP 5 and MySQL
This is on a remote Linux server with GoDaddy
When you move an item up or down by one 'move' swap the 'number' values of the item being moved with the 'number' value of the item it is jumping over.
I would handle it this way:
For moving/sorting your items, you could use jQuery UI sortable for example.
You can get the order by it's method serialize().
Send the serialized data to your PHP script and swap/update the changed positions (number in your table)
Update your dataset in your database.
To SELECT your sorted data use ORDER BY number DESC (more)
Good luck! :-)
I'm having a problem figuring out an exercise my boss gave me as personal enrichment of knowledge sort of. Unfortunately I have been looking for a glimpse of answer everywhere on the web with no success and I'm now turning to you fellow programmers.
What he asked me to do is make a simple countdown from 100 to 0 (it can be displayed all at once). Easy enough so far eh? Just make a simple for loop or a while even. The problem here is that he asks that there are no assignations in the code, ex: $[var]=[value].
How can one even make a loop with no assignations? Since $i--; is the equivalent to $i = $i - 1; how can we count down?
I'm baffled by this problem which I can't resolve, I really want to find the answer as I am very curious on how this can be done.
Help is kindly appreciated.
Edit
Take note that this problem is kind of a 2 part where as the first part is to make it work 100 to 0, and in the second part x to 0 where x is inputted by the user
I will give you some hints, writing the exact code is an exercise for you.
No assignments, so you must have some type that can hold multiple values. Primitive types like integers cannot be used directly here, think of arrays.
Now, you cannot use variables, so using a loop and then printing the values is disallowed. Use one of the many ways to print an array (or the result of operating on an array).
The range() function does not accept a negative step value, you need to reverse the numbers.
My guess would be that your boss is trying to encourage you towards recursion rather than iteration. Recursion, as a technique, works extremely well for certain problems, and is an excellent tool to have in your armoury as a programmer.
Without giving everything away, try experimenting with defining a function that takes an argument - the "countdown" number -- and then calls itself in some way. You'll also need to separately kick it off by calling it once you've defined it.
Bear in mind that recursion must have some kind of termination defined, otherwise things can go very wrong. Here's an example of something going very wrong to get you started: :D
<?php
function infinity() {
print "Whoah.";
infinity();
}
infinity();
I need to create a quiz/multiple choice questions with correct answer.
Its basically for vocabulary, so 'A definition will be shown' and then possible Words would be provided.
My previous plan: Select word and meaning from Database, then generate the form for answers and then when user answers move on to next word.
I would be selecting words from database randomly, to keep it different each time. Also, I would be noting down how many times the word was answered correctly or incorrectly. So, that later on I can arrange them according to difficulty also.
My Problem: How do I update to the next question when user answers the question? How do I move on to the next problem, I thought I could do it easily using Jquery but integrating Jquery with PHP is tougher than it seems. I searched for plugins but they basically hard code the question in the JS, I wish to take them out from Database which would be updated as required.
Its a pet project, just so that I can help people practice their Vocabulary. I have implemented the basic design and also developed space for people to Contribute their words. The plans for expansion are vast with many possible directions, later, ranking system, facebook integration, etc. etc. I wish to finish it by the end of this weekend, so that I can move on to other things.
You can see vocabulary.dharamveer.in but you would only get idea about the basic application, not much regarding the problem at hand.
Eagerly awaiting your answers, thank you for help in advance :)
ajax is definitely the way to go with this, use ajax with json to load up your questions and then just have jquery loop through them.
Option 1
If you are creating a set length quiz, you could always create the full quiz off the start. That way you can ensure there are no repeating questions without the need to save anything. Then you can just loop through the whole quiz without making a call for each question.
Option 2
If it's a never ending quiz, then maybe you can do the questions in batches. Then when the user is done the old batch you can send that one back as a parameter and exclude them from the new batch. If your batches are large (for example 50 questions), than it would be at the very least 50 questions until a repeat and that's the worst case. Depending on how many questions you have total, you could end up with multiple batches before a repeat.
I would use Ajax to he the next question. Have another page simply retrieve a single random question and retrieve it with jquery Ajax.
I was wondering how I could quickly search a data string of up to 1 billion bytes of data. The data is all numeric. Currently, we have the data split into 250k files and the searches using strpos (fastest built-in function) on each file until it finds something.
Is there a way I can index to make it go faster? Any suggestions?
Eventually I would like to find multiple occurrences, which, as of now, would be done with the offset parameter on strpos.
Any help would surely lead to recognition where needed.
Thanks!
- James Hartig
Well, your tags indicate what you should do (the tag I am referring to is "indexing").
Basically, you should have separate files which would have the indexes for the data. It would have the data strings that you are looking for, as well as the file and byte positions that it is in.
You would then access the index, look up your value and then find the location(s) in the original file(s) for the data string, and process from there.
A good answer may require that you get a little more specific.
How long is the search query? 1 digit? 10 digits? Arbitrary length?
How "fast" is fast enough? 1 second? 10 seconds? 1 minute?
How many total queries per second / minute / hour do you expect?
How frequently does the data change? Every day? Hour? Continuously?
When you say "multiple occurrences" it sounds like you mean overlapping matches.
What is the "value" of the answer and to how many people?
A billion ain't what it used to be so you could just index the crap out of the whole thing and have an index that is 10 or even 100 times the original data. But if the data is changing by the minute, that would mean your were burning more cycles to create the index than to search it.
The amount of time and money you put into a solution is a function of the value of that solution.
You should definitely get a girlfriend. Besides helping you spend your time better it can grow fat without bursting. Oh, and the same goes for databases.
All of Peter Rowell's questions pertain. If you absolutely must have an out-of-the box answer then try grep. You can even exec it from PHP if you like. It is orders of magnitude faster than strpos. We've actually used it quite well as a solution for something that couldn't deal with indexing.
But again, Peter's questions still all apply. I'd answer them before diving into a solution.
Would a hash function/table work? Or a Suffix Array/Tree?