Home > Software engineering >  Style is not being applied except for the first row from sql query in html code
Style is not being applied except for the first row from sql query in html code

Time:06-04

I'm retrieving all the rows from sql database through a query, and i'm using a loop to get all the rows I have a style.css file which i'm specifying a style for the , however, only the first row is receiving the style and the style is not being applied to the rest.

<!DOCTYPE html>
<html>
<link href="styles.css" rel="stylesheet">
<head>
    <title>IIACSS search center</title>
</head>
<body>

<form method="post">
<label>Search</label>
<input type="text" name="search">
<input type="submit" name="submit">
</form>

<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");

$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
  <tr>
    <th>Project</th>
    <th>Question</th>
    <th>Sample</th>
  </tr>
  <?php
  for ($a=0;$row = $sth->fetch();$a  ) {
  ?>
        <tr>
            <td><?php echo $row->Proj_Name; ?></td>
            <td><?php echo $row->Question;?></td>
            <td><?php echo $row->sample;?></td>
            <br><br>
        </tr>
    </table>

    <?php
  }
  }
  ?>
body {
  background-color: white;
}

h1 {
  color: black;

}

td {
  color: black;
  font-family: sans-serif;
}
th {
  color: blue;
  font-family: sans-serif;
}

CodePudding user response:

It is not a CSS problem, it is because you are closing your </table> in the loop, so it is ending your table after the first tr.

Just remove it from the loop:

<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");

$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
  <tr>
    <th>Project</th>
    <th>Question</th>
    <th>Sample</th>
  </tr>
  <?php
  for ($a=0;$row = $sth->fetch();$a  ) {
  ?>
        <tr>
            <td><?php echo $row->Proj_Name; ?></td>
            <td><?php echo $row->Question;?></td>
            <td><?php echo $row->sample;?></td>
            <br><br>
        </tr>

    <?php
  } ?>
      </table>
  <?php }
  ?>
  • Related