create php copys with different variables inside - php

I have a database in MySQL with info of different places. I want to create a profile page in PHP for each one of those places but there is a lot of them (more than 5000).
So if i do that manually it'll take me a lifetime!. What I did is, created a template that is working just fine (is retrieving the data from the data base perfectly). The only problem is that I want a profile for all of the places and what I have to do now is to copy that template, go inside that copy and change a variable (ID) so it became the profile of another place. Is there any way to do this automatically (create the copy, go inside change the (ID), and save that with another name I.E. $nombre_lugar?
Thanks.

Don't set the variable inside the page,
set it as a query string
yourpage.php?place_id=x
and inside your page
$place_id = $_GET['place_id'];

Related

How can I extract the value of a variable and re-use that value until it is overwritten

I have a website which essentially acts as a blog / photo gallery. It comprises PHP and MYSQL.
My objective is to be able to set an image on my homeimage dynamically. I have set up a form which posts a variable (which contains the relevent image filename) to my homepage file. I pick this up using the $_POST method. This works fine, but only until I refresh the homepage at which point the variable is no longer there and so, understandably, the image is no longer called. Can anyone explain how I can store the variable value i.e. the image filename, so that it can be used until such time as it is overwritten by a new image. I have tried setting up a function that returns the value, but I can't seem to hold on to it!! Any guidance would be much appreciated.
Well, you can do multiple things :
Store it using MySQL. Create a table that will named "configuration" for example, with one field named "homepageImage" for example. When you want to modify the image on the homepage, just update this field. When you want to get the image, just query this table and fetch this field.
Store it in a file on your server. You just have to overwrite the file with the new filename inside it when you want to change the image. Fetch the content of the file when you want to display the image
Create a symbolic link (pointing to the right image), and update the link every time you want to change the image. When you want to display the image, just put the name of the link in your img tag
Use $_SESSION['some_variable'] = some value;
Later put 'if($_SESSION['some_variable'] = some value)' condition and display the image.
After the job done (if you need your visitors to do something) then distroy session.
session_destroy();

WordPress - Update Database Value in real time.

I have a piece of code that counts the lines of code within a wordpress site. I have successfully managed to send that number to the wordpress database so I can call on it in a different location in my theme files (this is because the script needs to be in the root of the install to count the lines of code).
Whilst this works really nicely, the BIG issue I have is that the number won't update itself automatically. i.e. the code for counting lines is www.mysite.com/loc.php and I mus go to this page and let it load before it updates the value in the database.
Is there a way to make it so that the value just automatically updates, so I don't have to navigate to the page for this to happen? i.e. when I add more lines of code, the value in the database updates itself.
Code for sending value to the db is as follows:
$num_of_lines = $folder -> count_lines();
update_option('line_count', $num_of_lines);
look into jquery and ajax call
Wordpress provides for you the framework to call backend functions on the frontend
http://wptheming.com/2013/07/simple-ajax-example/
another example:
http://premium.wpmudev.org/blog/how-to-use-ajax-with-php-on-your-wp-site-without-a-plugin/
You need to create a trigger, in order to update automatically MYSQL database
Look at this, i hope it will help
How to automatically update a MYSQL column value using formula and data from other columns in same row?

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.

Using PHP to dynamically create pages from a template

I'm creating a blog for a website I am building. The main blog page obviously has each blog listed as it should. But I want each blog to also have it's own individual page on the website. I want this page to be generated on creation of the blog post.
My question is, what would be the best method of creating this page. If I use the php file functions to create it, I would need to fill up a $data variable with hundreds of lines of HTML for the page. Which I guess is feasible, IF I am also able to dynamically change the variable to work for the new content that needs to be posted on said page.
Is there better methods? Would PHP work for this? Any suggestions would help.
In general, I would do it more or less like this:
1. Create a 'Blogs' SQL database table.
Create an 'id' column (primary key), and columns for fields like 'blog_date', 'title', 'author', and 'html_content'.
2. Create a 'Blog' class using php.
Create properties which correspond to the SQL fields in your 'Blogs' table, including 'id'.
3. Create public 'loadFromDB()' and 'saveToDB()' methods
These methods should load and save the SQL values of a row in 'Blogs' (selected by id) to and from the class properties.
4. Create a public 'view()' method for the 'Blog' class
This is simply an HTML view/template of a single blog entry displaying date, title, author, etc.
5. Create an 'index.php' blog page in a folder on the server
You want to use this page to display a single blog entry.
Use a blog's SQL id when calling this page from links in your main blogs page, for example via http GET: 'http://www.yoursite.com/blogs/blog/index.php?id=30'. In your php code inside index.php, where you want the entry to appear, do something like this:
$id_default = 27; // the blog entry which must appear when there is no query parameter
$id = empty($_GET['id']) ? $id_default : $_GET['id'];
$B = new Blog($id);
$B->loadFromDB();
$html = $B->view();
echo $html;
In this way, you don't have to create an HTML page for EVERY blog entry, or put ALL the entries on one page. You can just use one page created dynamically with a GET parameter instead.
Obviously there is more you can/must do, such as creating commenting displays and forms and a 'Comments' SQL table, etc. But this should give you some ideas.
Its possible to create individual page for each blog dynamically. Here are the steps you can follow.
Create one master template. Add some special tags where you want dynamic content.
While adding new post, Read the content from that master template, replace appropriate special tags with actual values
Write finally generated content into new file and save it to associated location.
You are ready to access that page.

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