0

I have the following code, but keep running into a syntax error on line 7, saying "Parse error: syntax error, unexpected 'htmlentities' (T_STRING)." Is this not the correct way to echo php and html at the same time? Any help is appreciated!

<?php
$stmt = $pdo->query("SELECT first_name, last_name, department, website filename FROM Profile");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
  echo('<div class="col-sm-3">
      <img class="img-rounded" src="'.$row['filename'].'"/>
    <p class="caption">');
  echo('<a href="'htmlentities($row['website'])'">'.($row['first_name']).' '.htmlentities($row['last_name']).'</a>')
  echo(', '.htmlentities($row['department']));
  echo('</p> </div>');
}
?>
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
Iz M
  • 393
  • 1
  • 4
  • 17

5 Answers5

0

Your echo should be without parenthesis and you're missing . on a few of your echo commmands.

Teknology
  • 21
  • 2
0

To join an echo string (concatenate) you need to add the strings with a "."

Please use :

<?php

$stmt = $pdo->query("SELECT first_name, last_name, department, website filename FROM Profile");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
   echo '<div class="col-sm-3">
   <img class="img-rounded" src="' . $row['filename'] . '"/>
   <p class="caption">';
   echo '<a href="' . htmlentities($row['website']) . '">' . ($row['first_name']) . ' ' . htmlentities($row['last_name']) . '</a>';
   echo ', ' . htmlentities($row['department']);
   echo '</p> </div>';
}
?>
  • I suggest you use a editor that will give you debugging information. Something like NetBeans. I use NetBeans all the time as my preferred PHP editor. – Armand Groenewald Dec 04 '15 at 05:45
0

You should concatenate your strings when you interrupt the string like

echo('<a href="' . htmlentities($row['website']) . '">'.($row['first_name']).' '

At the htmlentities part you are missing the dots for stringconcatenation

sillysicko
  • 126
  • 7
0

You have concatenated the string in wrong way. Try following

<?php
    $stmt = $pdo->query("SELECT first_name, last_name, department, website, filename 
                    FROM Profile");
    while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) 
    {
        echo '<div class="col-sm-3">
                <img class="img-rounded" src="'.$row['filename'].'"/>
                <p class="caption">';
        echo '<a href="'.htmlentities($row['website']).'">'.$row['first_name'].' '.htmlentities($row['last_name']).'</a>';
        echo ', '.htmlentities($row['department']);
        echo '</p> </div>';
    }
?> 

OR

You can also try the following as an alternative way..

<?php
    $stmt = $pdo->query("SELECT first_name, last_name, department, website, filename 
                    FROM Profile");
    while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) 
    {
        ?>
        <div class="col-sm-3">
            <img class="img-rounded" src="<?php echo $row['filename'] ?>"/>
            <p class="caption">
                <a href="<?php echo htmlentities($row['website']) ?>">
                    <?php echo $row['first_name'].' '.$row['last_name'] ?> 
                </a>
                <?php echo ', '.htmlentities($row['department']); ?>
            </p> 
        </div>
        <?php
    }
?> 
Suyog
  • 2,472
  • 1
  • 14
  • 27
0

I edit the code you try it. You miss . and ;

echo '<div class="col-sm-3">
      <img class="img-rounded" src="'.$row['filename'].'"/>
       <p class="caption">';
  echo '<a href="'.htmlentities($row['website']).'">'.($row['first_name']).' '.htmlentities($row['last_name']).'</a>';
  echo ', '.htmlentities($row['department']);
  echo '</p> </div>';
  
Virendra Nagda
  • 673
  • 5
  • 9