I don't get it, what am I doing wrong here?

Discussion in 'PHP' started by Mitchell, Jun 22, 2010.

  1. #1
    It retrieves info successfully however I wanted it in a table so I placed it between <<<_END tags. Now it gives me this error.

    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\htdocs\clphp03\new_post09.php on line 118

    line 118 is <td><?php echo $row['title'];?></td>

    Is there something I am missing? Thanks.

    
    $db = dbConnect('query');
        $sql = "SELECT * FROM mammals05 WHERE postid = '$postid'";
        $result = $db->query($sql)or die ($db->error);
        while ($row = $result->fetch_assoc()) {
            echo 
            <<<_END
            <table>
                <tr>
                    <td><?php echo $row['title'];?></td>
                    <td><?php echo $row['email'];?></td>
                </tr>
            </table>
    _END;
            }
    
    PHP:
     
    Mitchell, Jun 22, 2010 IP
  2. hosseintdk775

    hosseintdk775 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In the name of the kind Allah
    Hello,
    you should write like this:
            
                    <td>$row['title']</td>
                    <td>$row['email']</td>
    
    Code (markup):
    you do not need to open php tags again in it, its open and you can put Variables without using php tags again,
     
    hosseintdk775, Jun 22, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your suggestion.

    You may be correct, however I still get the same error at same location.
     
    Mitchell, Jun 22, 2010 IP
  4. new2seoo

    new2seoo Peon

    Messages:
    143
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Replace while with this code.

        while ($row = $result->fetch_assoc()) {
            echo "
            <table>
                <tr>
                    <td>" . $row['title'] . "</td>
                    <td>".  $row['email'] . "</td>
                </tr>
            </table>";
            }
    
    PHP:
    Hope this helps.
     
    Last edited: Jun 22, 2010
    new2seoo, Jun 22, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. That removed the error and the info is displayed properly.

    Is this how it's normally done or is this a work around to a problem with my code? It seams a little complicated.

    I googled and found this, but I am still to new to fully understand it.
    http://www.gidforums.com/t-965.html
     
    Mitchell, Jun 22, 2010 IP
  6. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes. I now see my book echoed it just the way you did.
     
    Mitchell, Jun 22, 2010 IP
  7. bvraghav

    bvraghav Member

    Messages:
    123
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #7
    the problem seems solved here... but for futute reference,

    when you use heredocs... it is just like using "" for enclosing the string, except that we dont have to escape the " with \"

    so your code would look like
    $db = dbConnect('query');
        $sql = "SELECT * FROM mammals05 WHERE postid = '$postid'";
        $result = $db->query($sql)or die ($db->error);
        while ($row = $result->fetch_assoc()) {
            echo 
            <<<_END
            <table>
                <tr>
                    <td>[COLOR="Red"]{[COLOR="Gray"]$row['title'][/COLOR]}[/COLOR]</td>
                    <td>[COLOR="Red"]{[COLOR="Gray"]$row['email'][/COLOR]}[/COLOR]</td>
                </tr>
            </table>
    _END;
            }
    Code (markup):
    check out for the {} used to enclose the variables.
    refer http://www.php.net/manual/en/language.types.string.php

    alternately, you can use printf command instead of echo.
    refer http://php.net/manual/en/function.printf.php
     
    bvraghav, Jun 23, 2010 IP
  8. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks. Your suggestion works and it seems simpler to use. This is going to help me a lot. I will read about it in the link you provided.
     
    Mitchell, Jun 23, 2010 IP