PHP Algorithm and MySQL [closed] - php

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)

Related

How to retrieve questions and answers from mysql database by using php files [closed]

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 6 years ago.
Improve this question
i would like to make a Quiz Application but i have some problems and i am not too sure as to how I should approach them. I am at a fairly basic level for php so was wondering if anyone could help me?
In mysql I have a database with a "Questions" table and "Answers" table.
The layout of my database on mysql
My database contains 20 question with each of them having 4 possible answers as can be seen in the link above.
I would therefore like to select all the questions and display them in a random order along with the multiple choice answers. I'm quite unsure of the coding to use. Through research I think I can use:
RAND() LIMIT 0,19 in order to randomise the questions.
The end of plan is to display the questions and answers in textview boxes on android studio. The user interface design is as follows:
User interface
I apologise that i can not give much detail on a solution it is due to my inexperience of PHP. Any help to retrieve questions and answers from mysql database through php files would be greatly appreciated.
Thanks in advance
Not sure about php, but
1. you can create an ArrayList and generate a random number in a range of your total question's id, //if you have 10 questions then 1-10
If that id isn't in the array, add it, and get the question from your question table according to the id that you've just generated
And also get your answer from the answer table and put it in another array, and then generate other three random numbers for your answers and put in the second array.
then generate number from 0 to 3 and for the second array to put the answers in 4 option(TextViews), you might wanna use a switch case approach here. (must be unique, only for each question, you can regenerate the same answers for other questions too)
if the users clicks/chooses any of them check with the answer and there you go
6.

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

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

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;

Deleting everything in database less than a number [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 PHP, I would like to run a query that would delete every row in a database that had the ID of less than a figure.
The context I want to use it in, is running a PHP CRON job every 24 hours that will delete all rows with values in the 'time' column that are LESS THAN time();
Putting it in to another context: I want to run a query that will delete every row that has an ID of ~10.
Thanks
I imagine you are not very familiar with SQL. This is a simple delete statement:
delete from t
where id < 10;
This assumes that by "database", you really mean "table". It also makes some other assumptions about foreign key references in other tables. Because the question seems so basic, I am guessing these are not issues.
However, I would recommend that you study up a bit on databases if you are going to use them in your applications.

PHP read specific row [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
I have a PHP script that displays data read from a MySQL database. I want to filter the output to only show rows where two columns contain specific values. How do I do this?
Add a WHERE clause, e.g., WHERE column1 = 'specific-value1' AND column2 = 'specific-value2'
Here's a link to MySQL's SELECT syntax:
http://dev.mysql.com/doc/refman/5.1/en/select.html
(and in German):
http://dev.mysql.com/doc/refman/5.1/de/select.html
no idea why you are posting in german on an english site and then translating it to english. But what you need to do is something like "select * from table where column1='val' and column2='val'". Though it sounds like you have a large set of data pulled off already and you want to filter that. I don't see why you shouldn't just query what you need from the database instead of pulling everything off and then filtering.

Categories