Jump to content

Including a PHP variable as part of an <a href....>text</a> HTML tag


Jodha

Recommended Posts

Hi All,

 

Got a little question, new to this PHP but rather enjoying it. My books dont seem to cover this however.

 

Gist -

 

I am building an "index.php"

It contains a table with three rows and two columns.

 

top row = header

middle row, left column = navigation

middle row, right column = content (as per navigation)

bottom row = footer

 

I have a whole host of HTML based navigation scripts from previous HTML based projects, I would like to use these along side my growing knowledge of PHP. In my current situation I am building a site for a friend, and he 'wants' a particular HTML/JS based menu (cos he likes the way it looks)... I cannot code the same thing in PHP without spending more time than I have to learn PHP at a deeper level (something for after this project).

 

- Back to the subject:

 

The content of the middle row, right column should be

 

include($navigation_variable);

 

$navigation_variable is ultimately a file similar to the header and footer files I am already including in the displayed table in the index.php. However, I am competent enough in other PHP areas to have this pull info from a MySQL database (already got catalog type apps working from learning  about the code).

 

When it comes to assigning different values to the $navigation_variable in the <a href....>text</a> HTML tags I have in my navigation script I am having problems :(

 

In the statement "<a href=....>text</a>" how would I assign a different PHP value to the "href=" section?

 

With this knowledge I believe in the "middle row, right column" I can then use "include($navigation_variable);" (formatting this so it becomes a file name) to display different content, as per clicked links on the navigation script.

 

I think I am looking for something like:

 

<a href=$navigation_variable>text</a>

 

Currently I am getting an "Object not found" message from my navigation selections.

 

Any help muchly appreciated :)

Link to comment
Share on other sites

Well, in the header and footer row of the table in index.php I am using:

 

include(header.html);

or

include(footer.html);

 

(eventually these are going to be .php or .inc files, I am just trying to get the navigation working at the moment so I am not doing anything too extensive here - very simple template building!)

 

In the right column of the middle row (Body) I provisionally want to use the statement:

 

include($navigation_variable);

 

Although I suspect I will need to use "$navigation_variable" to create a new string which equates to a file name and path, something like:

 

$display = *the file path* $navigation_variable *the file extension*

 

Thus in the Body I will probably be using

 

include($display);

 

This website project will contain about 50 content pages, but these can be broken into 4 template which I can code to show relevant content, as per $navigation_variable referenced against the database.

 

Does this help? I'm off for a ciggie in the garden, back shortly.

 

Link to comment
Share on other sites

Basically:

 

table layout in index.php

 

|--------------------------------------------------------|

|include(header.html);                                                  |

|--------------------------------------------------------|

|                                  |                                          |

|include(navigation.html);  |include(body.html)                |

|                                  |                                          |

|--------------------------------------------------------|

|include(footer.html);                                                    |

|--------------------------------------------------------|

 

header.html = header content

navigation.html = Menu script

body.html = content as per menu selection

header.html = footer content

 

navigation.html = html and css script currently containing standard <a href="something.html">text</a>

 

Its making the "<a href="something.html">" aspect equate to assigning a PHP variable which changes what is displayed in the "include(body.html);" aspect for the different pieces of content.

 

Does that help a little more?

Link to comment
Share on other sites

I think you are trying to do something like this.  You could achieve this effect with $GET variables easily, though it would add a little bit of legwork to your end getting everything to work together smoothly.  Also, I'm not sure how to add tabs (I'm a newbie to these forums), so it will look kinda ugly.

 

include(config_nav.php);  // this would go at the top of the page, before it gets to any navigation output, such as in the header of the page or even before there if you are changing headers.

<?php
if (is_numeric($_GET['id'])) //prevent hacking
{
switch ($_GET['id']) //get the id of menu needed for this page
{
    case 0:
        $nav_menu = "default_nav.php"; //if the variable is 0 or not set
        break;
    case 1:
        $nav_menu = "category1_menu.php"; //if the variable is 1, get category 1 links
        break;
    case 2:
        $nav_menu = "category2_menu.php"; //if the variable is 2, get category 2 links
        break;
    default:
        $nav_menu = "default_nav.php"; //redundant for defaulting if the id is greater than 2
}
}
?>

 

Then, wherever you want that menu to appear, you do:

 

include($nav_menu);

 

And since it has been set according to the id number in the $GET variable, it will set the variable to the correct menu file name depending on the category linked to.  Therefore, if you wanted a link to lead you to a page that includes category 1 links, you would setup the link to look like this:

 

<A HREF=yourdomain.com/link.php?id=1>Category 1 Stuff</A>

 

This is usually how I do stuff like that.

Link to comment
Share on other sites

Hmmm.. interesting. That looks useful but I dont think it's quite what is required. It's probably useful if I post the menu script for you:

 

<d1 id="menu">

                <dt onclick="javascript:menu();"><a href="#">Home</a></dt>

 

  <dt onclick="javascript:menu('');"><a href="#">Section #2</a></dt>

 

<dt onclick="javascript:menu('smenu1');">Section #3</dt>

<dd id="smenu1"><ul>

<li><a href="#">Section #3.1</a></li>

<li><a href="#">Section #3.2</a></li>

<li><a href="#">Section #3.3</a></li>

<li><a href="#">Section #3.4</a></li>

<li><a href="#">Section #3.5</a></li>

<li><a href="#">Section #3.6</a></li>

<li><a href="#">Section #3.7</a></li>

<li><a href="#">Section #3.8</a></li>

<li><a href="#">Section #3.9</a></li>

<li><a href="#">Section #3.10</a></li>

<li><a href="#">District Councils Conferences</a></li></ul></dd>

 

<dt onclick="javascript:menu('smenu2');">Section #4</dt>

<dd id="smenu2"><ul>

<li><a href="#">Section #4.1</a></li>

<li><a href="#">Section #4.2</a></li>

<li><a href="#">Section #4.3</a></li>

<li><a href="#">Section #4.4</a></li></ul></dd>

 

                <dt onclick="javascript:menu();"><a href="#">Section #5</a></dt>

 

                <dt onclick="javascript:menu();"><a href="#">Section #6</a></dt>

</d1>

 

Where there are no sub menus you just link to the page.

 

Where there are submenus the open when you click on the main header and then you see a panel with all the actual links within.

 

Click on another section and this panel will collapse again, showing a new panel if its a section where there is a submenu, etc.

 

 

Now, if I stick with purely a HTML based website I can do this easily but I will be using frames, and I will be creating an individual html page for each menu item.

 

I want to move away from using html and framesets, but in this case my brief is to use this menu script as this is what my 'client' (friend in need of a website for his business quick) wants to see on his website.

 

Bear in mind I have only included a cut-down version of this menu script, there are many more sections and sub sections!

 

Sorry to be ott, but is there not a simple line of PHP code I can insert into the

 

<a href=" *required PHP code*  ">text</a>

 

section within this script which will allow the variable for the

 

include(body.html);

 

to change as per click on menu item?

 

By the way, many thanks so far people :)

Link to comment
Share on other sites

Its more of a case of making

 

<a href=  ~set a new value for a PHP variable~ > the menu item title </a>

 

Thus in another part of the index.php this "<a href=" set variable is used to adjust what

 

include( ~the <a href= set variable~);

 

displays.

 

One question which can bring this to closure -

 

Is there a short line of PHP code, for changing a variable value, which can be inserted (represented by "PHP HERE") into an <a href=....></a>, thus:

 

<a href="PHP HERE">My menu item</a>

 

If there is no such code snippit which will work I am going to have to give up on this for now and will resort back to HTML and framesets.

 

Not meaning to sound harsh, just need to get on with this project pronto.

 

I have been impressed with people's willingness to offer advice and guidance :) :) :) :) it just hasn't seemed to answer the question I am asking so far  ???

Link to comment
Share on other sites

<a href="PHP HERE">My menu item</a>

 

maybe some thing like this I do not understand what you are talking about really because you do not understand the basic of php to ask the right question I think.

 

$my_var='home.php'; // or could be a mysql result

<a href="<?=$my_var;?>">my page</a>

$id='21'; // or mysql result

<a href="home.php?id=<?=$id;?>">my page</a>

Link to comment
Share on other sites

Lordshoa, you probably have a good point in regard to my PHP knowledge as I am very much only just starting to learn. But so far I have successfully

 

- assigned variables

 

- changed variable values in regard to changing conditions (how many times through a loop/what has been selected from drop-down lists, etc

 

- combined variables to create new variables

 

- used loops

 

- use if statements and variables in loops to discount items within a database (or other info source) from display

 

- used include(); statements to retrieve content of other files

 

- used sql queries to acquire data from databases

 

- echo statements to display a reasonable host of different page elements

 

- code forms to insert data into a database

 

From what I know I can create an index.php file which includes elements using the "include();" statement. This I have done.

 

From what I think I believe it should not be hard to alter an <a href... statement to change a PHP variable, INSTEAD OF LINKING TO ANOTHER WEB PAGE ( sorry for the capital letters ;) )

 

In my index.php file I have PHP echoing the table layout as oppose to creating an HTML file, it contains my page elements in it where I want them

 

I have an HTML/JS menu script I want to use (listed above) in the index.php layout (also listed above)

 

Lets take the middle (problem) row -

 

Left hand side = HTML menu with <a href... type links

Right hand side = include(); statement

 

I want/need to use this menu script but I have to alter it so it changes a PHP variable instead of linking directly to another page. The value of the variable will be displayed in an include() statement, so, when on (and only on) index.php you have -

 

 

 

Top, full width of page:

Header always at the top, content defined in another file and included using include(header.html):

 

- this is done

 

Middle, left:

My menu script with collapsible panels where there are sub-menus, standard <a href... links including code to change the value of (for augments sake) $content, **instead of** linking to another page

 

Just need to know how (if possible) to modify the <a href... statements to change the value of $content instead of providing a link.

 

Middle, right:

include($content);

 

Bottom, full width of page:

Footer always at the bottom, content defined in another file and included using include(footer.html):

 

- this is done

 

 

Premise -

 

* I want the website visitor to only ever be displayed one page (index.php)

 

* Within index.php I want the middle right section to be dynamic, as per the visitor's selection from the menu on the left of the page

 

I can build this in HTML using framesets and all the code I currently have. Easy.

 

But I want to impliment this as PHP so I can use my other knowledge to build in content management functions. References to .html files will ultimately be .php or .inc files once I have template working. I just want to know what the underlined section in:

 

<a href="some_page.html">menu item text</a>

 

needs to become so that the value of $content becomes something new each time the visitor selects a new menu item.

 

Such as: <a href=<?php $content=links ?>>link</a>

 

Sorry if that was a rant, like I say, I'm impressed with the fact there are people here trying to help and advise, I'm sorry if I am too much of a newbie to be at grips with some of the things you are saying.

Link to comment
Share on other sites

What you are saying makes half sence to me.

 

You want a php page that uses frames which you would just include these files in the index.php

 

include('header.php');

<div>

<td align=top>

include('menu.php');

<div>

<td align=top>

 

include('main.php');

 

<div>

include('footer.php');

 

so as you can see its like frames but not becuase they are included with some divs tds and html to break it and make it.

All code is differant and can achieve the same results so hard to be exact.

 

but your main.php would be dynamic maybe with a switch from the menu ?

 

 

 

 

 

 

 

Link to comment
Share on other sites

Using a switch sounds like what kittrellbj suggests.

 

My understanding of switch is limited, I tried an experiment with this a few days ago but didn't get very far.

 

I know on some PHP sites the URL in the address bar changes from

 

http://www.mysite.com/index.php

 

to

 

http://www.mysite.com/index.php?action=something

 

This was what I was trying to achieve the other day, but was not entirely successful; syntax problems which although following the code stated one of the books I have still occured. Double and triple checked the code for typos, no avail.

 

As I understand it, the index.php (or any PHP file) can be sectioned and the content changes as per which section is executed, correlated to the "?action=something".

 

Problems I seem to come across here is:

 

1) How do I identify each subsection of the code to the respective "?action=something"

(the identifying piece of code at top of each code subsection)?

 

There is a line of PHP which heads each subsection, the syntax is not clear to me but I expect its easy to understand and implement.

 

2) How do I make a link which executes the desired subsection?

 

Further to this, can my current <a href... statements point to these subsections?

If so, whats the syntax within the <a href... statement, something like

 

<a href="index.php?action=links">links</a> ?

 

This would make sense to me, if true, the stumbling block is just heading each subsection correctly.

 

Which half of what I say does not make sense to you? I'll try and explain better for you :)

 

Link to comment
Share on other sites

kittrellbj suggests

 

case is the switch it is what changes the switch to the page from the menu and in the main.php would be the pages 0 to 2

 

default being the page everyone sees first

 

index.php?id=0

index.php?id=1

index.php?id=2

 

 

So maybe you should follow his suggestion it looks like what you wanted to know.

 

Link to comment
Share on other sites

Using action= instead of id= is exactly the same as one another, with a simple difference:  you are using a string identifier instead of a numerical identifier.  Therefore, a case switch might look something like this:

 

<?php
$string = $_GET['page'];
switch ($string) {
    case "links":
        include(links.php); // mydomain.com/index.php?page=links
        break;
    case "forum":
        include(forum.php); // mydomain.com/index.php?page=forum
        break;
    case "contact":
        include(contact.php); // mydomain.com/index.php?page=contact
        break;
    default:
        include(index.php); // mydomain.com/index.php?page=(anything else)
}
?>

 

For the above, you notice of course that each "page" case is in double quotes, as needed for defining strings.  If the user has a page= matching any of the strings, it will send them to that page (or include that page wherever you have this switch statement).

 

Also, if you go with this route, make sure you add some hack detection.  In the first one, I included (is_numeric) to defend against hacks (it requires an integer to proceed).  But, for strings, it becomes a little more complicated.  You can do a test against an array to see if the value they entered after page= is in the array, you can do a preg_match or an ereg, etc.  Check the PHP manual on each of these functions.  (in_array, preg_match, and ereg).

Link to comment
Share on other sites

So, let me see if I understand this -

 

Lets say I have PageX, Page Y, Page Ya, Page Yb, Page Yc, Page Z.

 

On all pages I want the same Header, Menu and Footer.

 

I code the index.php file with the following way:

 

Includes =

 

include(header.html);

include(menu.html);

include(footer.html);

include(PageX.html;

include(PageY.html);

include(PageYa.html);

include(PageYb.html);

include(PageYc.html);

include(PageZ.html);

 

I have these in a 3 row table, Header & Footer at top and bottom across entire page width. Middle row is split into two columns, left side is Menu, right column is content, as per:

 

PageX

PageY

  PageYa

  PageYb

  PageYc

PageZ

 

Lets call this entire arrangement of Header, Menu and Footer plus content page  "Display Table", or more specifically "Display Table with Page X" (for example).

 

In the menu the <a href="blah">blah</a>'s become

 

<a href="index.php?PageX=">PageX</a>

<a href="index.php?PageY=">PageY</a>

<a href="index.php?PageYa=">PageYa</a>

<a href="index.php?PageYb=">PageYb</a>

<a href="index.php?PageYc=">PageYc</a>

<a href="index.php?PageZ=">PageZ</a>

 

As I run down index.php I code it thus?

 

<?php

$string = $_GET['page'];

switch ($string) {

    case "PageY":

        "Display Table with Page Y"

        break;

    case "PageYa":

        "Display Table with Page Ya"

        break;

    case "PageYb":

        "Display Table with Page Yb"

        break;

    case "PageYc":

        "Display Table with Page Yc"

        break;

    case "PageZ":

      "Display Table with Page Z"

        break;

    default:

      "Display Table with Page X"

}

?>

 

 

You're probably getting a bit tired of me by now, but confirmation or denial + "here's what's wrong" would be handy.

 

Thanks :)

Link to comment
Share on other sites

So, let me see if I understand this -

 

Lets say I have PageX, Page Y, Page Ya, Page Yb, Page Yc, Page Z.

 

On all pages I want the same Header, Menu and Footer.

 

I code the index.php file with the following way:

 

Includes =

 

include(header.html);

include(menu.html);

include(footer.html);

include(PageX.html;

include(PageY.html);

include(PageYa.html);

include(PageYb.html);

include(PageYc.html);

include(PageZ.html);

 

Not really; the pages would not need to be included at this point, as they would be included by use of the switch later.

 

I have these in a 3 row table, Header & Footer at top and bottom across entire page width. Middle row is split into two columns, left side is Menu, right column is content, as per:

 

PageX

PageY

  PageYa

  PageYb

  PageYc

PageZ

 

Lets call this entire arrangement of Header, Menu and Footer plus content page  "Display Table", or more specifically "Display Table with Page X" (for example).

 

In the menu the <a href="blah">blah</a>'s become

 

<a href="index.php?PageX=">PageX</a>

<a href="index.php?PageY=">PageY</a>

<a href="index.php?PageYa=">PageYa</a>

<a href="index.php?PageYb=">PageYb</a>

<a href="index.php?PageYc=">PageYc</a>

<a href="index.php?PageZ=">PageZ</a>

 

It wouldn't work like this.  You have to use an identifier for each page value.  For instance, the variable is $_GET['page'].  The value stored in that variable is based on the URL.  i.e. if the URL is http://mydomain.com/index.php?page=PageY, then the $_GET['page'] variable is automatically storing "PageY" as it's string variable.  Therefore, wherever you want the PageY content to appear at, you would include that information in the switch.

 

As I run down index.php I code it thus?

 

<?php

$string = $_GET['page'];

switch ($string) {

    case "PageY":

        "Display Table with Page Y"

        break;

    case "PageYa":

        "Display Table with Page Ya"

        break;

    case "PageYb":

        "Display Table with Page Yb"

        break;

    case "PageYc":

        "Display Table with Page Yc"

        break;

    case "PageZ":

      "Display Table with Page Z"

        break;

    default:

      "Display Table with Page X"

}

?>

 

Sort of.  Instead of "Display Table with Page Z", it would be one of two options.  It would be setting a variable to a URL, i.e. $URLtoinclude = "mydomain.com/pages/PageZ.php", or you would include() the page in the switch if the switch was at the location you wanted the code to appear.

 

You're probably getting a bit tired of me by now, but confirmation or denial + "here's what's wrong" would be handy.

 

Thanks :)

 

Nah, it's a learning process.  That's what the forum is for.

 

Let me formulate a tutorial for what you are doing real fast...

 

Using $GET Variables to Display Content

 

index.php

<?php
include(includes/header.php); // includes the header file.
// include any configuration files or connection files here on the first line
?>

<TABLE BORDER=1 WIDTH=100%>
<TR>
    <TD COLSPAN=2>
    <CENTER>This is the Top Bar Area</CENTER>
    </TD>
</TR>
<TR>
    <TD WIDTH=30% VALIGN=top>
    <CENTER>Navigation Menu</CENTER><BR>
    <A HREF="index.php?page=home>Home</A>
    <A HREF="index.php?page=links>Links</A>
    <A HREF="index.php?page=contact>Contact</A>
    <A HREF="index.php?page=forum>Forum</A>
    </TD>
    <TD WIDTH=70% VALIGN=top>
<?php
   // This is where the content of the page goes.
   //  Therefore, based on what link the user clicks,
  //  He gets the content depending on what link is
  // clicked in the navbar menu.
switch ($_GET['page'])
{
     case "home":
       include(includes/main.php);
       break;
     case "links":
       include(includes/links.php);
       break;
     case "contact":
       include(includes/contact.php);
       break;
     case "forum";
       include(includes/forum.php);
       break;
     default:
       include(includes/main.php);
       break;
}

// Now, break the column and the row
    echo "</TD></TR>";

// include the footer at the end
    include(includes/footer.php);
?>

 

Now, you can create a page called links.php, contact.php, forum.php, and main.php, and make just random information in them.  Save them in a directory called /includes/ where the index.php is.  You can also create a random header and footer just to test it out for now.

 

That's basically how $GET works to make your pages dynamic, use less files, and thus less hard disk space.

Link to comment
Share on other sites

Hi Jodha.  As a newbie myself, I couldn't help but notice similarities to a problem I have grappled with, and overcome except for a bug in DW CS3.  It looks to me as though you have more or less got it, but for expediency, and as I haven't much time, I'll just try to list how I have over come most difficulties:

 

I have more or less a php template page (representing all pages) that I 'fill' with 'stuff' that is extracted from an xml file.  A typical menu link is:

<a href="?page=index">

 

if Javascript is off, this querystring gets sent to the original page (beit index.php or contact.php etc).  I return a value (the specific page xml) from an include statement and save the value in a variable that all subsequent requests for relevant xml, which are called from the point where I need the xml in the page, echo it as a string and Bob's your auntie.

 

It is much more convoluted than this because I also have the whole thing duplicated for when Javascript is on - sigh!

 

Hope it helps.

 

PS You don't know how to get php to read an attribute from a script tag, within the main document (such as index.php itself) do you?

 

Cheers

Link to comment
Share on other sites

Hi GURAQTINVU,

 

All problems solved (see other thread on same subject), further up the learning curve, and much happier :)

 

PS You don't know how to get php to read an attribute from a script tag, within the main document (such as index.php itself) do you?

 

Getting there ;)

 

Still a lot of other stuff to learn, but I now know enough to build my current site project.

 

Once done, I can turn attention back to the books and get to grips with other aspects. Enjoying the learning, just get frustrated (maybe a little too easily) when what should be a simple answer doesn't show its self clearly... but that might be compounded by me not always knowing what I am actually looking for  ::)

Link to comment
Share on other sites

  • 6 months later...

I found this page while searching for an answer to a similar problem I was having. I know this thread is like 7 months old, but I feel it important to post a small nugget of wisdom to help anyone else who comes across this page looking for help.

 

after you have declared your variables, example:

 

<?php 
$val1 = $_GET['display'];
$val2 = $_GET['menu'];
?>

 

to change these in a ahref link you can do this:

 

<a href="?display=<?php echo $val1; ?>&menu=test2.html">Menu to Test 2</a>
<a href="?display=test.html&menu=<?php echo $val2; ?>">Display toTest 1</a>

 

So inserting an echo into the link works just fine, allowing you to change one php value and allowing another to stay the same on the page. I hope this helps someone out there :D

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.