简体   繁体   中英

echo a table style with variable text (depending on conditions) without repeating table style

so, I am trying to echo error warnings such ass (incorrect password/email),(fill in all fields) with the same styles but just differnt text but I cant seem to find a shortcut to do this without echoing the whole style schpiel in every echo, I'm sure this is painfully obvious for most people so please help me out. TVM .heres the code:

   if ($oldpassword!==$oldpassworddb)
     { echo"<head>

<style type='text/css'>
.tab2

{
   width:400px; height:40px;
   position: absolute; right: 300px; top: 70px;
}
 .td2
{
    background-color:pink;
    color:blue;
    text-align:center;
}

</style>
</head>
<body>
<table class='tab2'>
<td class='td2'>first meggage</td>
</table>
</body>";}

else if (strlen($newpassword)>25||strlen($newpassword)<6)
   {echo "what should I put in here!!! ">second message;}

You're approaching this incorrectly. Avoid intermixing your PHP logic and your output HTML.

Determine which message to display first before outputting anything , and store it in a variable. Then output all the HTML with the variable in place. This allows you to define any other variables you need ahead of time as well, and insert them all into the output simultaneously.

<?php
// First define the $message variable
$message = "";
if ($oldpassword!==$oldpassworddb) {
  $message = "first message";
}
else if (strlen($newpassword)>25||strlen($newpassword)<6) {
  $message = "Second message";
}
else {
  // some other message or whatever...
}
Close the <?php tag so you can output HTML directly
?>

Then output the HTML (don't forget a DOCTYPE!)

<!DOCTYPE html>
<head>

<style type='text/css'>
.tab2

{
   width:400px; height:40px;
   position: absolute; right: 300px; top: 70px;
}
 .td2
{
    background-color:pink;
    color:blue;
    text-align:center;
}

</style>
</head>
<body>
<table class='tab2'>
<!-- the PHP variable is inserted here, using htmlspecialchars() in case it contains <>&, etc -->
<td class='td2'><?php echo htmlspecialchars($message); ?></td>
</table>
</body>

It is not considered a good modern practice to use <table> for layout, and it would be better to move your CSS into an external .css file linked via a <link rel='stylesheet' src='yourcss.css'> tag in the <head> , but you should first address your PHP issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM