How to enter HTML code in PHP

Asked

Viewed 763 times

1

I have the following PHP code:

<?php

    include("config.php");

        if(!isset($_SESSION['user'])){
            echo "you are not logged in,please click here to <a href='memberarea.html'>Login</a>";
        } else{

     $query = $_GET['query']; 


        $min_length = 3;


        if(strlen($query) >= $min_length){ 

            $query = htmlspecialchars($query); 


            $query = mysqli_real_escape_string($conn,$query);

          $row_results = mysqli_query($conn,"SELECT * FROM books WHERE `Title` LIKE '%".$query."%' OR `category` LIKE '%".$query."%'OR `category` LIKE '%".$query."%'") or die(mysqli_error($conn));


            if(mysqli_num_rows($row_results) > 0){ 

                while($results = mysqli_fetch_array($row_results)){

                    echo "<span>".$results['Title']."</span>"
                        "</h3>".$results['category']."</p>";

                }
            }
            else{ 
                echo "No results";
            }

        }
        else{ 
            echo "Minimum length is ".$min_length;
        }
        }
    ?>

And in the echo I want to customize using HTML and CSS. my question and how to enter HTML code in PHP?

  • 1

    You already do it there: echo "<span>".$results['Title']."</span>" (incidentally, there is one missing . at the end of this line to concatenate with the next string). And here: "</h3>".$results['category']."</p>"; you are closing an element h3 and a p that don’t make much sense.

  • is not working we can talk in chat sff?

3 answers

3

just put you in the same echo

echo "<head>";
echo "<link rel='stylesheet' type='text/css' href='mystyle.css'>";
echo "</head>";
echo "<a class='classeexemplo'>TEXTO COM EFEITO CSS</a>";

but it is also possible to make the page in html and only use php code in specific places

<table>
 <?php gerarTabela(); ?>
</table>

2

There are several ways in the echo customize using HTML and CSS

Examples:

1 - directly in the file itself

<head>
   <link rel='stylesheet' type='text/css' href='estilo.css'>
</head>

<?php

    if(!isset($_SESSION['user'])){
         echo "<span class='notLogged' you are not logged in,please click here to</span> <a href='memberarea.html'>Login</a>";

    } else{
     ............

css style.

 a {
   text-decoration : none;
   .........
 } 
 .notLogged {
   ........
 }

2 - with include

<?php

include("config.php");

if(!isset($_SESSION['user'])){
    //pagina em html
    include("not_logged.html");
} else{
 ............

config.php

echo "<link rel='stylesheet' type='text/css' href='estilo.css'>";

not_logged.htm

 <span class='notLogged' you are not logged in,please click here to</span> <a href='memberarea.html'>Login</a>

1


In addition to the other answers, which are correct, you can use the includewith the php.

        ...
        if(mysqli_num_rows($row_results) > 0){ 

            while($results = mysqli_fetch_array($row_results)){

                echo "<span>".$results['Title']."</span>"
                    "</h3>".$results['category']."</p>";

            }
        }
        else{ 
            include("erro_no_found.html"); // coloquei aqui para exemplificar
        }
        ...

Just below the error page (erro_no_found.html):

<h1>Não encontramos resultados para a sua pesquisa</h1>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.