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.
Related
I am having a big database roughly it has 5 lakh (500K) entries now all those entries also have some document associated with them (i.e. every id has at least pdf file). Now I need a robust method to search for a particular text in those pdf files and if I find it, it should return the respective 'id'
kindly share some fast and optimized ways to search text in a pdf using PHP. Any idea will be appreciated.
note: Changing the pdf to text and then searching is not what I am looking for obviously, it will take a longer time.
In one line I need the best way to search for text in pdf using PHP
If this is a one-time task, there is probably no 'fast' solution.
If this is a recurring task,
Extract the text via some tool. (Sorry, I don't know of a tool.)
Store that text in a database table.
Apply a FULLTEXT index to that table.
Now the searching will be fast.
I myself wrote a website in ReactJS to search for info in PDF files (indexed books), which I indexed using Apache SOLR search engine.
What I did in React is, in essence:
queryValue = "(" + queryValueTerms.join(" OR ") + ")"
let query = "http://localhost:8983/solr/richText/select?q="
let queryElements = []
if(searchValue){
queryElements.push("text:" + queryValue)
}
...
fetch(query)
.then(res => res.json())
.then((result) =>{
setSearchResults(prepareResults(result.response.docs, result.highlighting))
setTotal(result.response.numFound)
setHasContent(result.response.numFound > 0)
})
Which results in a HTTP call:
http://localhost:8983/solr/richText/select?q=text:(chocolate%20OR%20cake)
Since this is ReactJS and just parts of code, it is of little value to you in terms of PHP, but I just wanted to demonstrate what the approach was. I guess you'd be using Curl or whatever.
Indexing itself I did in a separate service, using SolrJ, i.e. I wrote a rather small Java program that utilizes SOLR's own SolrJ library to add PDF files to SOLR index.
If you opt for indexing using Java and SolrJ (was the easiest option for me, and I didn't do Java in years previously), here are some useful resources and examples, which I collected following extensive search for my own purposes:
https://solr.apache.org/guide/8_5/using-solrj.html#using-solrj
I basically copied what's here:
https://lucidworks.com/post/indexing-with-solrj/
and tweaked it for my needs.
Tip: Since I was very rusty with Java, instead of setting classpaths etc, quick solution for me was to just copy ALL libraries from SOLR's solrj folder, to my Java project. And possibly some other libraries. May be ugly, but did the job for me.
How to store chemical formula like (C8H9NO2) in database in php.
then display chemical formula like (C8H9NO2) in php
enter image description here
I just tested it locally, and I think this is what you're looking for:
preg_replace('/(\d+)/i', '<sup>$1</sup>', 'C8H9NO2')
// returns "C<sup>8</sup>H<sup>9</sup>NO<sup>2</sup>"
You can store this HTML, which will render each number as superscripts. If you want them as subscripts, replace sup with sub.
As for storing it to the database, I have to be honest and StackOverflow isn't exactly a tutorial website, but you can create a VARCHAR or TEXT column on a database and just store it there. I think it's best to format the text upon saving so that you don't have to do it every time you render a page for each formula you have. However, that depends on you, maybe you need the raw text. I guess it's fine either way.
EDIT
After seeing the picture again, I noticed that it is different than shown in the question, so it is important to pick sup or sub accordingly. As it is shown in the question, it should be sup; but, according to the example image, you are asking for sub. Therefore:
preg_replace('/(\d+)/i', '<sub>$1</sub>', 'C8H9NO2')
// returns "C<sub>8</sub>H<sub>9</sub>NO<sub>2</sub>"
In your code, this would end up being as follows:
<b>Molecular Formula by Element:</b> <?=preg_replace('/(\d+)/i', '<sub>$1</sub>', $row['mole_for_element'])?><br>
In my iOS app I get data from an external PHP script which builds and returns strings using queries on a mySQL database. In this database, texts have HTML entities in them, e.g. Josè is written as
Josè
When I pass these built strings to my app, all the entities are still there but I'd like to transform them into human readable text in my app. I can't find a way to do this.
I saw questions like this one with accepted answers like this but I can't write a line for any of the hundreds of entities that exist. I mean, I could, but I can't believe there is not a way to do this in a more simple way.
Also, since I use said strings in many places from many views through all the app (text views, labels, table view cells, etc) I think it would VERY useful to apply the correct transformation in the PHP script itself, rather than in the app. So my final question is this: which is the correct way to build a string with entities in it so when I load it in my iOS app all the entities are readable characters? Thank you to ANYONE who will help me!
You can use the Google's category GTMNSString+HTML
This category possibly covers most of the HTML entities you might want to convert into human readable format.
Since I'm not able to find the Google code for this category, I'm pointing to an alternative location; MWFeedParser.
GTMNSString+HTML.h
GTMNSString+HTML.m
I have an HTML table with contents, I would like to have an feature of Edit/Delete to that table. How do I do it with PHP?
I actually think that this sounds more like a job for JavaScript, which can edit/remove rows on-the-fly and with much less code. (Implement some AJAX too, and you can edit/remove rows in database too).
But if you insist on using PHP, you might just want to add some GET parameters to the Edit/Delete links that would delete or edit those rows.
Well, there is a pure PHP way to do it, and then there is a combination of Javascript and PHP. You must use PHP one way or another if you want your changes to the database to be permanent as that is your gateway to communicating with the database (as far as I know you cannot do that with Javascript as that is client-based and runs entirely in your web browser).
If using just PHP, you must generate HTML documents for each change. E.g., you click on one cell in the table and that gets you to a new HTML page where the field is editable through an input element; or you can list all fields at once for that row and edit them all at the same time. The fields are then posted in a form to a PHP page which will take the new values and update the database (or insert new values or however you wish it to behave). Here's a tutorial for how to do this:
http://www.freewebmasterhelp.com/tutorials/phpmysql/1
You can also mix in some Javascript which allows a more interactive interface to modifying the values in a cell. However, this obviously requires more code and may be overkill for what you're trying to do. Nonetheless, here is a link which demonstrates just that and also shows the code:
http://www.java2s.com/Code/JavaScript/GUI-Components/Editabletablecell.htm
Hope this is what you're looking for.
EDIT:
Forgot that you also wished to delete content in the table. That is also explained in the first link.
If you intend to work with databases, and it seems like you have little understanding of how they work, pick up a good book like: SQL - The Complete Reference. When you have enough knowledge of SQL, look at PHP's PDO extension: http://php.net/manual/en/book.pdo.php
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 ');