PHP, MySQL Dynamic Maths - php

I am trying to find out a way to do maths dynamically. I want to be able to add a formula to MySQL and then solve it in PHP and display the answer.
So I'll save it like getItemPrice(10) * getItemPrice(50) However I'm unsure how to execute functions in PHP if its saved like that.
I thought about splitting the date but I want to be able to do more advanced formulas then that. I have no idea how to do this to be honest...

If the text you stored in db is a valid php code you can use
eval — Evaluate a string as PHP code
http://php.net/manual/en/function.eval.php
otherwise if the text is a valid mysql code you can generate a dinamic sql statementes and the execute as a normal sql statemets ..

Related

how do i insert math quadratic equation/formula in database?

I want to create online math quiz and all questions and answers and correct answer are store in the mysql database. I have quadratic equation like.
now how do i insert exactly same question formula or equation with their answer in the database table column and database column type is blob.
Database Column After Inserted Formula or Equations
Inseet Query
if(isset($_REQUEST['submit']))
{
$class1=$_POST['class1'];
$subjects1=$_POST['subjects1'];
$lessons=$_POST['lessons'];
$marks=$_POST['marks'];
$length = count($_POST['ques']);
for($i=0; $i<$length; $i++)
{
$query=mysql_query("INSERT INTO quiz
VALUES ('','$class1','$subjects1','$lessons',
'".$_POST['ques'][$i]."','".$_POST['ans1'][$i]."',
'".$_POST['ans2'][$i]."','".$_POST['ans3'][$i]."','".$_POST['ans4'][$i]."',
'".$_POST['corans'][$i]."','$marks')") or die ("quiz insert query");
}
}
can i use MathJax is an open source JavaScript for quiz and it will work for php and mysql databasee query?
Your question is still pretty unclear to me, but I'll try to answer as best as I can. The way I understand it now, someone (a teacher) is composing a question, including the formulas, and many other people (the students) should choose the correct answer from a fixed set of alternatives. So only the author of the question has to get the math formula into the system, while the students must see the nice math rendering.
You write (in a comment) that you intend to paste the equation into the web page. I don't know from what other application you intend to paste it.
Entering LaTeX notation manually
I do know that most professional mathematicians will probably feel most at home in writing down an equation the way LaTeX understands them. And that is one of several possible input formats for MathJax. In the case of your formula, the LaTeX markup could look like this:
f(x) = a_0 + \sum_{n=0}^\infty \left(
a_n\cos\frac{n\pi x}{L} +
b_n\sin\frac{n\pi x}{L}
\right)
So one possible approach would be providing a text area where users could enter markup like this. Then you could store that markup in just that form inside the database, and use MathJax to render a nice formula in the resulting HTML page.
Using an external editor
If you insist on using an external editor to compose formulas, you'll have to provide more details. What editor(s) do you have in mind? In what formats do they offer their documents on the clipboard? Can they export to a file in any established format for math notation?
The most likely solution would be getting that external editor to somehow export a MathML file. That file (which is an XML format) could then be uploaded (using a file upload form element) and stored in the database, and either embedded directly in HTML, thus relying on the math rendering modern browsers provide out of the box, or again be used as the input to MathJax to increase portability with older or misbehaving browsers.

Need to know what is Mid([GTIN],2,10) in MySQL

I've been given a query to use in some PHP code but the problem is, the person that gave me the query is using MS Access and I'm using MySQL so everything isn't quite translating. In his query there is this
... Mid([GTIN],2,10) AS items_bar ...
MySQL doesn't like this part and I'm not sure what it is supposed to be doing. Is there an easy translation for this to MySQL?
It is selecting a sub-string of the GTIN element.
Instead of using this:
Mid([GTIN],2,10) AS items_bar
Use this:
substring(GTIN,2,10) as items_bar

Compare strings and generate a suggestion in PHP

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/

Exporting/Downloading CSV on the fly using PHP/MYSQL

I am wondering what is the best way to tackle the following problem. I need to provide a client with the option to download various ranges of records from a database in CSV format. I was thinking of doing the following.
Provide user with a webform to determine what rows to pull from the DB.
Pull the appropriate records and create a large string from the return data.
Post the string to the script which creates the CSV for downloading.
Before I attempt the above I was wondering if there is a better way to do this?
Thanks ^_^
You can use MySQL's SELECT ... INTO OUTFILE to immediately obtain your results in a CSV. You then need only read the contents of that CSV file back to the user.
This is much safer than attempting to construct a CSV yourself (where you must consider how to properly escape field delimiters, etc); however, if you wanted to do it that way, I'd recommend using fputcsv(STDOUT, $results) to avoid common pitfalls.

How can I store PHP code inside of a mysql table

I am working on building a small php/mysql script that will act something like a wordpress blog but will just be a small site for my eyes only to store PHP code snippets. So I will have categories and then pages with sample code that I write with a javascript syntax highlighter. Instead of storing my php code snippets in the file I am wanting to save them to mysql DB. So what is the best way to save PHP into mysql and to get it out of mysql to show on the page?
My end result will be something like this
alt text http://img2.pict.com/c1/c4/69/2516419/0/800/screenshot2b193.png
Update:
I just wasn't sure if I needed to do something special to the code before sending it to mysql since it has all different kinds of characters in it
Just store in a text field, as is. Not much more beyond that.
If you're not using some kind of database abstraction layer, just call mysql_real_escape_string on the text.
Do you want to be able to search the php code? If so, I recommend using the MyISAM table type as it supports full text indexes (InnoDB does not). Your choices for column type when it comes to a fulltext index are char, varchar and text. I would go with text as your code snippets might get too long for the other types.
Another point worth mentioning, is make sure you properly escape all php code (or any value for that matter) before you insert it. The best way to do this is by using parameterized queries.
Unless I'm missing part of the problem, you should be safe storing it as a TEXT field in a MySQL database. Just make absolutely sure you sanitize the code snippets, as PHP code in particular is quite likely to contain the characters that will escape out of an SQL string. (If you're already using an SQL framework, odds are the framework is doing this for you.)
Store as text (varchar) in the database.
Use cascading style sheet (css) to format code.
http://qbnz.com/highlighter/
Try this:
mysql select ...
eval('?>' . $row['phpcode'] . '<?php ');

Categories