6 Replies - 238 Views - Last Post: 15 March 2012 - 05:56 PM Rate Topic: -----

#1 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 15-March 12

Passing a pointer between HTML/CSS pages

Posted 15 March 2012 - 03:33 PM

I have multiple HTML/CSS pages that access a contact page. I currently have to duplicate the contact page for every
page that accesses it. Needlessly redundant I think.

I have tried to declare a global variable in PHP at the start of each HTML page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php

   global $page_view ;
   $page_view="index";

?>

<html>
<head>


Obviously a different value for each page

Then in the contact page I tried to use an IF/ESLE to select which page to return to, but no success. The following
example has only 2 choices. It displays both button but will only execute the second one.

<div id="info_buttons">


                   <?php $select=$GLOBALS["page_view"] : ?>
                   <?php if ($select="index") : ?>

                      <a href="../../index.html"><img src="../../images/button_back.png"
                              alt="Back" /></a>

                   <?php else : ?>

                      <a href="../index2.htm"><img src="../../images/button_back.png"
                              alt="Back" /></a>

                   <?php endif : ?>



                </div><!-- ends info_buttons -->



I am new to PHP and have spent 2 days running around in circles. Any help would be greatly appreciated. Thanks.

This post has been edited by Atli: 15 March 2012 - 04:00 PM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Passing a pointer between HTML/CSS pages

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,574
  • Joined: 08-June 10

Re: Passing a pointer between HTML/CSS pages

Posted 15 March 2012 - 04:08 PM

Hey.

I think you may be misunderstanding how global variables work. The global keyword is not used to declare a global variable, it's used to import a global variable into the current scope. Any variable created outside of a function or class method is global, and can be imported using the global keyword.
<?php
// Define a global variable.
$myWord = "Hello";

// Define a function to print the word.
function printWord() {
    // Import the $myWord global variable into this scope.
    global $myWord;

    // Now this will print: Hello
    echo $myWord
}



In your case, you may be better of using a Constant. Those are ideal when you need to store a value that doesn't change, and needs to be accessible throughout the code.
<?php
define("MY_WORD", "Hello");

function printMyWord() {
    echo MY_WORD;
}




By the way, there is one crucial error in your code. If you want to compare values, you should use two equal signs. If you use only one, you are setting the left-hand variable to the value on the right-hand side.
// This doesn't check if $something is "index", it
// sets $something to "index", and then, inherently, evalues
// as true.
if ($something = "index") { ... }

// That should be:
if ($something == "index") { ... }


Was This Post Helpful? 0
  • +
  • -

#3 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 15-March 12

Re: Passing a pointer between HTML/CSS pages

Posted 15 March 2012 - 04:23 PM

Thanks, I'm starting to see light at the end of the tunnel.

Another question.

Can I in index.html declare $page_view="index" ;

Then in menu.html declare $page_view="menu" ;

Then in contact.html use an IF/ELSE to filter $page_view for either "index" or "menu"
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,574
  • Joined: 08-June 10

Re: Passing a pointer between HTML/CSS pages

Posted 15 March 2012 - 05:07 PM

Yes, like I say in the post above, you can define a constant in the top of your pages and then use that anywhere in the following code (including included code) to do whatever you need.
<?php
// Page: index.php
define("PAGE_ID", "index");

include "contact.php";


<?php
// Page: menu.php
define("PAGE_ID", "menu");

include "contact.php";


<?php
// Page: contact.php

if (PAGE_ID == "index") {
    echo "This is the index page";
}
else if (PAGE_ID == "menu") {
   echo "This is the menu page.";
}
else {
    echo "This is, apparently, the " . PAGE_ID . " page...";
}


Was This Post Helpful? 0
  • +
  • -

#5 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 15-March 12

Re: Passing a pointer between HTML/CSS pages

Posted 15 March 2012 - 05:31 PM

Sorry for seeming thick,but I'm not use to script languages, I've been an assembler programmer for over 30 years. I appreciate your patience.
I'm am trying to nest the PHP in HTML, but the conditional branch statement is not working. Both buttons are being displayed.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php

   define("PAGE_ID", "index");

?>

<html>
<head>






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php

   define("PAGE_ID", "menu");

?>

<html>
<head>




In contact.html


                <div id="info_buttons">


                   <?php 

                      global "PAGE_ID" ;

                      if (PAGE_ID == "index") {

                         echo <a href="../../index.html"><img src="../../images/button_back.png" alt="Back" /></a>;
                         }

                      else {

                         echo <a href="../menu.html"><img src="../../images/button_back.png" alt="Back" /></a>;
                         }

                   ?>



                </div><!-- ends info_buttons -->




As I said it displays both buttons but only the second one is active
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,574
  • Joined: 08-June 10

Re: Passing a pointer between HTML/CSS pages

Posted 15 March 2012 - 05:52 PM

Okay, there are a couple of issues with your contact.html file.

The first one is that it's called "contact.html". Unless specifically configured to do so, your HTTP server will only execute PHP code in files with a .php extension. (Actually there are a few others, but for the sake of simplicity, lets just stick with .php.) If you view the source of the page in your browsers, no doubt you will see the actual PHP code in there. That's never supposed to happen. - So if you want the PHP code to be executed, rename your file to "contact.php".

The second one is how the echo statements in that file are written. You can not put HTML directly into a PHP block. It must be a part of a string; enclosed in quote marks.
// Wrong
echo <h1>Hello, PHP!</h1>;

// Right
echo "<h1>Hello, PHP!</h1>";




And I'm assuming you are including the contact page into your index and menu pages? (I don't see it in your code though...)
Was This Post Helpful? 1
  • +
  • -

#7 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 15-March 12

Re: Passing a pointer between HTML/CSS pages

Posted 15 March 2012 - 05:56 PM

Thank you for your patience. I'll give it a try.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1