1

I need to be able to style some simple HTML I've written in PHP

PHP:

if ($result-> num_rows> 0) {
  readfile("ViewReturn.html");
  while($row = $result-> fetch_assoc()) {
    echo "Bird Name: ".$row["pageLink"]. "<br><br>";
  }
} else {
  echo "0 results";
}

I would like to be able to style the HTML I'm outputting at "birdname" and "page link" because it doesn't sit very well on the page. Does anyone know how to go about doing this?

2
  • 9
    Same way as you'd style any html. Give it some class, id, whatever and style it Commented Mar 17, 2016 at 14:46
  • At some point you can probably look into templating. It can save you lots of grief as your application grows. Commented Mar 17, 2016 at 15:14

4 Answers 4

1

You could add the bird name in a <span> with a class and style the class using CSS.

Do it as below

 echo "<span class='birdname'>Bird Name: ".$row["pageLink"]. "</span><br><br>";

ie, replace your code as

if ($result-> num_rows> 0) {
  readfile("ViewReturn.html");
  while($row = $result-> fetch_assoc()) {
    echo "<span class='birdname'>Bird Name: ".$row["pageLink"]. "</span><br><br>";
  }
} else {
  echo "0 results";
}

and in CSS, add

.birdname{
   color:red;
}
Sign up to request clarification or add additional context in comments.

9 Comments

class="birdname" will cause a server error. Change to single quotes
...or escape it with \"
oops..sorry..my mistake..thanks for pointing out @MarcosPérezGude ..updated the answer..
You forgot to update the quotes around class="birdname" in the second block of code
echo '<span class="birdname">Bird Name: '.$row['pageLink']. '</span><br /><br />';
|
1

You can add to your echo command the html elements just as you were writing directly on HTML file. For example:

echo "<div class='some-class'>"
while($row = $result-> fetch_assoc()) {
echo "<someTag class='other-class' >". "Bird Name: ".$row["pageLink"]. "<br><br>" . "</someTag>";
}
echo "</div>"

Comments

1
<?php
     if ($result-> num_rows> 0) {
        readfile("ViewReturn.html");
        while($row = $result-> fetch_assoc()) {
?>
        <span class="birdname">Bird Name: <?php echo $row['pageLink']; ?> </span>
        <br><br>
<?php
        }
      } else {
         echo "0 results";
     }  
?>

Comments

1

You can style your result by adding these elements Like

 echo "<p class='birdy'>Bird Name: " . $row["pageLink"] . "</p >";

then in your CSS file or inline or in STYLE

.birdy{
   color:blue;
   text-decoration: none;
}

2 Comments

The same problem. Your double quotes will produce a server error.
use single qoutes then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.