Codeigniter doing translations from the database - php

I have been looking around to see if I can learn how to do translations that come from a database.
My aim is to have an admin do the translations for each label in an admin console and when a user selects a language, the site will reload and it will load the translated options from the db instead of having multiple view files in different languages.
So far what I have is adding an extra segment in my url to accommodate different languages ex. www.testwebsite.com/Application/en-US/content
I plan on reading that segment, storing it into a cookie or session and when a new page loads, it retrieves that option from the cookie or session and always loads the right language.
As for the db structure, so far I have
id | label | language | translation
The main issue now is how do I load this properly? My experience so far has taught me that when I want to load a bunch of items from the database, it will come in an array form and in order to access it, I must add a for loop/ foreach loop.
Is there a way to replace the labels without them being in a loop? Or is there a better way to do what I am trying to achieve?

Related

How to efficiently make many (100+) HTML pages with minor differences

I'm trying to make 100+ registration pages for my site with minor differences between each one.
For example:
State, Age, Start Date, Client Goal
Page 1 characteristics: California, Age 18-25, Start Date Dec 1, Goal 1
Page 2 characteristics: New York, Age 25-30, Start Date Dec 15, Goal 3
Page 3 characteristics: New Jersey, Age 18-25, Start Date Jan 1, Goal 2
Goal:
It's important that I have separate pages and not just a form or something selected by the user on a single page. Ultimately I'd like to have many more levels of customization than what I have above and would like to control all the pages without having to go in and edit each individually.
Suggested Method:
I was thinking I could make 1 PHP template and use it on every page to pull a unique code from the href to determine what data it gets from the database to display on the page.
To get my page 1 it would be something like:
window.location.href
returns -> mysite.com/CA-18-12-1-G1
Then use that code to set PHP variables which would customize the entire page just by echoing the html with the variables.
Is there a better/smarter/safer way?
I'm fairly new to this.
Think of it this way: you want to make just one page, and you want to render it with 100+ different customizations.
Figure out the common HTML and CSS for your template page, and figure out what needs to change customization-by-customization. You'll have a set of customization parameters when you finish this design work.
Then, design some kind of data structure to hold your customization parameters. A table in MySql with one column for each parameter and one row for each of your 100 customizations might be a good way to do this.
Then write a php program to retrieve the row with the parameters for the customization you want, fill in your common HTML with the customizations, and send it to the browser for your user to fill in.
If you only need two or three custom pages, my advice would be to program them separately. But with 100 you need a template / parameter approach.
For writing a single page and then using the code of that page in multiple pages you can use PHP.
Lets say you have a page named constant.phpand you want to use the code of the page in a different PHP page named home.php. You have to use this code in the home.php where you want the code to be inserted:
<?php include "constant.php";?>
Now the code ofconstant.php code will get inserted in the home.php in the place where you wrote this code.

Passing Variables between PHP pages then performing a MySQL search

fairly new to PHP and webdesign.... I have a website which has a bunch of products for vehicles - all of this is stored in a database.
In order to narrow down what products are available, I want to take the user through 2-3 pages where they firstly select what categorey of product they want, then what make of car they have, then the model, then I want to run a mysql query with that information and return the answerson a page.
I already have the code to request the info from a database and then display it on the page, but The way the current website is setup, they are hard coded mysql queries (meaning the user didnt input any data to get there).
So how can I transfer variable between php pages? Page 1 (contains Categorey) - then pass to Page 2 (contains Make) then page to Page 3 (contains Model) then compile the three variavles collections, and pass them to results.php (a standard results page which Gets variables and then searches).
I know this might seem basic, but I am really stumped as to how to get it coded.
If someone can give me an a newbie explanation about how to achieve this? Thank you
You can pass PHP variables through URLs for example
Your HTML code,
<a href='yourPage.php?yourVariable=someValue'>TEST!</a>
Your PHP code,
if (isset($_GET['yourVariable'])) {
$yourVariable = $_GET['yourVariable'];
// Your code here.
}
Now using this you can create what you want with a little bit of tinkering.
Remember
You need to use $_GET because parameters are being passed using the URL and not a POST request.
Don't forget to clean all input.

How to keep results filtered by selectbox even after page refresh

I'am using the CodeIgniter. I have a global category list, which is separated into the several components such as (Conten articles, E-commerce, Users, Banners, etc...). There is /categories/get_categories page where I display all rows from Database Table ci_categories.
On that page, there is a <select> box with the <options> of available ci_categories.com_id (components). Whenever I select one of them, either Users or E-commerce it will send the POST data /categories/get_categories/$com_id and it will filter the category items corresponding to the component's id com_id. All of this works great.
But what I want is to keep this filter selected and do not return to default selection (all components). For example, I click on Categories - it will output all category rows in a <table>. There I choose an option from a dropdown selectbox list, and it will filter the specific rows output, then i Click Add new category, and it should pass that selected component option to the next page. On the next FORM page, where I add a new item informations, i Click on SAVE, and it should return me to the previous page where All of categories are listed, but with that filter com_id selected.
Any suggestion ? Is there a way to do it without sessions or cookies ?
My suggestion is to make use of sessions as they are especially meant to store data between requests based on the current user experience. Most people only search the website in one browser window, so it shouldn't give a real problem. Even if there are other options, you always need to identify the user by a specific code which will be stored in session/cookie.
The only option I can think of is changing every link afterwards to contain the stored information in a base64 encoded string. But that would ruin your link structure and needs canonical links on every page to show the right url to search engines.
I would suggest you to stick to Sessions, as they are handled fine in CodeIgniter. You can even store them in the database if you want by setting that in the main config file.
Flashdata is an option, but it's a session in the end anyway but only lives one request.
you can use session flashdata like this:
Set flashdata
$this->session->set_flashdata('search','your_search');
Read flashdata
$this->session->flashdata('search');
flashdata is setted for only one refresh, after the refresh the variable is deleted
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

CakePHP - generating different pages/views dynamically using a menu

I'm a total beginner to CakePHP, and I'm trying to build a pretty basic website here that has a menu + some UI stuff like a footer that will persist through all the pages.
The trouble that I'm having right now is that in my controller, I have several different functions corresponding to the website's menu options that grabs data from database, but obviously I wouldn't want to create the same number of views since every page has the exact same setup.
So my question is what's the standard or best practice to achieve this? I read up on elements and am still kinda confused as to how this would be done; how would I keep track of which page is the user currently browsing? And if they click on a menu option, how would it be coded so it takes them from "blah.com/home" to "blah.com/contact"?
I know my question is kinda long and noobish but I'd really appreciate it if I could get some help in beginning CakePHP.
For functions corresponding to the website's menu options that grabs data from database, put these in beforefilter() of the App Controller and use $this->set to set variables and make element for menu which you can call in your layout. You can set the layout in your controllers like var $layout=''.

How do I display varying pieces of data through a fixed template without making multiple slightly altered 'include' files?

I have a database of different stores.
When a user clicks on a store name, I want an Ajax function to run displaying information about the store in a different div.
Information categories for all stores wll be the same: products carried, location, general information, etc.
I could easily make it so that each different store uses a different file name as an argument to the ajax function, and all files would have the same layout/format but with different data.
However i feel like this is bad form. How can i make it so that i have one fixed template and all that changes is the information specifics imputed into the template?
Please note that the store information display pages will also need to be able to have clickable links of their own (i.e. click on location and a google map pops up).
Is it something to do with XML? I dont know much about it.
Instead of returning a template, return the data.
So it says getstore.php?id=2 which returns a json string
{"name":"my store", "info" :"blah"}
Then you use java script to insert a new div, populated with that data.

Categories