Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i print multiple values separately in php?

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>
like image 525
Naga Naveen Avatar asked Dec 04 '25 10:12

Naga Naveen


2 Answers

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>";
like image 148
Nandan Bhat Avatar answered Dec 06 '25 23:12

Nandan Bhat


Did you simply try:

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

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

like image 40
Realitätsverlust Avatar answered Dec 06 '25 22:12

Realitätsverlust