Creating and App using PHP, Mysql, and jquery and i am stuck - php

I would like to create an app that writes a basic music chord chart to a database and pulls it out onto a different page.
I know the basic to intermediate concepts of inserting and returning data to a database but I am having trouble on this one.
In a textarea I would like the member to be able to insert into the database like so...
{C}I`ve {F}been {G}though {A}the {C}desert
The Letters inside the brackets being the chords and the words outside of the brackets being the lyrics.
This is not the problem though, I know how to do that.
The problem is, I would like the {Chords} to be placed directly above the letter they are preceding while also giving them a class that would allow me to change the font color, size, and weight when rendered on page.
I have been thinking that there is a way to do this with tables, str_replace, and strTok but I cannot figure it out.
Any suggestions?

use regular expression to extract content of {} into an array
str_replace {} to blank (space), in a way you will have plain text except chords letter
use html css, put Chords letters in span tag.change css of span, so it position above the letter. i.e. span position is absolute and parent container position to relative.
if you want to save those chords into table , you can as its already in array
Hope this helps, best of luck

Working with HTML tables can be a real pain. Here's how you could do the HTML part of it without using tables:
<style>
.phrase { display: inline-block; }
.chord { font-size: 20px; }
.lyrics { font-size: 12px; margin-right: 1em; }
</style>
<div class="phrase">
<span class="chord">C</span><br>
<span class="lyrics">I've</span>
</div>
And with a little PHP mixed in:
<?php
$phrases = array(
array('chord' => 'C', 'lyrics' => "I've"),
array('chord' => 'F', 'lyrics' => 'been'),
);
?>
<?php foreach($phrases as $phrase): ?>
<div class="phrase">
<span class="chord"><?php echo $phrase['chord']; ?></span><br>
<span class="lyrics"><?php echo $phrase['lyrics']; ?></span>
</div>
<?php endforeach; ?>
As far as saving/retrieving from the database. You could do some fancy regular expression/string replace thing, but that's probably more work than you really need to do. Just keep it simple and create a structure like this:
Songs Example
--------- -----------
id (PK) 1234
song_title I've been through the desert
Phrases
-----------
id (PK) 1
song_id (FK) 1234
chord C
lyrics I've
order 1
You'll have a lot more rows in your Phrases table, but it will be a lot easier to work with.
Also, as a final, biased, piece of advice: check into Ruby on Rails. I started out learning PHP and wished I would have learned Ruby on Rails earlier in my career. There are a lot of ways that Ruby on Rails makes it easy for a beginner to get started programming.

Related

Formatting fraction fir display

I'm formatting fraction with MathJax and are having problem displaying it properly.
$disp = '<h1>$${{10 \over 9 }} of 99 $$</h1><br>';
echo $disp;
For some reason, i cannot get a space before and after the word 'of'. Any pointers is greatly appreciated. Thanx in advance.
This is better handled as
$disp = '<h1>$${10 \over 9}\text{ of }99$$</h1><br>';
as the accepted answer does not get the font or spacing for "of" correct.
It also seems that you may be using <H1> simply to get a larger size. If so, that is bad practice, as <H1> is a structural element indicating a top-level heading (not a layout element for a larger size). Unless this expression really is a top-level heading, you should not use <H1> for it. For example, people using assistive technology like screen readers often are given a list of the headings so they can quickly jump to the important starting points of your page, so if you make all your expressions be headings, that will complicate their already difficult task of navigating your page.
Layout should be controlled by CSS, so you could use a <div> with a class around your display math if you want to size it. Or you could use one of the TeX macros like \Large or \LARGE to make the math larger from within the expression. But don't use a heading indicator unless it really is the start of a new section of your page.
Here are some examples:
.dmath {
font-size: 200%;
}
<script src="https://cdn.jsdelivr.net/npm/mathjax#3/es5/tex-chtml.js"></script>
Bad:
<h1>$${10 \over 9} of 9$$</h1>
Better using CSS and <code>\text{}</code>:
<div class="dmath">
$${10 \over 9}\text{ of }9$$
</div>
Better using <code>\LARGE</code> and <code>\text{}</code>:
$$\LARGE {10 \over 9}\text{ of }9$$
<br><br><br><br>
Usually, \ keep the space between letters.
$disp = '<h1>$${{10 \over 9 }}\ of\ 99 $$</h1><br>';
Reference - Spacing in math mode

How to style the output of an echo to display a grid?

My team and I have made a database in php my admin for a restaurant, and I'm currently working on the customer dashboard. Im using for each loops to display complete orders in one of the dashboard tabs, and have the code working, but right now it just outputs regular black text. I was wondering how to style it to output the rows as a grid, similar to bootstrap grids.
I've tried to just add containers with rows and columns to the foreach echo itself, but its just not working as I thought it would.
<div id="CurrentOrders" class="tabcontent" style="margin-left: 24px">
<!-- This information will be pulled from the Orders table in the DB -->
<h3>Current Orders</h3>
<p>
<div class="container">
<?php
foreach ($orderno as $order) {
$n = $order['OrderNo'];
$menunamequery = "SELECT * FROM OrderItem WHERE OrderNo = '{$n}'";
$currentorders = getRows($menunamequery);
foreach ($currentorders as $currentorder) {
echo "Order Number -"." ".$currentorder['OrderNo']." , "."Order -"." ".$currentorder['MenuName']." , "."Quantity -"." ".$currentorder['Quantity']."<br>";
}
}
?> </div>
</p>
</div>
The expected result is for these rows im outputting to have some sort of grid layout, the actual result is just plaintext currently.
Sorry if this is a bad question, my team and I just learned php this semester and are hoping to continue getting better at it. Any help would be appreciated.
You can simply output HTML from PHP:
echo '<span style="color: red">'.$currentorder['MenuName'].'</span>';
However, it is advised that you sanitize your output, so nobody can "create HTML" by putting tags in the database;
echo '<span style="color: red">'.htmlspecialchars($currentorder['MenuName']).'</span>';
This does exactly what it says; makes HTML entities from special characters. For example, > will be printed as >, which the browser will safely render as >, instead of trying to interpret it as an HTML element closing bracket.
Alternatively, you can simply write HTML directly if you wish, by closing and opening the PHP tags:
// PHP Code
?>
<span class="some-class"><?=htmlspecialchars($currentorder['MenuName'])?></span>
<?php
// More PHP Code
You may also want to look into templating engines to make it easier for you, although it depends on the project if it's worth it for you to look into that, since there is a little bit of a learning curve to it.

Generating an HTML color code for event category in ai1ec

Although this question relates to a particular Wordpress plugin called the All in One Event Calender by Time.ly it can also be a general PHP related question.
I am trying to modify a theme to use event colours selected in the ai1ec back end and would like to produce a simple HTML colour code - ie "#f2f2f2"
The plugin itself has loads of functions and php shortcodes to pull a wealth of information off each event such as the one listed below.
<?php echo $event->get_category_text_color(); ?> which will print style="color: #f2a011;"
Can also change to print style="background-color: #f2a011;" with the use of $event->get_category_bg_color();
Now the real meat of the question
All I want to do is get that HTML colour so I can also code it into buttons and other visual elements. I have scoured through blogs and code to try and find something that does it to no avail.
What I was wondering is if you could write a filter of some sort to just take the information within the "#f2f2f2" quotation marks. I know it's not called a filter as searches for php filter return information about something completely different - I'm a self taught PHP programmer and so searching for a lot of terms I don't know can be pretty tough!
As pointed above, substr would be a great solution but it wouldn't solve the case where the color code is expressed in this format:
#FFF;
instead of:
#FFFFFF;
Therefore, this regex should do the job quite well:
'/?=#(.*(?=;)))/'
Example:
$matches = array();
preg_match('/?=#(.*(?=;)))/', $event->get_category_text_color(), $matches);
$colorCode = "#{$matches[0]};";
You could use the substr() function like so:
echo substr($event->get_category_text_color(),14,-2);
Which in the example, would return #f2f2f2.

Parse Text field with custom Tags and convert to HTML

I have been struggling with this for hours.
Have tried searching online for a solution but everything I found wasn't exactly what I was looking for. Am desperate hope to get an answer.
I have a long string (mySQL text field) it contains text.
Dummy text sample :
There are many possible ways to minimize chances of cancer.
One such which has been proven beneficial is the use of anti oxidants
and various supplements.
[ARTICLE]
[TITLE]Green tea shows strong anti oxidant effects[/TITLE]
[DATE]Article published on May 2005[/DATE]
[BY]Department of Oncology research, University Hospital Denmark[/BY]
[TEXT]We test 54 subjects and given several vitmins,
other group received placebo. [[MARK]]We concluded that green
tea is an effective anti oxidant[[/MARK]]. We found Vitamin C to be
less effective.[/TEXT]
[/ARTICLE]
We also tested other supplements and also found interesting properties.
[ARTICLE]
[TITLE]Carrots ineffective for testicular cancer[/TITLE]
[DATE]Article published on October 2012[/DATE]
[BY]Oncology Journal, issue: 54[/BY]
[TEXT]Many people carrots are effective for several types of cancer.
In the research we did [[MARK]]we found that Carrots did lower
the cancer markers in test subjects[[/MARK]]. We cannot recommend
it as anti cancer.[/TEXT]
[/ARTICLE]
We will publish more research later on."
What I need to do is to parse that string and output it.
Notice that the string has two section (each starts with [ARTICLE] and ends with [/ARTICLE] and each of these sections have inner custom tags in them.
For every place that starts with [ARTICLE] and ends with [/ARTICLE] I don't output as is, I want instead to call my custom function to format it differently.
For example:
function format_text_with_articles (ArtTitle, ArtDate, ArtBy, ArtText){
// This is just a simple function I already have that gets
// the params and formats a table with special formatting inside that makes
// article extracts look nice.
}
So simply output all the text to the browser removing everthing between the tags [ARTICLE] and [/ARTICLE] (including the tags themselves of course) and these sections I out with special formatting that my function does.
Just notice that inside my custom tags TEXT tags I have special MARK example: tags[TEXT] blah blah blah [[MARK]]this is emphasized text[[/MARK]] [/TEXT]. For simplicity since MARK is the only tag that can be nested inside [TEXT] i put it as [[MARK]] (with double brackets]
How do I output all the text field as is, except for sections in between the [ARTICLE] tag and treat them as parameters send to a custom function ?
Help is much appreciated!
I think that what you are wanting to do is entirely possible. There are probably several ways to skin this cat, but this is how I'd go about it.
<?php
$full_article = '[ARTICLE]
[TITLE]Green tea shows strong anti oxidant effects[/TITLE]
[DATE]Article published on May 2005[/DATE]
[BY]Department of Oncology research, University Hospital Denmark[/BY]
[TEXT]We test 54 subjects and given several vitmins,
other group received placebo. [[MARK]]We concluded that green
tea is an effective anti oxidant[[/MARK]]. We found Vitamin C to be
less effective.[/TEXT]
[/ARTICLE]
We also tested other supplements and also found interesting properties.
[ARTICLE]
[TITLE]Carrots ineffective for testicular cancer[/TITLE]
[DATE]Article published on October 2012[/DATE]
[BY]Oncology Journal, issue: 54[/BY]
[TEXT]Many people carrots are effective for several types of cancer.
In the research we did [[MARK]]we found that Carrots did lower
the cancer markers in test subjects[[/MARK]]. We cannot recommend
it as anti cancer.[/TEXT]
[/ARTICLE]';
// MATCH ALL OF THE ARTICLE TAGS IN YOUR TEXT AND STORE EACH ONE INTO $matches
preg_match_all('~\[ARTICLE\](.*?)\[/ARTICLE\]~ms', $full_article, $matches);
// LOOP THROUGH EACH OF THE MATCHES, DO SOME FORMATTING AND REPLACE THE EXISTING CONTENT
for ($i = 0; $i < count($matches[1]); $i++) {
$article = $matches[1][$i]; // TEXT WE WILL OPERATE ON
$existing_article = $matches[0][$i]; // TEXT WE WILL BE REPLACING
// PULL OUT EACH OF THE FIELDS WE WANT TO PASS ALONG TO OUR FUNCTION
preg_match('~\[TITLE\](.*?)\[/TITLE\]~ms', $article, $match_article_title);
preg_match('~\[DATE\](.*?)\[/DATE\]~ms', $article, $match_article_date);
preg_match('~\[BY\](.*?)\[/BY\]~ms', $article, $match_article_by);
preg_match('~\[TEXT\](.*?)\[/TEXT\]~ms', $article, $match_article_text);
$article_title = $match_article_title[1];
$article_date = $match_article_date[1];
$article_by = $match_article_by[1];
$article_text = $match_article_text[1];
// SEND THE VARIABLES TO A FUNCTION TO FORMAT THE TEXT
// THIS IS WHAT WE WILL BE REPLACING THE EXISTING TEXT WITH
$replacement_text = format_text_with_articles ($article_title, $article_date, $article_by, $article_text);
// REPLACE THE EXISTING ARTICLE TEXT WITH OUR REPLACEMENT TEXT
$full_article = preg_replace('/'.preg_quote($existing_article, '/').'/', $replacement_text, $full_article);
}
// PRINT OUT THE FINISHED ARTICLE
print $full_article;
// THIS FUNCTION TAKES SOME PARAMS AND PRETTIES THEM UP FOR THE DANCE
function format_text_with_articles ($article_title, $article_date, $article_by, $article_text) {
// REPLACE THE 'MARK' BRACES WITH BOLD TAGS
$article_text = preg_replace('~\[\[MARK\]\](.*?)\[\[/MARK\]\]~ms', '<b>$1</b>', $article_text);
// RETURN THE FORMATTED TEXT BACK TO THE CALLING 'FOR' LOOP
return "<span style=\"font-family: VERDANA; font-size: 11px;\"><p style=\"font-weight: bold; margin-top: 20px; margin-bottom: 5px;\">".$article_title."</p><p style=\"color: blue; margin: 0px;\">".$article_date."</p><p style=\"color: orange; margin: 0px;\">By: ".$article_by."</p><p style=\"margin-top: 5px; margin-bottom: 20px;\">".$article_text."</p></SPAN>";
}

Workaround for floats in DOMPDF

DOMPDF does not support floats.
However I am listing many tables, and they are mainly key & value pairs. I would like 2 of these tables to appear side by side.
i.e. if I could use floats
HTML
<table id="stuff">
...
</table>
<table id="other-stuff">
...
</table>
CSS
table#stuff {
float: left;
}
table#other-stuff {
float: right;
}
What sort of workaround can I do to support this? Or is it impossible?
If anyone has any ideas, there is a place to test here.
It looks like it may be supported in the beta.
You can download that from the Google Code page.
Make a table with 2 cells (50% if needed). Align 1st to left and the 2nd to right.
In each cell you place your tables.
It should look like 2 floats.

Categories