SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again, Need some help with global variables. The coding below is used with PHPLib templates too. I read up on the manual and found that it wasn't the same as in C. But here's what i came up with, which also didn't work. Help Appreciated! Also can i use the global variables in the set_var statement for PHPLIb too?

    Code

    <?
    include("/home/xiccc/www/db_mysql.php");
    $db = New DB_SQL;
    include("/home/xiccc/www/templates/template.inc");
    $t = new Template("/home/xiccc/www/templates/", "keep");

    //Global Variables
    $game = 'Dynasty Warriors';
    $alpha = 'D';
    $table = 'playstationfighting';

    global $game, $alpha, $table;

    $t->set_file('games', 'games.phtml');
    $t->set_block('games', 'Europe1Block', 'E1Block');
    $t->set_block('games', 'Europe2Block', 'E2Block');
    $t->set_block('games', 'Europe3Block', 'E3Block');
    $t->set_var('title', '$game - Playstation Xploder Codes (XploderCodes.Com)');
    $t->set_var('platform', 'Playstation');
    $t->set_var('gametitle', '$game');
    $t->set_var('copyright', 'All Contents Copyright XICCC');

    $sql = "SELECT europedate, europetcodes, europeurl FROM $table WHERE alpha='$alpha' and game='$game'";

    $db->query($sql);

    while ($db->next_record()) {

    $europedate = $db->f(europedate);
    $europetcodes = $db->f(europetcodes);
    $europeurl = $db->f(europeurl);

    $tmp1 = ".$europeurl";
    $t->set_var('europeurl', $tmp1);
    $t->parse('E1Block', 'Europe1Block', true);
    $tmp2 = "$europedate";
    $t->set_var('europedate', $tmp2);
    $t->parse('E2Block', 'Europe2Block', true);
    $tmp3 = "$europetcodes";
    $t->set_var('europetcodes', $tmp3);
    $t->parse('E3Block', 'Europe3Block', true);

    }

    $t->pparse('Output', 'games');

    ?>
    Last edited by XploreR; May 16, 2001 at 00:49.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It seems to me that a variable declared as global in the main block is redundant. A variable in the main block is global by nature. However, a global variable does not have scope inside a function. To be able to use a global variable inside a function, you can use the global keyword inside the function. If you didn't use the global keyword, and instead just used the same variable name, you would create a new variable inside the function that hides the global variable. However, using the global keyword is generally considered bad coding practice because it ruins the modularity of functions: now the function is tied to the main block of code and its value for the variable you reference using the global keyword. If you want to use a variable declared in the main block of your code in a function, you should simply pass the variable to the function as an argument.

    However, all of that information doesn't help you in your predicament. Declaring the variables as global does nothing because they are already global, though you might want to try eliminating that line and see what happens; and it looks like you are correctly passing the variables to the methods of the object, so I don't know why you are having problems. Are you sure you are passing the correct number of arguments? And, do they match the parameters of the methods?

    Uh...oh. I see some problems now. Do you know the difference betwee double quotes and single quotes in php? For a quick lesson try out this code:

    <?php
    $string='world!';
    echo "Hello $string";
    echo 'Hello $string';
    ?>
    Last edited by 7stud; May 16, 2001 at 03:45.

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What Stud7 said. This line (below) is unnecessary and might even cause problems ?!? because your mainline is in global scope already:

    global $game, $alpha, $table;

    Now the problem you have in these two lines,

    $t->set_var('title', '$game - Playstation Xploder Codes (XploderCodes.Com)');
    $t->set_var('gametitle', '$game');

    is that you are using single quotes around $game and you need to use double quotes. Here's the deal:

    Single quotes - php treats the contents literally (more efficient)
    Double quotes - php parses the string and substitutes the values of variables named

    Eg 1:
    $game = 'playstation';
    echo 'game: $game';
    expected result: game: $game

    Eg 2:
    $game = 'playstation';
    echo "game: $game";
    expected result: game: playstation

    Here is a good tutorial on strings in PHP http://www.zend.com/zend/tut/using-strings.php

  4. #4
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As usual, thanks for the explaination and also the url. Got it sorted now


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •