i am confused what file extension should i use - php

Like for example, i have a page that has a form that asks the user to enter their name and last name and that form will be directed to a php file but at the same time, that page(form) has a mysql code because it shows the current names of the users that were recently added so will my file be saved as .php or .html?

Ideally, you would separate your server side scripting from your client side pages, calling them though either includes on an intermediary rendering php page, or ajax on a static html page.
a quick rule of thumb: If it has logic, interacts with backend resources or performs a calculative function, then it's php, if it provides structure to the output then it's html and if it provides blueprint for the presentation of the layout it's css.

Of course php!
if you are using php your pages must be in php otherwise your code will not get rendered.
you fill up the gaps using:
e.g: <span><?php echo $yourVariable; ?></span>
$yourVariable is either extracted from Database or has been defined

Related

How to properly communicate a parent PHP with embeeded PHP?

You may remember that not long ago I asked you for some help with objects transfering through $_SESSION in PHP (more: Data loss during sending via $_SESSION from one script to another ). Currently I'm reworking the whole project and I don't have a foggiest idea how to do it properly. I mean, how to do it "according to the art", not "at any cost / the easiest (but nasty) way".
Here's the mockup of what I want to achieve (animated gif):
As you can see, I want my website to display at startup only the navigation, in which I want user to set his starting parameters. In the future, the navigation bar will have ability to be collapsed (to extend the data display section's height). When user sets the parameters (or not - for default view) and clicks "FILTER AND SORT" (which probably will be renamed to "GENERATE"), the second section should appear with data filtered and sorted as user defined.
Data is read from CSV file and stored in PHP Objects defined by my custom classes I've shown you in the question linked above. So I need to communicate data between 5 files:
FRONTEND:
index.php <-- my main file handling the website
FRONTEND-BACKEND:
navigation.php <-- file that shall be displayed in the navigation div
data.php <-- file that shall be displayed in the data section div / iframe (?)
popup.php <-- file that appears in a div over the index.php when certain DataBox is clicked; handled with tinybox2.
BACKEND:
classes.php <-- file with definitions of classes (properties and methods)
tinybox2 "library".
My communication flow is as to be:
navigation.php displayed on the top of index.php sends filtering and sorting parameters to data.php, making it appear on the bottom of index.php.
data.php and popup.php shall use classes contained in classes.php constructed from CSV file datafile.csv.
When a DataBox is clicked in data.php, popup.php appears over index.php content to display extended object data.
So, how should I do this, to make it properly? Which option will be best: $_POST, $_SESSION or maybe something else? Maybe due to embeeding all files in / over index.php it is possible to store all data in such a way, that no communication is needed? How to embeed files - with include, require or maybe other way?
There are lots of assisting questions, but still, the most important is: how to do it properly? Thank you for your help in advance :)
PS For certainty, please find below one additional usecases:
UC1: Standard use of display system
User enters website with the system
User chooses parameters for filtering and sorting
User starts generating view
Data is being read from *.csv file
During above, data is being filtered and sorted
Data is being displayed by the system to the user
Extensions:
5a. No data to display: system displays empty data section
6a. User want's to filter and sort again: back to step 2.
UC2 (OPTIONAL): User wants to share the data
UC1
User chooses an option to share data
System displays question if user wants to send it to printer, or e-mail
User chooses option (for this case: e-mail)
System asks for e-mail address
User provides e-mail address
System sends an e-mail with the current data.php data as it is displayed on website.
PPS I know I should show you some code snippets, but my current code is a mess with lots of interchanging html, php and comments, that cannot be cut of from the system (or would take me ages to clean it up to show it here). I'm asking you for help mostly, because I really want to remodel the current solution, therefore I'm rewriting the code from zero, using old one as a hint, not a template. Like Microsoft did with Windows Vista and Windows 7 :P Hope you'd understand ^^'
Okay, so here it is. The ultimate answer to a question some understands as too wide.
What was the case:
I wanted to achieve a two-section page, where the first (partent) section takes user input and uses it to filter the data in the second section. The problem was with providing parameters from the parent side to the invoked one.
What I had to change in my conception:
At first I thought about passing data with $_SESSION or other PHP way, BUT there is one major problem with such way of thinking:
As PHP is server side interpreted, it cannot dynamically add another page without refresh!
I know I didn't intent a new lightbulb or explore America, but it's still this conclusion what leaded me to accept the fact, that JavaScript usage is inevitable. I think most beginners will find this important: you need to completely change your way of thinking, as website ain't the same thing as traditional application. After this, it finally came to me (thanks to #Yoshi) that my initial problems with jQuery weren't a good reason to hold back from using it and I had to find out what they were.
Solution comes here:
Okay, so what is the solution?
I've started up with finding out what was wrong with my jQuery and... it was wrong way of interpreting "what is the scope of code included into php". If I include view/header.htm file into index.php that lays in the root directory, it will behave as it was in the root directory, not view directory. And that was the stupidest, as well as not uncommon error that one can make.
After finding this out (thank you #Yoshi again!):
I've created backend (model) for my app that can:
Read data from file
Put data into a class-based storage (that will in the future be then exported into a php file as "pseudo-database")
Then I've created a kind-of controller, that can use the model to pass data to a variable in my view.
At least I've created a view for my site, with the "parent site" (it has a backend part with a main-controller, and a frontend part with a view), and a "invoked site" that is opened through jQuery request, and contains a filtering backend and the main data view.
These 3 not-so-easy at first, but now so-so-essential, steps took me here. Please find some php-pseudocode below:
model_Data.php
<?php
// Class for single instance of data storage
model_DataSet.php
<?php
// Class for set of data instances storage
model_ReadData.php
<?php
// Code for reading CSV file with fgetcsv function
controller_DataMaster.php
<?php
require_once 'model_Data.php';
require_once 'model_DataSet.php';
require_once 'model_ReadData.php';
// Read data from file to a variable
$reader = new ReadData();
$reader->setConfiguration(//some setup);
$tmp_data = $reader->read("filename");
// Put data into the class
$dataSet = DataSet($tmp_data);
return $dataSet;
index.php (which is the main controller!)
<?php
header('Content-Type: text/html; charset="UTF-8"');
include 'view/header.htm';
header.htm contains HTML headers, includes of jQuery and jQuery-UI (for sake of datepicker) as well as css references, and (of course) physical header of the website with the input form displayed.
view.php
<?php
$import = include 'controller_DataMaster.php';
$usableData = $import->getAllDataSingles();
if (isset($_POST['data_from_the_form']))
{
// Do something!!!
}
?>
<table>
<tr>
<?php $i = 1; foreach($usableData as $singleData): ?>
<td><pre><?php print_r($singleData->getAllProperties()); ?></pre></td>
<?php if ($i++ % 3 == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endforeach; ?>
</tr>
</table>
And this way you can achieve something that looks like this (currently - without any CSS applied):
So here it is. An easy, not very long, not very short answer to my own question. Case closed :)

Drupal 7: Database Query file

In page.tpl.php of Drupal, the following code has variable $page which contains the html codes that are required to render part of the webpage.
<?php if ($page['content']): ?>
I'm trying to retrieve the updated content from DB at an interval and update a DIV without refreshing the whole page. My question is, where can I find the DB query codes from which $page came about?
page['content'] variable
When Drupal displays the "content" variable, think of it as big array of data that will be rendered in a specific section of the page.
Before that happens, it either retrieves data from the block settings of the native interface (block visibility settings) or from custom modules that override this original settings like f.e. context (https://www.drupal.org/project/context).
>> Template files
The template files are kinda like the last stop where data is built and ready to be rendered and delivered to the client.
In general, the best practice is keep the render templates intact and keep the logic and variables manipulation at the pre_hook levels .
>> So if you do wanna have "programmatic" control over what you can display you can f.e.:
Render specific nodes with "node_load" function and then using node_view($node)
f.e.
$node = node_load(23); //23 is random a node id
if (isset($node)) {
$node_data = node_view($node,'default''); // here's my node display data
print drupal_render($node_data); // here's my html
}
Render templates that aggregate different data with the "theme_render_template"
https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_render_template/7
Render views by using views_get_view('view name') like stated before.
Hooks like these might help you in adding/modifying the content you wish
template_preprocess : (hook templates
hook_node_view : (alter node array data before render)
page_alter : (alters page variables like content, header and so on)
hook_views_pre_render : (views is built, can alter render process)
hook_panels_alter : (for page manager and ctools approach)
>> Database queries
Doing a straight query to the database can also be done through the database api and then rendered, but be aware you might miss out the drupal hook power and end up having high maintenance code that escapes the convenient and safer Drupal way.
Hope it helps!
That content comes from inner node templates (if you are displaying node on that page) or i.e. view template....or...depending on what page you are displaying. But if we assume that you are displaying node page in some content type "player" you created content will "come" from template file "node--player.tpl.php"
https://www.drupal.org/node/1089656
Inside that file you can do database query (by using Drupal's database api) or even easier use Drupal's views module to query database:
https://www.drupal.org/project/views
What you are trying to do :
"I'm trying to retrieve the updated content from DB at an interval and update a > DIV without refreshing the whole page"
, is usually done with Javascript sending Http requests from the browser and not from PHP code executing server side in a Drupal template file, and is named AJAX. Doing AJAX with Drupal could be done in multiple ways, one of the recommended ones is with the use of the "Drupal AJAX Framework", resulting in PHP code with "Drupal AJAX API" calls server side that will generate for you the appropriate JavaScript client code renderer in the Drupal's output. The Drupal "Views" module may also help in this task, being "aware" of the Drupal AJAX framework. A good understanding of general AJAX mechanisms and of the Drupal AJAX framework are required to do this properly.

My php/jquery/ajax design hierarchy isnt working

I currently have a site I built using jquery/php/PDO/mysql.
I use classes for most functions, including database, logins, content, etc...
I am wanting to change my forms over to jquery's ajax calls. But there's where my problems start. With the ajax call, I can't call a php function in the ajax post url. Heres the heirarchy Im using;
->content.php (form resides in a function named content.)
->process.php (post checks and then a call to add content from class)
->class.content.php (insert vars into db)
Once form is submitted, it goes to process.php which contains checks and then a class call to add content.
While this hierarchy seems to be the most used for ajax, it causes path issues. It breaks my db connection, my config connection, etc...
All I really want is to add ajax forms. But I don't want to rewrite my whole site. Any suggestions?
It sounds like process.php was previously included in content.php but now you're trying to submit to it directly. However, process.php was dependant upon the configuration variables, database connection etc. which was included in content.php and accessible by being included within that page.
I think you need to make a new page which includes the same other files and has the same initialisation code as content.php, except it doesn't output the form and instead just includes process.php, then submit to the new page.
Hard to say for sure without code though.

Creating Dynamic form in android

Has anyone faced this feature request and were able to resolve it? We have a webapps that is capable of creating a form as a template. That template will basically be called and user will need to fill out the form before it got push into the db. So the form is created directly inside the application and the fields, labels and variables are all defined when the form is created by user using the apps. Since the form is always going to be changing, I can't hardcode the activity in android and have to create it manually and recompile every time a new form is created. Is there a way for us to read the label, variables setting that's stored in db either in XML format or called as JSON and build the form dynamically everytime the form is called via android? Am I making any sense? Please advise?
Yes. Everything you do in XML (view creation, positioning etc) can be done dynamically via code as well.
A simple way would to be to put a single ScrollView with a single LinearLayout inside it. Then in your activities onCreate(), you can read your JSON or XML file just like any other file (you can store this is assets folder or maybe query it from your backend). Then depending on your variables you can initialize and add TextViews and EditText's to the LinearLayout. The ScrollView will expand infinitely to accommodate all your form elements.
Just make sure you don't do any long-running operation such as querying from your backend or reading from your file in the main UI thread. Another caveat is that if ScrollView does not recycle views and putting too many views in it (say more than 20) can make your application run out of memory and slow down/crash.
You can create a form in relative layout having all the fields/view you require in XML, then on Runtime in code According to your label name in db or whatever you are using, you can hide/show the fields/view which are needed dynamically. This way you can preserve the position and setting of each field as when 1 view is hidden other views are going to take its position.

Dynamically Generating Javascript Instructions Through PHP with Magento

Goal
I would like to be able to dynamically create javascript and serve it based on preset configuration from the application. The url of the javascript resource can remain the same. The idea is to make available the option to change javascript parameters depening on backend configuration.
An example of this:
The administrator has a set of classes that when clicked upon open a modal pane for the end user. lets say that those classes follow the format of *_modal where the * portion of the class will indicate a portion of the url where the content of the modal pane will be pulled from via ajax.
For example:
click me
would then trigger an ajax event on click where the contents from #contents would be pulled from http://www.myurl.com/modal/orange and displayed in the div #modal_output at the client side.
lets say for some odd reason the administrator for the site decided that he wanted to change the id of the display modal pane for some reason to #modal_output_view . Normally this would require some modification of the javascript and possibly the static output of the page.
I would like to forgo that and offer a configuration option whereas the administrator can choose what the div id would be for the modal output.
At run-time the resource would be requested, php would grab the configuration value for that div's id and return a string in the form of a javascript resource.
Secondly I will be using magento, is there a good way to improve the performance via cache?
Is there any way to use a cdn?
Questions
1) I know how to do the configurational portion, asside from doing this within an appended block or inside of a template file:
<script type="text/javascript">
(function(){
configuration_value = <?= Mage::getStoreConfig('my/config/value');?>;
})();
</script>
is there another easier way to interface between magento's configuration values and client side javascript?
2) Will this work correctly? are there any issues I might face? The key being the .php extension
3) Are there existing core javascript classes/methods that may be useful? Any documentation?

Categories