How do i print multiple values separately in php?

时间:2015-04-23 05:28:40

标签: javascript php html

I'm retrieving data from the database using an unique id. Now the problem is, all the values are being retrieved and are being printed in a single line. I want them below one another. I tried "
" "\n
" and also nl2br. If anyone could help me. Thanks in advance.

PHP file:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'DB';
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) 
{
die("connection couldn't be established");
}
if(isset($_POST['EnrNo']) === true && empty($_POST['EnrNo']) === false) {
//$Enr = $_POST['EnrNo'];
$EnrNo = mysql_real_escape_string ($_POST['EnrNo']);
$query = "Select * FROM cert WHERE EnrNo = '$EnrNo'";
    $result = $db->query($query);
    $total_num_rows = $result->num_rows;
    while ($row=$result->fetch_array())
  {
  echo ("EnrNo: " .$row["EnrNo"]);
  echo ("Name: " .$row["Name"]);
  echo ("Batch Code: " .$row["Batch Code"]); 
  echo ("Start Date: " .$row["Start Date"]);
  echo ("End Date: ".$row["End Date"]);
  echo ("Course: " .$row["Course"]);
  echo ("Duration: " .$row["Duration"]);
 }  mysqli_free_result($result);
    } else {
        echo ('Data not found');
    };
?>

HTML file:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
Enr No: <input type="text" name="EnrNo" id="EnrNo" /><br/><br />
<input type="submit" name="retrieve" value="retrieve" id="EnrNo-sub" />
<div id="EnrNo-data"></div>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">

$('input#EnrNo-sub').on('click', function() { 
var EnrNo = $('input#EnrNo').val();
if (EnrNo != '') {
$.post('retrieve.php', {EnrNo: EnrNo}, function(data) { 
    $('div#EnrNo-data').text(data);

});

}


});
</script>
</body>
</html>

7 个答案:

答案 0 :(得分:1)

Did you simply try:

echo ("Duration: " .$row["Duration"]."<br>");

\n is only visible in the source code, while <br> affects the html.

答案 1 :(得分:1)

Try "
", instead of "\n", Example:

 echo "Value1  :".$value1."<br>";
 echo "Value2  :".$value2."<br>";
 echo "Value3  :".$value3."<br>";
 echo "Value4  :".$value4."<br>";
 echo "Value5  :".$value5."<br>";

答案 2 :(得分:1)

You have to use <br> for line breaks and .html() instead of .text().

Otherwise the html will be escaped anyhow.

答案 3 :(得分:1)

Html uses BR tag to insert a single line break.

To have a value printed on a new line, you can add your line break tag <BR /> after every line like this:

echo ("EnrNo: " .$row["EnrNo"]).'<BR />';  

Please Note :

In HTML, the <br> tag has no end tag.

In XHTML, the <br> tag must be properly closed, like this: <br />

答案 4 :(得分:1)

try this

  while ($row=$result->fetch_array())
  {
  echo ("EnrNo: " .$row["EnrNo"]."<br>");
  echo ("Name: " .$row["Name"]."<br>");
  echo ("Batch Code: " .$row["Batch Code"]."<br>"); 
  echo ("Start Date: " .$row["Start Date"]."<br>");
  echo ("End Date: ".$row["End Date"]."<br>");
  echo ("Course: " .$row["Course"]."<br>");
  echo ("Duration: " .$row["Duration"]."<br>");
 }

答案 5 :(得分:0)

试试这个

foreach($result->result() as $row)

{   echo(&#34; EnrNo:&#34;。$ row-&gt; EnrNo。&#34;
&#34;);   echo(&#34;姓名:&#34;。$ row-&gt;姓名。&#34;
&#34;);   echo(&#34; Batch Code:&#34;。$ row-&gt; Batch Code。&#34;
&#34;);   echo(&#34;开始日期:&#34;。$ row-&gt;开始日期。&#34;
&#34;);   echo(&#34;结束日期:&#34;。$ row-&gt;结束日期。&#34;
&#34;);   echo(&#34;课程:&#34;。$ row-&gt;课程。&#34;
&#34;);   echo(&#34;持续时间:&#34;。$ row-&gt;持续时间。&#34;
&#34;);  }

答案 6 :(得分:0)

考虑到你的输出,你可以为你想要打印的字段写一个循环,然后在每一行附加<br>;还要确保正确地转义HTML的输出:

foreach (['EnrNo', 'Name', 'Batch Code', 'Start Date', 'End Date', 'Course', 'Duration'] as $field) {
    echo $field, ': ', htmlspecialchars($row[$field], ENT_QUOTES, 'UTF-8'), '<br>';
}