SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Echo Joke Title and Joke Text

    Hi there

    I am following along with the PHP Novice to Ninja book, and I am adapting the project to a blog application. So instead of just submitting joketext, I have successfully added additional code and a table in the MySQL database to accept a joketitle too. I have also created a new directory to list the jokes with an index.php which includes a listjokes.html.php where (like a blog) I list all the entries. The code that pulls the date, title and text looks like this:

    PHP Code:
    try
    {
      
    $sql 'SELECT jokedate, joketitle, joketext FROM joke';
      
    $result $pdo->query($sql); //use the $pdo to connect to the database and pass in the sql query.  
    }
    catch (
    PDOException $e)
    {
      
    $error 'Error fetching jokes: ' $e->getMessage();
      include 
    'error.html.php';
      exit();
    }

    while (
    $row $result->fetch()) //we are using the fetch method of the PDO to return the rows as an array.  The loop will return all the joke titles and will stop when condition is false
    {
      
    $jokes[] = $row['jokedate'] . $row['joketitle'] . $row['joketext'];
    }

    include 
    'listjokes.html.php'
    However I am unsure how to display the date <br> title(in h2) another <br> then the text and then a line to separate each entry?

    I need to echo the date, then the title and then the joketext and this is what I've got so far:

    PHP Code:
    <body>
         <?php foreach ($jokes as $joke): ?> <!-- a reference to the $jokes[] array in the controller -->
          <blockquote>
            <p>
              <?php echo htmlspecialchars($joke['jokedate'} . $joke['joketitle'] . $joke['joketext'], ENT_QUOTES'UTF-8'); ?><!--echo each joke title and text  -->
            </p>
          </blockquote>
        <?php endforeach; ?>
      </body>
    I do understand that for specific formatting I will need to apply CSS classes etc to make it prettier, but I think the layout of the data is the first things

    Hope someone can help
    Cheers
    Volterony

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    You can concatenate literal strings with variables.
    PHP Code:
    <?php echo $joke['jokedate'] ."<br />".$joke['joketitle'] ."<br />"$joke['joketext']; ?>
    Also you have a typo on the jokedate variable's closing bracket.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    You can concatenate literal strings with variables.
    PHP Code:
    <?php echo $joke['jokedate'] ."<br />".$joke['joketitle'] ."<br />"$joke['joketext']; ?>
    Also you have a typo on the jokedate variable's closing bracket.
    Thanks for replying StarLion. I've tried your suggestion:

    PHP Code:
     <?php foreach ($jokes as $joke): ?> <!-- a reference to the $jokes[] array in the controller -->
          <blockquote>
            <p>
              <?php
                
    echo $joke['jokedate'] . "<br>" $joke['joketitle'] . "<br>" .$joke['joketext'];
              
    ?><!--echo each joke title in the database -->
            </p>
          </blockquote>
        <?php endforeach; ?>
    However I get and illegal string offset warning for all three array items: jokedate, joketitle and joketext.

    I read that I need to check that $joke is an array to prevent this errror, so do precede the block with:

    PHP Code:
     if (is_array($joke)) ........
    <?php foreach ($jokes as $joke): ?>  etc etc
    Cheers
    volterony

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Well the problem there is your construction of your jokes array.
    PHP Code:
    while ($row $result->fetch()) 

      
    $jokes[] = $row['jokedate'] . $row['joketitle'] . $row['joketext']; 

    This creates a string out of the jokes. I -think- what you're trying to do here instead is make an array out of the joke:
    PHP Code:
    while ($row $result->fetch()) 

      
    $jokes[] = array('jokedate' => $row['jokedate'], 'joketitle' => $row['joketitle'], 'joketext' => $row['joketext']); 

    Which will give you a multidimensional $jokes array that you can then navigate with a foreach as you are trying to do.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    StarLion, yes I now see what I've done there. Thanks for noticing mistake.

    It displays correctly now.

    Many thanks
    Volterony


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
  •