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.
Related
I am working on building a Webstie for a restaurant and because clients will need to be able to order the items from the menu I've got to store everything on a MySQL database.
Now, everything is going to be organized visually in "cards"(you'll see what i mean in the image below) and each card is made up by an image and the title. Since I'm using PHP to deal with the SQL Queries and connection, the only thing I need is that once a card has been clicked, a new page gets presented with the same style, the same js scripts that I have, in other words, everything the same to the main page but with the correct number and type of cards.
You might have noticed that there are cards that create new pages with other cards inside and there are other cards, usually the last ones at the tree(image 3) that only create the menu. I've built everything and it works but all the data was stored inside JSON files and I had to copy the div for each card and create HTML pages for every category manually.
Now, I have already put everything inside the MySQL Database, I am referring to each item's data. So, what I need to do is simply create other html pages with different cards inside or create a menu(depending on the type of card). So, I understood that I need to create a tree, I thought about doing it in PHP in this way:
$tree = array("Drinks" => array(
"Alcholic" => array("Beers", "Vodka", "Wines"),
"Non-Alcholic" => array("Juices", "Something Else...")
),
"Food" => array(
//...
)
);
But, I don't have any idea about how to go from here. I tried my best to explain my objective in a clear and precise way. If, for any reason you need more details I will edit the post without any problems. This is a serious job and I will be very greatful for any amount of help I can get. Thanks in advance and have a great day!
You could use url parameter and do it all on one page, fetching data from db and creating cards dynamically based on the fetched result. For example:
if (isset($_GET['type') {
//suppose I clicked drinks
//then fetch drink types from db and and loop over them to create cards
//for example
<a href='index.php/?type=alcoholic'><img src='alcoholic.jpg'></a>
} else {
//show home page categories (drinks, food) if no parameter in url
//for example
<a href='index.php/?type=drinks'><img src='drinks.jpg'></a>
}
Alternatively, you could create separate pages for each subcategory, but that would be just duplicating the code.
I have a table named tags which contains thousands of tags, and increasing. While creating a new post (via newPost blade), I want to pass all tags from controller into this blade and populate them in a dropdown list (with typing functionality) to help the user adding tags. I feel this will be a lot of data to be sent with every post creation, which would affect performance. Am I right ? Any suggestions?
Here's the layout at the moment:
<header>
<navigation>
<section id="<?= $this->id; ?>">
<?= $content; ?>
</section>
<footer>
The page layout however (here called main.php), has the following hierarchy:
[header]
[dynamic gallery of images depending on db record id field] !!!
[navigation]
[content]
[footer]
Navigation is equal on all pages, so they should be on the layout, correct?
The dynamic gallery wrapper is equal on all pages, so it should be on the layout, correct?
But the images themselves, they should be different depending on the database records.
I don't even know where to start here.
Can I have a little push please?
UPDATE
Should I create a method inside components/controller.php or mycontroller or whatever we may have, that retrieves the value from the model, and make that value available as a public parameter to be used on the layout?
Does this makes sense to you?
As you correctly know, You must put your non-changing views into your main layout. But about views which will change during the request, You must create a different view for each one. Then, depend on your request you can renderPartial() your views.
Say, Your image view.
First, We create a view with name _image.php in our layout directory (or any directory).
Second we fetch the data from database. for example we have images data like below:
$images=array('test1.jpg','test2.jpg',...);
Third, In each request you must send your data to your renderPartial method:
$this->renderPartial('//layout/_image.php',array('images'=>$images));
and in your _image.php you can manipulate your data, which is always different in different requests.
There are another ways to do this:
Using the Yii's DECORATOR feature which is really cool. You can google it
Using Yii's CLIPS feature
UPDATE
In this case, using Yii's Decorator worked for questioner.
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'];
I'm writing simple website with some cms functions. It would be good to have all menu items and pages contents held in database.
I have table with id, name, parent_id and a content field. In future I would maybe move content to a content table to have multiple contents to menu item with fk. But it is not the case here.
The question is:
Do I need the URL field in menu table?
What else do i need to get it to work? Should every page have its own controller? I,m a beginner with zend framework, so please give me some directives. Thanks in advance.
Lets start with Question 2: every page does not need its own controller. If your pages are static you can even load every page using a single action. For more dynamic processing you could use a separate action for each page.
In any case, make sure you structure your code into controllers and actions in a way that makes sense. For example, inside your CMS a user might edit, create or delete a post. You could then create a PostController inside which you write an editAction, createAction and deleteAction.
You could store the URL in the table, but you do not necessarily have to.
Single action approach (mostly for static content)
Make sure the page id or name is stored in a GET param. You could then use the following code:
public function genericpageAction()
{
$thePageID = $this->_request->getParam('id');
// fetch the page content from the db based on $thePageID
// and pass it to the view
}
Of course, here, you could also match against the URL stored in the table if you chose that approach.
Multiple action approach (for more dynamic processing, most likely what you want with a CMS)
You could define a route for each page and load its content in the respective action. For example, for the page to edit a post:
class MyCMS_PostController extends Zend_Controller_Action
{
public function editAction()
{
// fetch the home page content
// do any further processing if necessary
}
}