Averaging user-entered form data in a SQL database [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
Can anyone school me on how to have MySQL via Codeigniter syntax average form data and insert it into the corresponding database table upon submission?
I am able to collect the form information and store it in the database correctly. I just don't know the general coding for averaging the data and storing it in the appropriate column of table.
For instance: If the user enters five test scores: 98 99 46 93 and 55 and presses "Submit" each item is successfully inserted in its correct column in the table ('test1', 'test2', 'test3', 'test4', and 'test5'). However, I would like the sixth column to be the average of the scores (i.e. 'test_avg'). But, I don't know how to code that part of the process.

You should not store that calculated value in you table. Instead you shoud use a view on top of that table that performs the calculation for you.

you can simply use php for that
$avg=(98+99+46+93+55)/5;
and make an insert or update query.

Why not simply
(test1+test2+..+test5)/5 as test_average

CodeIgniter has it's own syntax for forms (See http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html as a reference), but in the end, it doesn't matter too much how you structure your SQL database. You will still need a controller that does the actual submission for you. At this point, you can add other data that you want to insert into the database.
I think you are already on the right track of having one column per field. For the average, calculate it yourself before you submit the form and insert it into the proper column.
$average = ($test1 + $test2 + $test3 + $test4 + $test5) / 5;

Related

MySQL backend, and PHP front search algorithm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I have reasearched a couple of options for MySQL & php search on the web and StackOverflow, but have not found something that really fits my scenario. Let me explain.
My scenario:
I have a table (TABLE1) in MySQL that has indexed terms (e.g. McDonalds, Microsoft, etc). This table has over 3000000 records.
I then have generic string that the user can enter - e.g. "There is a company called MCDonalds in my neighbourhood".
What i would like to look for occurances in the user entered string for any terms in my mysql table. I have implemented this currently by traversing each of the 3 million records in TABLE1 to see if it is in the user entered string (entered by user through a PHP page).
3 approach works 'well' - but of course it does not scale and the performance sucks :)
Any suggestion or pointers to algorithms to solve such a problem.. in a scalable and algorithmically sound way?
Thanks,
Joe
You should try MySQL's FULLTEXT search capability. Try something like this:
SELECT whatever, whatever
FROM TABLE1
WHERE MATCH(terms_column) AGAINST('There is a company called McDonalds in my neighbourhood')
You'll probably get lots of false positive matches, but this is an efficient way to at least narrow down the search.
It worked for me on a list of US cities matching against phrases like "Im going back to New Orleans to wear the ball and chain" and "Chicago, New York, Detroit and its all on the same street.".
You will need a FULLTEXT index on the your terms_column. And your table may, depending on the version of MySQL you're using, need to be in the MyISAM storage method.

PHP Algorithm and MySQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an equation that I need to process from a couple MySQL columns. I am just trying to figure out the best way of doing this once I run my SELECT statement to retrieve all the data.
Table - tbl_Spirits
Columns - ID, Volume, Proof
I know how to retrieve all the Data from MySQL, the problem is, is that there may be one record returned or 5. Probably never more then 5. Is there a clean fast method of the equation below in PHP.
This example is if there was 2 records returned.
((Volume1 * Proof1) + (Volume2 * Proof2)) / (Volume1 + Volume2)
Even if you did not show any attempt to solve your problem, I will give it a try. Basically you want to divide the sum of products between the two columns by the sum from one of them. So it should be something like
SELECT
SUM(`Volume`*`Proof`)/SUM(`Volume`)
FROM
`tbl_Spirits`
WHERE
1
and you will need to fill in your WHERE statement to fit your needs
Update: added this sample in sqlfiddle: http://sqlfiddle.com/#!2/0dc417/1/0
you will see that for the two records the value you will get is 4, exactly (2*4+3*4)/(2+3)

Using SQL table join to use a value from one table as a reference to a value in another (primary key) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have two tables stored on phpmyadmin, 'gigs' and 'bands'.
gigs: "gigID, gigBand, gigDate"
bands: "bandID, bandName, bandGenre...."
gigBand is a integer which corresponds to the bandID. I am aiming to diplay all the available gigs, but show the actual band name rather than the number. I have read table joins are the easiest way of doing this, and I have no trouble displaying the data of 'gigs' on the webpage.
But managing to display the name rather than the value is proving very difficult.
Any advice, or even an example of the code would be greatly appreciated.
You need to use a MySQL JOIN to pull the records of two related tables.
http://dev.mysql.com/doc/refman/5.7/en/join.html
SELECT * from gigs
INNER JOIN bands ON gigBand = bandID
This is pretty basic stuff though, so I would strongly recommend taking the time to do a bit more research. This seems like a good place to start:
http://www.tutorialspoint.com/mysql/index.htm

How should I implement a "like" counter to my site? (not facebook) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my website users can post submissions, and I want other users to be able to do what would be my equivalent of facebook's "likes".
But I don't know how to implement this. My main problem is that I need to prevent uses from liking the same thing more than once.
I was planning to add a long string into the database entry of each submission, for example "userid:rating/userid:rating/userid:rating". But I think that would be highly inefficient, because I would need to parse the string every time someone presses a button, and if there's lots of ratings then it would have to do a lot of work I think.
Should I make a separate table for ratings globally, and use the submission ID to link them to the right thing, or what? I feel it would be very inefficient to make mysql dig through the whole database and look for all individual rating entries that have a matching ID every time someone opens a submission page...
You create another table: submission_rating. Three columns would suffice: rating, submission_id and user_id.
Someone presses like, you do an an INSERT. But, before you do the insert, you check whether or not this particular user has already liked this submission. If the user has, you remove the like. If it does not, you insert the rating.
edit: As two gentleman below suggested, rather than relying on another check, go for the UNIQUE index. Make sure you catch the error and properly show it when trying to insert. Boldly said, if(!$insert) {}.

Performance of a big database, faster method: finding value with php or sql? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Lets say we have a very big (a lot of rows and cells) table in our database and we are looking for a very special value.
Which way will show us the result earlier?
a) put the table in a php array and get through this array
b) search for the value with sql commands
The database with an index on the column should be faster.
Even without an index, as you describe the problem, the database would be faster. Just moving the data to the php array is an expensive operation. Even if the database has to do a full table scan, it will only be moving back the row that you care about.
Assuming that the columns you are searching on are properly indexed, using SQL directly should be substantially faster. Otherwise, you have to select the entire table and loop through it in PHP without the benefits of indexes.
If you cannot search on an index for whatever reason, I think that both methods would be very slow. Essentially, there is no benefit to searching with PHP alone.
If the table is big, fetching the data to fill the array will take time, and searching in an big array will be slow (unless you have a special indexing method for your array), whatever language you use , php, java, c# or other.
Put an index on the column you are searching for: row, cell or only value (if you search only for a value), it depends on your search.

Categories