Hello, im trying to add data to the table with heredoc, but i get an error:

Undefined variable: letter_details

My search of this kind of error was without any results, thats why i registered here.

Here is part of the code:

<?php

                    while($row=mysql_fetch_array($result)){
                        $letter_title = $row['letter_title'];
                        $letter = $row['letter'];
                        $time_added = $date = date('Y-m-d H:i:s', $row['time_added']);
                        $letter_details .=<<<EOD
                        <tr>
                            <td>$letter_title</td>
                            <td>$letter</td>
                            <td>$time_added</td>
                        </tr>
                        }
EOD;
                        }  

                      ?> 

I made it to work in the other way, but i guess using heredoc would be better.

<?php
                        while($row=mysql_fetch_array($result)){
                        echo "<tr><td>";
                        ?>
                        <a href="previewMotivation.php">
                        <?php
                        echo $row['letter_title'];
                        ?>
                        </a>
                        <?php
                        echo "</td><td>";
                        echo $row['letter'];
                        echo "</td><td>";
                        $date = date('Y-m-d H:i:s', $row['time_added']);
                        echo $date;
                        echo "</td><td>";                      
} ?>

    EDIT: Welcome to PHPBuilder!

    Tom10;11000339 wrote:

    Hello, im trying to add data to the table with heredoc, but i get an error:

    Undefined variable: letter_details

    That would make sense, based upon the code snippet you've shown us. Where do you define/initialize $letter_details before you attempt to concatenate strings onto its previous value inside the while() loop?

    Tom10;11000339 wrote:

    I made it to work in the other way

    What about it didn't "work" when you were using the heredoc syntax? Also, note that "the other way" you're talking about does something completely different - rather than storing the HTML in a variable, it simply outputs it directly. In other words, you're comparing apples to oranges.

    Tom10;11000339 wrote:

    i guess using heredoc would be better.

    Actually even better (IMHO) would be to separate the business logic from the presentation markup. In other words, PHP files contain PHP code, not a mix of various HTML documents or stray markup strewn throughout the script. 😉

      Also note:

                                  $letter_details .=<<<EOD 
                                  <tr> 
                                      <td>$letter_title</td> 
                                      <td>$letter</td> 
                                      <td>$time_added</td> 
                                  </tr> 
                                  } 
      EOD;                        }   

      EOD; needs to be on its own line with NO whitespace or other mark up of any kind.

      Edit: Apparently this advice doesn't help, as its already being followed. Re: BG's post below.

        Derokorian;11000342 wrote:

        EOD; needs to be on its own line with NO whitespace or other mark up of any kind.

        It actually already is. Don't ask me why, but vBulletin apparently gobbled up the line break after it when rendering the OP's post body.

        If you quote his post (so that his raw post text gets inserted into your edit box), you'll see that the code looks a bit different (namely, "EDO;" and "}" no longer share the same line).

          Thanks for fast answer. I am totally new to php, thats why my code looks like a mess 🙂

          I was trying to make something with heredoc by looking to an example(here it is, copying from pdf, it may look without formating):

          <?php
          $link = mysql_connect(“servername”,”username”,”userpassword”) or
          die(mysql_error());
          mysql_select_db(“wiley”) or die (mysql_error());
          
          $query = “SELECT
          movie_name,
          movie_director,
          movie_leadactor
          FROM
          movie”;
          $result = mysql_query($query,$link) or die(mysql_error());
          $num_movies = mysql_num_rows($result);
          
          $movie_header =<<<EOD
          <h2><center>Movie Review Database</center></h2>
          <table width=’70%’ border=’1’ cellpadding=’2’ cellspacing=’2’ align=’center’>
          <tr>
          <th align=’left’>Movie Title</th>
          <th align=’left’>Movie Director</th>
          <th align=’left’>Movie Lead Actor</th>
          </tr>
          EOD;
          
          while($row = mysql_fetch_array($result))
          {
          $movie_name = $row[‘movie_name’];
          $movie_director = $row[‘movie_director’];
          $movie_leadactor = $row[‘movie_leadactor’];
          $movie_details .=<<<EOD
          <tr>
          <td>$movie_name</td>
          <td>$movie_director</td>
          <td>$movie_leadactor</td>
          </tr>
          EOD;
          }
          $movie_details .=<<<EOD
          <tr>
          <td>&nbsp;</td>
          </tr>
          <tr>
          <td>Total :$num_movies Movies</td>
          </tr>
          EOD;
          
          

          Thats it, i do not see movie_details initialization before EOD, so thats why i havent made one too.. 🙂

          I understand that using heredoc stores everything in a variable, and echo only prints out, but im not sure what i really need to use for adding information to the page.

          Coding manner causes me big considerations too, because i dont know what is better, and how make things this way 🙂 Im using 'John Wiley & Sons - Beginning PHP, Apache, MySQL Web Develop' as the reference to enter php world 🙂

            Tom10;11000347 wrote:

            Thats it, i do not see movie_details initialization before EOD, so thats why i havent made one too.. 🙂

            Ah, so it's actually the example that was written poorly. Well in that case, note that you should properly initialize the variable, otherwise it won't exist in the first iteration of the loop when PHP tries to concatenate (that's what the ".=" operator does) a value onto its previous one. For example, you could set the variable equal to an empty string ('') just before that loop.

            Tom10;11000347 wrote:

            I understand that using heredoc stores everything in a variable

            No, it doesn't. The heredoc syntax is just another way of delimiting a string - it performs the same role as single or double quotes, in other words.

              bradgrafelman;11000360 wrote:

              Ah, so it's actually the example that was written poorly. Well in that case, note that you should properly initialize the variable, otherwise it won't exist in the first iteration of the loop when PHP tries to concatenate (that's what the ".=" operator does) a value onto its previous one. For example, you could set the variable equal to an empty string ('') just before that loop.

              hmm, i just set the value as you said, before loop. Now it wont generate any errors, unfortunately, it do not print out anything.

              bradgrafelman;11000360 wrote:

              No, it doesn't. The heredoc syntax is just another way of delimiting a string - it performs the same role as single or double quotes, in other words.

              But moreover, it enable html tags to work in php tag?

              And btw, i have a question. If i get more problems in my coding, where should ir create threads? In general help or coding section?

                Tom10;11000377 wrote:

                hmm, i just set the value as you said, before loop. Now it wont generate any errors, unfortunately, it do not print out anything.

                In the last code you showed us, you still haven't added a way to output $movie_details, you simply create the string you never output it. See [man]echo[/man] and [man]print[/man]

                  Tom10;11000377 wrote:

                  hmm, i just set the value as you said, before loop. Now it wont generate any errors, unfortunately, it do not print out anything.

                  Can you show us more of the actual code you're using?

                  Tom10;11000377 wrote:

                  But moreover, it enable html tags to work in php tag?

                  No. PHP doesn't care what you output, be it HTML, plain text, binary data, or anything else.

                  There is no difference at all between a string defined using heredoc syntax vs. one defined using normal quotes (or the nowdoc syntax). In the end, a string is a string is a string.

                  Tom10;11000377 wrote:

                  And btw, i have a question. If i get more problems in my coding, where should ir create threads? In general help or coding section?

                  Yes, no, and it depends. Post whatever you want wherever it seems most appropriate. 🙂

                    Really, i don't know what is bad here 🙂

                    <?php
                    include("include/session.php");
                    ?>
                    <html>
                        <head>  
                    <meta http-equiv="X-UA-Compatible" content="IE=9; text/html; charset=utf-8"/> <title>Motyvacinis laiškas</title> <link href="include/styles.css" rel="stylesheet" type="text/css" /> </head> <body> <table class="center" > <tr><td> <img src="pictures/top.png"/> </td></tr><tr><td> <?php //Jei vartotojas prisijung&#281;s if ($session->logged_in) {
                    include("include/meniu.php"); ?> <table><tr><td> Atgal &#303; [<a href="index.php">Pradžia</a>] </td></tr></table>
                    <br> <div style="text-align: center;color:green">
                    <h1>Motyvacinis laiškas</h1>
                    <a href="createMotivation.php">Sukurti</a>
                    </div> <?php $username = $_SESSION['username']; //$sql = "SELECT `letter_title`,`letter`,`time_added` FROM letters LIMIT 0, 30 "; $sql = "SELECT `username`, `letter_title`, `letter`, `time_added` FROM `letters` WHERE `username` = '$username' LIMIT 0, 30 "; $result=mysql_query($sql) or die(mysql_error()); ?> <table border="1" cellpadding="5" cellspacing="5" width="100%"> <tr> <th>Žymeti</th>
                    <th>Antrašte</th> <th>Laiško turinys </th> <th>Ikelimo laikas</th> </tr> <?php $letter_details = ''; while($row=mysql_fetch_array($result)){ $letter_title = $row['letter_title']; $letter = $row['letter']; $time_added = $date = date('Y-m-d H:i:s', $row['time_added']); $letter_details .=<<<EOD <tr> <td>$letter_title</td> <td>$letter</td> <td>$time_added</td> </tr> EOD; } //Jei vartotojas neprisijung&#281;s, rodoma prisijungimo forma //Jei atsiranda klaid&#371;, rodomi pranešimai. } else { include("include/loginForm.php"); } include("include/footer.php"); ?> </table> </body> </html>

                      I see you're building up a string in $letter_details in the loop ... but I don't see you do anything with that string after you've built it.

                        That was the case... I thought by writing this part, i am already not just storing data, but displaying it too! 🙂

                        <tr>
                                                        <td>$letter_title</td>
                                                        <td>$letter</td>
                                                        <td>$time_added</td>
                                                    </tr>  

                        After end of EOD; i just wrote echo $letter_details; and it appeared... Thanks, i guess this is what i was looking for. Unless, you can suggest me something different than echo $letter_details, it would be interesting 🙂

                          If you just want to echo it out, then why not just echo it out in the first place? Why do you even bother to create $letter_details at all if you have no other use for that variable other than to just echo its contents when you're done?

                            Im trying to learn new things. Take a look at my first post. In second code snippet i post solution, how i made it to just display data with echo, in the table. Im beginner in php, so this code is written only with logic and looks like a mess. Example with heredoc is much more tidy.

                            This is why i choose heredoc, is it wrong? Or i could rearange my second code snippet to be not so messy?

                              I never said not to use heredoc - I just suggested not storing the resulting string in a variable and instead simply echo/print it.

                              In other words, why do this:

                              $string = 'Hello world!';
                              echo $string;

                              rather than this:

                              echo 'Hello world!';

                              ?

                                yes, i understand what you are saying 🙂 with heredoc, i create new variables instead of echoing them directly. I guess the main reason why im doing this is just because of manner.

                                                        echo "</td><td>";
                                                        echo $row['letter'];
                                                        echo "</td><td>";
                                

                                I do not feel this kind of look, maybe im strange 🙂))

                                  Tom10;11000421 wrote:

                                  with heredoc, i create new variables instead of echoing them directly.

                                  Yes, but why do you do that? Why not just echo the heredoc string directly just like you're doing in your non-heredoc example?

                                  EDIT: In other words, to make my previous example more relevant, why do you do this:

                                  $string = <<<EOD
                                  Hello world!
                                  EOD;
                                  
                                  echo $string;
                                  

                                  rather than this:

                                  echo <<<EOD
                                  Hello world!
                                  EOD;
                                  
                                  Tom10;11000421 wrote:

                                  I guess the main reason why im doing this is just because of manner.

                                                          echo "</td><td>";
                                                          echo $row['letter'];
                                                          echo "</td><td>";
                                  

                                  Well yes, that is a bit tedious. I'd much rather see:

                                                          echo "</td><td>$row[letter]</td><td>";

                                    There are two reasons:
                                    1. In the book i mentioned before, there was few sentences saying, that its better to store value in the variable (when taking it with $row[]), because in some situations (could not remember exact ones) values can be lost.
                                    2. Second one, and i guess, the main one, is that i found this in the example of the same book.

                                    All in all, you helped me a lot! This headache is over! Going to have way much more of them🙂 Thanks for all.

                                      Write a Reply...