Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Variables URL - Assistance Needed

Featured Replies

To get it out of the way, I am learning php as I go and I am new to it. I am really trying to get my hands dirty in it on this project to learn and to make my life easier!

I have a scenario, and I would love for someone to help me out through it and point me in the right direction.

 

My scenario is:

  • I have a latest news page on my 'static' website.
  • I am using php includes on the header, navbar and footer for every page.

I am using includes to grab the latest news information I have in assets/news/form/news1.php. This is the following:

 

<?php
if (!defined("ACCESS"))
{
die ("Don't waste your time trying to access this file!! Please head to the families relief home page and donate!!");
}
?>
<?php
$header = 'Article Header';
$date = 'June 1st 2013';
$url = '/news/articleName';
$body = 'Body in HTML';
?>
 

 

I then have another file in assets/news/news1.php containing:

 

<div id="appeal3" class="row-fluid">
                <div class="well span12">
                    <div class="span3">
                        <div class="thumbnail">
                            <a href="<?php echo $url ?>"><img src="/img/news-blank.jpg"></a>
                        </div>
                    </div><!-- span3 -->
                    <div class="span9 appeals">
                        <p class="app-date"><?php echo $date?></p>
                        <a href="<?php echo $url ?>"><h3><?php echo $header ?></h3></a>
                        <p><?php echo substr($body, 0, 200) ?><span class="readmore"><a href="<?php echo $url ?> "> ...Read More ></a></span></p>
                    </div><!-- span9 -->
                </div><!-- well span12 -->
            </div><!-- appeal# -->
 

 

 

I then use the following code on my news overview page at /news/index.php to display the html with the variables entered:

 

<?php include("$_SERVER[DOCUMENT_ROOT]/assets/news/news1.php"); ?>
 

 

So what this gives me is an overview page, with the news article displayed around a wrapper in overview view.

I then have another page when the overview 'read more' is clicked.

 

Within this page, I have a full view news article page with a wrapper where I can echo the variables in.

 

Wha La! That is my news article structure.. Long winded isn't it!

 

Now, what I want!! I want to be able to include the variables from an article into my overview page and include the 2nd block of code above as the template. I then want the articles to fill out the variables. However, I have 5 articles on this page with identical variables. What is the best and most efficient way to do this??

 

Secondly, I have a full-view article page. How can I use this page as a template, and change the php it gets it's variables from. But, I don't want to create a new php page every time I have a new article.

 

What I had in mind, is something like the following:

domain.com/news/article/index.php?news1

 

I have been reading up on passing variables in a URL. I want to expand my knowledge on this. Could this be the solution to my problem. My issue is that I have NO idea how to go about this? Can someone please lecture me to solve this and hopefully teach me a little in between! happy-01.gif

 

Thanks!

Hi Icot.

 

You are asking some great questions, which I came across myself when moving from static websites to building more data-driven sites.

 

There's a couple of pieces you need to make this work. The notion of a template is essential. My own project, www.openclassical.com, depends heavily on this approach. It's a site that manages some 1,000 composers, and many thousands more compositions - so far. The majority of pages on the site are generated by the interaction of a handful of php files and a database.

 

As an example, you can see my page for Beethoven: https://www.openclassical.com/composer/Ludwig_van_Beethoven.

 

I have a php file called composer.php, which takes the composer "string" identifier from the URL, one of the things you ask about. This is done (on an apache server) via mod_rewrite. This looks fairly daunting initially (at least it was for me), but if you know what you need to use it for, you can quite quickly put together what you need.

 

A good tutorial is here: http://www.elated.com/articles/mod-rewrite-tutorial-for-absolute-beginners/.

 

For my Beethoven page mentioned above, the following code in my .htaccess file transforms the url into something my composer.php file can then process, with the "link" parameter now containing the composer name string:

 

 

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule composer/([^/]+)$ /composer.php?link=$1
 

 

 

This transforms

 

 

https://www.openclassical.com/composer/Ludwig_van_Beethoven
 

 

 

into

 

 

https://www.openclassical.com/composer.php?link=Ludwig_van_Beethoven
 

 

 

In the php file, I can grab this parameter from the URL as follows:

 

if(isset($_GET["link"]) && (strlen($_GET["link"]) > 0)) {
    $linkToPage = $_GET["link"];
} else {
    myLog("link was not provided.");
    exit;
}
 

 

 

 

From this point, my composer.php file knows which particular composer to generate the page for. In my case, I now hit a database, as each composer has a great deal of associated data that would be harder (and slower) for me to manage in separate files. However in your case a separate file would be fine; data is data.

 

I have several utility php files, such as html_utils.php that I use to generate my html header & footer code, user_toolbar.php which genarates a toolbar for the visitor, and so on. The key point here is that composer.php is my template, but it's really a large php script that runs to generate the html code, and I break out common functions as much as possible, for maintainability and easy re-use. So there's no specific 'template' functionality or technology that I use, just php functions broken out into separate files. The key step is to figure out the inputs via the URL, then make sure that the ensuing code is appropriate for all possible inputs.

 

Finally, my personal preference would be to avoid using 'include' as a way of having php code executed - I would rather wrap the desired functionality up in a function (in the same file you would include), then call the function. For example, I have a function to generate html for the document opening, which I give the function signature as follows:

 

function printDocumentOpening($title, $description, $cssFiles, $scriptFiles, $bodySetupFunction, $additionalKeywords = NULL);
 

 

 

 

Once my php file knows what to do regarding data, it calls this function as follows:

 

printDocumentOpening(    htmlspecialchars($composer->pageTitleDisplayString),
                        $pageDescription,
                        array("composer.css", "composer_all_works.css", "composer_analysis.css", "composer_catalogs.css", "composer_top_ten.css"),
                        array("composer.js", "composer_all_works.js", "composer_analysis.js"),
                        "setup",
                        array(    htmlspecialchars($composer->pageTitleDisplayString),
                                htmlspecialchars($composer->periodName)));
 

 

 

 

I can then call this function with different parameters as I need. I find doing this via an "include" counter-intuitive, as my coding background is more grounded in object-oriented development, such as C++; making things happen via includes is harder to extend, and you can't parameterize it - except by depending on implicit global variables you should declare prior to the include that the included file then has access to ... ugh! As your project develops in complexity, this approach will quickly lead to much head-scratching & bug-hunting, which a more function-oriented approach will avoid.

 

Hope that helps!

 

-David

Edited by d-clef

rallport - thanks for the reply. I fully appreciate that when it comes to coding there are many ways to get things done, so if includes & template frameworks are your preference, that's fine. Coming from a C++ background I have pretty firm notions about what is an include, and what is a function, and using an include to essentially execute code mixes things up in my head unnecessarily. I'm afraid I don't see an actual benefit to using an include over a function, which would of course help to persuade me.

 

Twig looks interesting - thanks for the link!

 

Regarding the function I provided as an example, the reason I don't pass in the entire $composer object by the way is because that function is designed to be used site-wide, which includes front-page, composer pages, composition-pages, data-editing pages, and so on.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.