SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error with eval()

    I would like to be able to set some variables at the beginning of my PHP script or possibly in another file, and then use them globally in all of my scripts and functions within those. How could I acheive this?

    Also, what is the php equivalent of the c++ operator "=="?

    And in addition... If I were to do the following...
    Code:
    <input type="hidden" name="step" value="edit">
    Is there any way to carry the new variable $step into a function within the php script which the form was submitted to?
    Last edited by JordanTLClive; Feb 24, 2001 at 17:18.
    -Jordan

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by JordanTLClive
    I would like to be able to set some variables at the beginning of my PHP script or possibly in another file, and then use them globally in all of my scripts and functions within those. How could I acheive this?
    In C/C++ you can #include files; in PHP you can do the same:

    PHP Code:
    <?php
    inlude
    ("fileWithAllMyVariablesAndFunctions.php");
    ?>
    See http://www.php.net/include for more information.

    Also, what is the php equivalent of the c++ operator "=="?
    It's "==". Exactly the same, but it can be used to compare strings as well as integers and floating point values, so there's no need to use something like strcmp(). For example:

    PHP Code:
    <?php
    if ($name == "Kevin Yank") {
      echo(
    "Hey there, Kev!");
    } else {
      echo(
    "Who the heck are you?");
    }
    ?>
    And in addition... If I were to do the following...
    Code:
    <input type="hidden" name="step" value="edit">
    Is there any way to carry the new variable $step into a function within the php script which the form was submitted to?
    Yup -- you just need to declare the variable as global inside the function:

    PHP Code:
    <?php

    function usesStepVariable() {
      global 
    $step;

      echo (
    $step);
    }

    //...

    usesStepVariable();

    ?>
    This assumes you aren't willing/able to pass the variable in as a parameter as follows:

    PHP Code:
    <?php

    function usesStepVariable($localstep) {
      echo (
    $localstep);
    }

    //...

    usesStepVariable($step);

    ?>
    Hope that answers your questions!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.... actually I was just returning to the post to say that I had gotten everything figured out after a lot of switching around but your post has clarified a few things plus I like the idea of including one file for my mysql information. In the end it is a full front/back end for a biography section to my website which allows me to add/modify/delete biographies as well as display them and it uses the search engine friendly URL's. My next step is to develop the menu system, as well as a mysql backend for the meta tags allowing me to dynamically change those per artist

    And to think... 2 days ago I couldnt code a line of PHP
    -Jordan

  4. #4
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick question, can I write a function in file a, include file a in file b, and call the function in file a from file b?
    example:

    File A
    function Add()
    {
    blah = x + y;
    }

    File B
    include("filea.php")
    Add();
    -Jordan

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Absolutely! That's one of the main uses of include files!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  6. #6
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I want the data in my menu's to be php parsed. How can this be done?

    IE when I call /bios.php/menuname/Artist Menu/artist/Creed
    it should pull up the Artist Menu (which it does)
    go through and see the following line from mysql:
    Code:
    <a href="/bios.php/artist/<? echo $artist ?>">Biography</a>
    and it should replace <? echo $artist ?> with Creed. I have a feeling now I'm getting complicated
    -Jordan

  7. #7
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    -- Removed irrelevent comment
    Last edited by Drinky; Feb 24, 2001 at 01:36.
    Drinky

  8. #8
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Assuming you're already parsing your URL to extract the variable $artist and its value "Creed", then the code you just quoted does everything you need...

    Am I missing something?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  9. #9
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it doesnt seem to work...
    see the URL bios.php/artist/<? echo $artist ?> is actually part of information stored in the mysql database. menu.php is called by menu.php/menuname/Artist%20Menu which tells it to pull up the generic Artist Menu, which has the URL I explained above in it. menu.php returns the entire query via an echo statement. menu.php is then included in bios.php. For one reason or another, <? echo $artist ?> is not changed to Creed (or whatever is used for the artist for that matter), and it remains <? echo $artist ?>.

    To see the code... http://www.hotmusicstars.com/php/bios.txt and http://www.hotmusicstars.com/php/menu.txt

    To see scripts
    http://www.hotmusicstars.com/php/men...u/artist/Creed
    http://www.hotmusicstars.com/php/bios.php/artist/Creed (thats where the menu needs to be included in)
    -Jordan

  10. #10
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I think I figured out what I have to do... dont know how to do it...

    I need to go through the variable $menu word for word and find some sort of special string. I guess anything in []'s and the word(s) within the []'s need to be replaced the data stored in the corresponding variable (retrieved from the URL data) Example if there is [artist] in $menu, it needs to be replaced with the information contained in the variable $artist, just as if [cheezit] was in $menu, it would be replaced with the information contained in $cheezit.
    -Jordan

  11. #11
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you want to evaluate PHP code stored in your database as a string, you'll need to use the eval() function. For example:

    PHP Code:
    <?php
    $artist 
    "Creed";
    $string 'bios.php/artist/$artist';

    $interpretedString = eval('"'.$string.'"');
    ?>
    This strikes me as a really awkward way of doing this, however. Why are you storing URL's in your database...?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  12. #12
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I'm doing is creating an entire site which will be run by PHP. The menus change on different levels of the site and I want to be able to easily update menus via an administratin panel. Also, it will allow me to use an artists name in the query string to create dynamic menus (because there are over 50 artists, and each one's menu is essentially the same other than their name
    -Jordan

  13. #13
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    by the way this seems to be not working:-(

    The following is stored in the database:
    Code:
    This is the generic artist menu<br>
    <a href=/php/bios.php/artist/$artist>Biography</a>
    It is retrieved by menu.php:
    Code:
    function ShowMenu($menuname)
    {
    global $artist;
    $querys = mysql_query("SELECT * FROM menus WHERE menuid='$menuname'");
      if (!$querys) {
        echo "There was an error fetching the $menuname menu as explained by:<br>";
        mysql_error();
        echo "<br>Please try again later!";
        exit();
      }
    
    $results = mysql_fetch_array($querys);
    $menu = $results["menutext"];
    $phpmenu = eval('$menu');
    echo($phpmenu);
    
    }
    There is an error:
    Parse error: parse error in /home/hotmusic/public_html/php/menu.php(212) : eval()'d code on line 1
    Where am I going wrong? LOL

    BTW thanks for all the help that has been given to me so far! I really appreciate it!
    -Jordan

  14. #14
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I decided that I would just write the actual menu in a php script, and do include("menu.php") from the main script. Later on I can create an admin script that will open the menu files rather than extract them from a database. Works for me
    -Jordan

  15. #15
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Probably a good idea.

    For the record, you needed to add the quotes around the string you were evaluating, as per the example I provided in my previous post:

    PHP Code:
    $phpmenu = eval('"'.$menu.'"'); 
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


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
  •