SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    35
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I want to add links to Title, name, and format. I was able to add links to all three but when I click on them they only open the page linked to format when I want the links for Title and name to point to separate pages. Can you tell me where my error is? Here is part of the code for index.php:

    Code:
    try
    {
      $sql = 'SELECT titles.id, 
      titles.Title, 
      directors.name,
      titles.link, 
      directors.link, 
      premieres.date, 
      availability.format, 
      availabilitylinks.link 
      FROM titles 
      INNER JOIN directors 
      ON directorid = directors.id
      INNER JOIN premieres 
      ON dateid = premieres.id
      INNER JOIN availability 
      ON availabilityid = availability.id
      INNER JOIN availabilitylinks
      ON availabilitylinkid = availabilitylinks.id';
      $result = $pdo->query($sql);
    }
    catch (PDOException $e)
    {
      $error = 'Error fetching titles: ' . $e->getMessage();
      include 'error.html.php';
      exit();
    }
    
    foreach ($result as $row)
    {
    $titles[] = array(
    'id' => $row['id'],
    'text' => $row['Title'],
    'titles.link' => $row['link'],
    'name' => $row['name'],
    'directors.link' => $row['link'],
    'date' => $row['date'],
    'format' => $row['format'],
    'availabilitylinks.link' => $row['link']   
    ); 
    
    }
    
    include 'films.html.php';
    And here is part of films.html.php:

    Code:
    <?php foreach ($titles as $film): ?>
          <form action="?deletefilm" method="post">
            <blockquote>
              <p>
                <a href="<?php
                echo htmlspecialchars($film['titles.link'], ENT_QUOTES,
                    'UTF-8'); ?>"><?php
                echo htmlspecialchars($film['text'], ENT_QUOTES, 'UTF-8'); ?></a>
                <input type="hidden" name="id" value="<?php echo $film['id']; ?>">
                <input type="submit" value="Delete">
                (by <a href="<?php
                echo htmlspecialchars($film['directors.link'], ENT_QUOTES,
                    'UTF-8'); ?>"><?php
                echo htmlspecialchars($film['name'], ENT_QUOTES,
                    'UTF-8'); ?></a>) &nbsp; Premiered <?php
                echo htmlspecialchars($film['date'], ENT_QUOTES,
                    'UTF-8'); ?> &nbsp; 
                    <a href="<?php
                echo htmlspecialchars($film['availabilitylinks.link'], ENT_QUOTES,
                    'UTF-8'); ?>"><?php
                echo htmlspecialchars($film['format'], ENT_QUOTES,
                    'UTF-8'); ?></a>
              </p>
    Last edited by cpradio; Oct 27, 2013 at 03:11.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,082
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You should do a print_r of $row to see what data is available. I'm guessing you should qualify all link rows with a special name and then use that name, i.e.

    Code:
    SELECT
       titles.id
     , titles.Title
     , directors.name
     , titles.link AS title_link
     , directors.link AS directors_link
     , premieres.date
     , availability.format
     , availabilitylinks.link AS availability_link
    FROM titles
      INNER
       JOIN directors
         ON directorid = directors.id
      INNER
       JOIN premieres 
         ON dateid = premieres.id
      INNER
       JOIN availability 
         ON availabilityid = availability.id
      INNER
       JOIN availabilitylinks
         ON availabilitylinkid = availabilitylinks.id
    and then use title_link, directors_link and directors_link in your code. (it's been a while since I worked with MySQL so I might be off here).

    Also, I took the liberty to clean up your code a bit, hope you don't mind

    PHP Code:
    <?php
    function escape($str)
    {
        return 
    htmlspecialchars($strENT_QUOTES'UTF-8');
    }
    ?>

    <?php foreach ($titles as $film): ?>
        <blockquote>
            <p>
                <a href="<?php echo escape($film['titles.link']) ?>">
                    <?php echo escape($film['text']) ?>
                </a>
                <form action="?deletefilm" method="post">
                    <input type="hidden" name="id" value="<?php echo $film['id']; ?>">
                    <input type="submit" value="Delete">
                </form>
                (by
                    <a href="<?php echo escape($film['directors.link']) ?>">
                        <?php echo escape($film['name']) ?>
                    </a>
                )
                Premiered
                <?php echo escape($film['date']) ?>
                <a href="<?php echo escape($film['availabilitylinks.link']) ?>">
                    <?php echo escape($film['format']) ?>
                </a>
            </p>
        </blockquote>
    <?php endforeach; ?>
    The <form> actually must be inside the loop, otherwise you can get very interesting results (deleting movies you didn't want deleted)

    The escape function I defined should be defined globally in your project somewhere, not repeated in each and every template.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    35
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You should do a print_r of $row to see what data is available. I'm guessing you should qualify all link rows with a special name and then use that name.
    Thanks! Giving each of the link columns a unique name solved the problem. I didn't make the other changes you suggested yet because I'm just about to read Chapter 6 of Kevin Yank's PHP and My SQL which is all about code structure.


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
  •