SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variables in a skin function?

    I have the following:
    PHP Code:
    ConnectDB('test');

    $query mysql_query('SELECT id,title,post,author FROM blogentries WHERE id <= 20');

    while(
    $row mysql_fetch_assoc($query)) {
    //At a lost here, I'm trying to get skin_entry() into here so that I can use the html, get it to loop
    }

    function 
    skin_entry () {
        return <<<EOF
        <span border='black 1px solid'>No. $row['id'] : $row['title']<br />$row['post']<br /><u>$row['author']</u></span><br />
    EOF;

    When I use the $row[] variables inside skin_entry() I get an error, is there an alternative to getting html code inside the while loop, apart from having to hard code everything inside (and the need to escape the special characters)?

  2. #2
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the $row variable (array or whatever) mis not in scope within the function skin_entry(). You should either pass it in as a parameter to the function, or set it to be global like this:

    function skin_entry() {
    GLOBAL $row

    ...

    }


    Mike

  3. #3
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can pass the row as an argument to the function.

    PHP Code:
     ConnectDB('test');

    $query mysql_query('SELECT id,title,post,author FROM blogentries WHERE id <= 20');

    while(
    $row mysql_fetch_assoc($query)) {
      echo 
    skin_entry($row);
    }

    function 
    skin_entry ($row) {
        return <<<EOF
        <span border='black 1px solid'>No. $row['id'] : $row['title']<br />$row['post']<br /><u>$row['author']</u></span><br />
    EOF;} 
    However, I do not think that a function is necessary in this case, you could just echo the data directly from the loop. But hey, it's your script.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    Hi,

    You can pass the row as an argument to the function.

    PHP Code:
     ConnectDB('test');

    $query mysql_query('SELECT id,title,post,author FROM blogentries WHERE id <= 20');

    while(
    $row mysql_fetch_assoc($query)) {
      echo 
    skin_entry($row);
    }

    function 
    skin_entry ($row) {
        return <<<EOF
        <span border='black 1px solid'>No. $row['id'] : $row['title']<br />$row['post']<br /><u>$row['author']</u></span><br />
    EOF;} 
    However, I do not think that a function is necessary in this case, you could just echo the data directly from the loop. But hey, it's your script.

    Yours, Erik.
    Thanks Erik! For some reason it still doesn't work!

    I was thinking because if I were to echo the data directly inside the loop, then I'll have to escape all the characters (such as ' ") right? It would be pretty tiring having to echo a whole load of html!

  5. #5
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can do that in your while loop too.

    PHP Code:
     ConnectDB('test');

    $query mysql_query('SELECT id,title,post,author FROM blogentries WHERE id <= 20');

    while(
    $row mysql_fetch_assoc($query)) {
      echo <<<EOF
        <span border='black 1px solid'>No. $row['id'] : $row['title']<br />$row['post']<br /><u>$row['author']</u></span><br />
    EOF;

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    Hi,

    You can do that in your while loop too.

    PHP Code:
     ConnectDB('test');

    $query mysql_query('SELECT id,title,post,author FROM blogentries WHERE id <= 20');

    while(
    $row mysql_fetch_assoc($query)) {
      echo <<<EOF
        <span border='black 1px solid'>No. $row['id'] : $row['title']<br />$row['post']<br /><u>$row['author']</u></span><br />
    EOF;

    Yours, Erik.
    It gives this error:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\everything\wamp\www\blogindex.php on line 17

  7. #7
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I don't use the heredoc syntax very often . Try the following:

    PHP Code:
     ConnectDB('test');

    $query mysql_query('SELECT id,title,post,author FROM blogentries WHERE id <= 20');

    while(
    $row mysql_fetch_assoc($query)) {
      echo <<<EOF
        <span border='black 1px solid'>No. {$row['id']} : {$row['title']}<br />{$row['post']}<br /><u>{$row['author']}</u></span><br />
    EOF;} 
    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for wasting your time, but it's really weird when it outputs this error:
    Parse error: syntax error, unexpected T_SL in c:\everything\wamp\www\blogindex.php on line 16
    Once again sorry for troubling!

  9. #9
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It seems like the closing brace must be positioned on a line of its own. That is the only thing that I can think of when looking at the code... Are you sure that it is the posted piece of code that causes the error?

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's solved! Finally. It turns out that there was one little space after the <<< EOF line. Thanks Erik!

  11. #11
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    A space... Invisible sucker.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.


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
  •