Using PHP to automatically add new post links - php

I'm certain I can use PHP to accomplish this task, but I'm not sure how.
What I currently have is a faux news (ha ha ho ho) site for practise here.
http://puu.sh/402Rl.png
For Browse News, I would like all html documents within a specified folder to be shown in the format I have
SAMPLE
<p class="content centeralign">
8.12.13 <!-- ARTICLE NAME -->
</p>
<hr noshade></hr>
Although it's not much, setting up a way to do this automatically would save some time.
Here is how I would imagine the logistics behind this would function ----
All HTML files will be listed in a folder
They will all have a consecutive order based on the date they were created (e.g. 1.html, 2.html, 3.html etc.
PHP would find each document and add it in the right order
A bit inside the file would define the title (meta tags?)

That seems a really bad way of doing it, but anyway.. You want to be you want to be using the directory functions. Specifically http://www.php.net/manual/en/function.readdir.php

A good idea would be to set up a MySQL system for this, but it is achieveable with PHP only.
You could get all .html files in the folder with glob, and then include them with this code:
foreach (glob("/htmlfiles/*.{htm, html}") as $filename) {
include "$filename";
}
The nice thing about this, is that it's sorted alphabetically and numerically too.
Edit: You would then use this system twice, with two folders, one for the meta tags/title, and one for the page itself. Again, not the best way to do it. You should really check out a CMS.

If you used MYSQL, you can still get all html files from the folders as you are now, but just use MYSQL to make simple basic references, such as the filename, date, category, etc. Then you don't need to use complex (and likely failing) file and directory code to determine when a file was created and which order to serve them in. The DATE in your MYSQL would be when to serve them.
To answer your meta questions, I would have a header.php file with all the meta data in for the site (doc declaration, titles, css links, etc) and then each individual file could have a variable to pass to the header when it's included.
eg
File: about.php
$PageTitle = 'About';
include_once('header.php');
<some more code>
<h1>$PageTitle</h1>
File: 1.php
$PageTitle = 'News about something';
include_once('header.php');
<some more code>
<h1>$PageTitle</h1>
File: header.php
usual code, doc declaration, head, etc
<title>$PageTitle</title>
So at the start of each file you declare what the title will be then include header.php. The title is used on the meta title (so your browser and tab etc) and the var in the file can also be used on headers (ie h1, h2) and links if needed.
you could do all this without MYSQL still, but using a database to even just reference things like date_of_creation - last_update_date - author, etc, could save you headaches, but is up to you how you want to do it and what skills you have etc.

Related

Magento modify the final output in all pages

i need to parse the final output contents (html) of all the pages of this CMS, (using PHP and my own class)
something like:
<?php
echo get_magento_output();
to:
<?php
echo parse_content(get_magento_output());
Can i create an extension that do this? if not, what files i must modify to modify the output in all pages?
To answer your question, yes, you can do it here:
magento/app/design/frontend/enterprise/themename/template/page/1column.phtml
subbing in 1column for whatever the name is for you. However, I would beg you not to do this. If you want to modify things on all pages, change the theme or template. Theres tons of documentation out there on Magento Themes, and there are plenty of places you can alter phtml files to change your output to be correct the first time around.
as example, we have the default header:
/app/design/frontend/enterprise/default/template/page/html/header.phtml
If you want to change css classes, of header for example, change:
<div class="header">
to
<div class="my-new-header">
the phtml functionality is strong, and you can accomplish a lot in these files.

PHP automatic footnote and endnote generator

This is more of a general information question involving endnotes than a "check my code" one. That's because I can find almost no (useful) information on the subject and don't have the skills to create this myself. But I still think it's useful to create a general brainstorm session / forum thread on the net about this.
The issue: I've written about 60 articles, a dozen of them book-length or near book-length on a site that has been manually designed with HTML5, CSS3, jquery and PHP - the latter two mainly with pre-existing code. I'm very happy with it except for one thing: endnotes! It takes forever to update them.
An average article has 120 endnotes (up to 550). It happens frequently, especially during the writing/proofreading process, that I need to add more information or want an additional endnote. That means anywhere from 2 to 30 minutes of copy-pasting "[113]s", "[114]s" around. It's hopelessly inefficient.
Ordinarily I dislike the uninspirational Wiki CMS platforms, but they have one huge benefit: cite.php plugins. Like this one:
https://www.mediawiki.org/wiki/Special:ExtensionDistributor?extdist_name=Cite&extdist_version=REL1_26&extdist_submit=
Once you have this, you just put an URL between <ref> </ref> and an endnotes gets automatically generated below a {{reflist}} tag. It's explained here:
https://en.wikipedia.org/wiki/Help:Footnotes
Footnotes are created using the Cite.php software extension. This
extension adds the HTML-like elements <ref>...</ref>, <references />
and <references>...</references>. The elements are also used in a
number of templates; for example, it is becoming more common to use
{{reflist}} rather than <references /> as it can style the reference
list.
I've checked out the plugin and it, of course, is much more than just a few lines of PHP.
My main question is if anyone is aware if this type of code has been created for custom designed websites. Or if someone has an idea how to program this manually? If it's not too hard, I might try it myself in the near future or hire a programmer.
P.S. I did study HTML5 solutions for endnotes in the past. Can't remember the details, but they were terrible. It's crucial to have one type of tag, with each one generating a new automatic endnote.
{{ }} is not standard HTML tags, but usually in some modern MVC frameworks they are used as replacement for PHP syntax like echo $foodNote which is the same as {{ $foodNote }}.
A MVC framework like Laravel use it as part of blade template.
But in the provided link you have in your question, the {{reflist}} is just referring to the content inside the tags like <ref>Content of the reference</ref>.
The provided Cite.php helper file is parsing the content inside tags like <ref>...</ref> to variable reflist inside a curly braces with the same content.
It should be not very difficult to program such thing.
Here is a simple PHP script to handle footnotes automatically. The only significant caveat is that your web page file name must end in .php or .phtml (not all web servers support .phtml). This is no problem because the web server will treat the file exactly as a .html file, except it watches for PHP tags so it can process the embedded PHP scripts.
Here is the script.
<?php
function footnote($footnote){
global $Footnotes, $FootnoteCount;
$FootnoteCount++;
$Footnotes[$FootnoteCount] = "$footnote";
print "<sup>$FootnoteCount</sup>";
}
function PrintFootnotes(){
  global $Footnotes, $FootnoteCount;
for($i = 1;$i < $FootnoteCount + 1;$i++){
  print "<sup>$i</sup>$Footnotes[$i]<br />";
  }
}
?>
You can put the script at the top of each page.
Better yet, save the script in a file named FootnoteFunctions.php. Of course, you can name it what you want or put it in a file with other functions. Just change the following include as appropriate. Next, put the following in the head of your HTML document:
<?php include("FootnoteFunctions.php"); ?>
Put this where you want the footnotes to appear at the bottom of the page:
<?php PrintFootnotes(); ?>
To create a footnote insert the following where you want the footnote number in the text (with your text between the quotes):
<?php footnote("footnote text here") ?>
That's it.
You can embellish the script as desired. For example, to pop up the footnote text as a tooltip, add title="$footnote" to the tag. You can also put a table tag, etc, in the printing function to make the footnote numbers and text line up nicely.
Here is my page explaining line by line how the script works. It also has an embellished version with the features mentioned above.
https://vocademy.net/textbooks/WebDatabase/Footnotes/PageSetup.php?Page=3&CourseDirectory=WebDatabase

PHP templates updating all html document

I have a lot of webpages that all have the same title and the client keeps asking me to change the name of the title, is it possible to use php to update all the titles in the html document at once?
Thanks
edit: I don't have any knowledge of PHP beyond using wordpress, I was thinking it would probably look similar to the get_header link, but when searching PHP get I don't get anything I can make use of.
All you need to do is create a separate file named something like title.php
The only thing you need in this file is something like this:
<?php
$title='titlename';
?>
Then in all of the php files that need this (note, if you've been saving them as .html they may not work; most of the time they will need to be .php) add this at the top of your page
<?php
include_once('title.php');
?>
Below, all you need to do is inside the head tag is this
<title><?php echo $title; ?></title>
Any time you need to change the title name, just change it in the title.php file.

Newbie Question about adding an array element on a page (php script)

Apologize me if the question is unclear.
I'm a total newbie php user and I have this script that I want to edit.
The script have uses .tpl for the theme.
In the memberprofile.tpl there is the first name element $profilearray[0].firstname I want to add the result of this element also in another .tpl file called docs.tpl
I tried copying and pasting `$profilearray[0].firstname in docs.tpl but it did not work.
I noticed that docs.tpl uses its own $docsarray[0].xxx
So guys any idea how to do this? because there are some information in the member profile that I would like to add them inside the docs page.
I tried playing with mysql but I don't know how to use the same element firstname for both memberprofile and docs tables
I'm sure there is an easy way to do it.
Here's the full code from memberprofile.tpland I want to show some of these info in that theme docs.tpl
<p class="gray">
{$lang112}: <b>{$profilearray[0].firstname} {$profilearray[0].lastname}</b><br>
{$lang130}: <b>{$profilearray[0].birthday}</b><br>
{$lang134}: <b>{if $profilearray[0].gender eq "1"}Male{elseif $profilearray[0].gender eq "0"}Female{/if}</b><br>
{$lang140}: <b>{$profilearray[0].city}</b> <br>
{$lang139}: <b>{$profilearray[0].country}</b> <br>
{$lang113}: <b>{insert name=get_stripped_phrase value=a assign=pdesc details=$profilearray[0].description}{$pdesc}</b> <br>
{$lang259}: <b>{$profilearray[0].url|stripslashes|truncate:20:"...":true}</b> <br>
{$lang260}: <b>{insert name=get_time_to_days_ago value=var time=$profilearray[0].lastlogin}</b> <br>
{$lang261}: <b>{insert name=get_time_to_days_ago value=var time=$profilearray[0].addtime}</b>
</p>
Smarty has a tag you can google for called {include} that will do what you want. http://www.smarty.net/docsv2/en/language.function.include.tpl Use that tag on your docs.tpl file and you will be fine.
$profilearray
in your example is assigned to smarty template before the template is called in some fashion simmilar to this
$smarty->assign('profilearray',$somearray);
you'll need to find what is setting that in your first file then make sure that is included in your second template call
but you should certainly read the smarty documentation to understand what you are trying to do.
To step back for a moment... There are two parts to all this. The first part is the PHP code which actually takes user input, queries the database, processes the data, etc. The second part is the TPL file. As much as possible, the TPL file should only be concerned with presentation, not with data-processing, data-cross-referencing, etc.
All that database-reading and cross-referencing should happen inside a plain PHP file, not inside the TPL.
In order to add "Author information" to the "Documents List" (or whatever you call docs.tpl) page, you need to find the PHP code which pulls up the list of documents. Find the PHP code where it says something like:
$smarty->assign('docsarray',$document_list);
Now what you want to do is pass more information to the smarty template (TPL file) so that it can display it. Something like:
for($document_list as $index => $doc){
$owner = $doc['owner']; // Get the owner of the document
$profile = getProfile($owner); // Create one of the same things that go into $profilearray elsewhere
$document_list[$index]['profile'] = $profile; // Modify original array
}
$smarty->assign('docsarray',$document_list);
Then go into docs.tpl and find where it displays information about each document, and add smarty template code to read from the new per-document information you added. (Check the Smarty reference page for details.)
For example, if docs.tpl shows a table of documents, you might add a new column that shows the author's first/last name:
<tr>
<td>{$docsarray[$index].title}</td>
<td>{$docsarray[$index].created_date}</td>
<!-- Next line is new -->
<td>{$docsarray[$index].profile.firstname} {$docsarray[$index].profile.lastname}</td>
</tr>
If you want something that looks exactly like the "profile box", you can do that too. In fact, using {include} you can create profilebox.tpl and use it in both places to reduce redundant code.

Smarty Caching (With Dynamic Content)

I have a very dynamic (social networking) site running smarty that I want to enable caching for.
My Structure:
index.php display()s template.tpl
template.tpl include()s indexContent.tpl
Most of the content in template.tpl is static .. such as the scripts, banner, footer.. etc. How can I cache that but not specific parts which look different to depending on whose logged in (among other factors)?
I've discovered 3 methods:
{nocache} {include='indexContent.tpl'} {nocache}
{dynamic} {include ...
Set the cache_id for each page.
Unfortunately each has a problem:
Doesn't really seem to work? Dynamic content still gets cached..
Not sure how to implement or how it's different than (1)
How to identify uniquely? Some pages have the same "name" but different content for specific members... think "myProfile.php"
Any suggestions? Thanks!!
You can use reverse proxy, like Varnish to cache the static part of the page and to include your dynamic content as Server-Side Includes (for Varnishi it is ESI). Next you will need to setup the caching rules for your static and dynamic URLs so that the static one will be cached for a long time period while the dynamic one will not be cached at all.
To make it easier to understand the whole idea here is how your page HTML code could look like:
<html>
<head>...</head>
<body>
...some static layout...
<esi:include src="/esi/indexContent.php"/>
...some another static layout...
</body>
</html>
Where /esi/indexContent.php is the script that generates the dynamic content.
For Varnish: beware of the gzipped or deflated content with ESIs as it is described in the answer here
We have the same scenario. Our entire front page is cached except for a couple of dynamic elements (news, latest forum threads) and the easiest way I found to get around this is to add in a keyword to the cached template
NEWS_BLOCK
on your logic script you then load your news template and preg_replace it with the keyword.
$news_template = $smarty->fetch('news_template.smrt');
$page_body_raw = $smarty->fetch('frontpage.smrt');
$page_body = preg_replace('/NEWS_BLOCK/', $news_template, $page_body_raw);
in 3 way u can save cache file by this name:
myprofile_id for example a persone that registered and his id is 455 in user table u can save cache file for he with this name myprofile_455
after that u can include cached file in tpl file like this:
{include file="cache/myprofile`$smarty.get.userid`.html"}
I know the question is old my i am still proposing a solution to help someone else.
I seem to get into same trouble with a social networking site i am developing. Here is the solution that worked for me
Doesn't really seem to work? Dynamic content still gets cached..
Not sure how to implement or how it's different than (1)
Just remove the static part of your page like footer and header and put them in a different tpl file. Then include the tpl file as
{include file='head.html' cache_lifetime=5000}
or conversely remove the dynamic part of your page and put it in another template and include it as
{include file='head.html' nocache}
3.How to identify uniquely? Some pages have the same "name" but different content for specific members... think "myProfile.php"
for same page with different content like a profile page, you can pass profile Id as a parameter to cache call.
$my_cache_id = $_GET['profile_id'];
$smarty->display('index.tpl', $my_cache_id);
This will ensure that same page with different parameters are not treated as same page.
Hope this helps.

Categories