[英]how to display the data in the table from the database?
This code to calculate the age of the date of birth of the database, but the problem is when shown in the table, the location of that age are outside the table, can someone help me, this code 这段代码可以计算数据库的出生日期的年龄,但是问题是在表中显示时,该年龄的位置在表外,有人可以帮我这个代码
<?php
include 'koneksi.php';
$sql=mysql_query("SELECT * FROM pasien");
function umur($tgl_lahir){
$parts = split('-', $tgl_lahir);
$thn_lahir='Year: ' + $parts[0];
$bln_lahir='Month: ' + $parts[1];
$tgl_lahir='Day: ' + $parts[2];
$tanggal_today = date('d');
$bulan_today=date('m');
$tahun_today = date('Y');
$harilahir=gregoriantojd($bln_lahir,$tgl_lahir,$thn_lahir);
$hariini=gregoriantojd($bulan_today,$tanggal_today,$tahun_today);
$umur=$hariini-$harilahir;
$tahun=$umur/365;
$sisa=$umur%365;
$bulan=$sisa/30;
$hari=$sisa%30;
echo floor($tahun).",".floor($bulan);
}
while ($row=mysql_fetch_array($sql)){
echo"<table border='1px'>";
echo "<tr>";
echo "<td width='50px'>".umur($row['tgl_lahir'])."</td>";
echo "<td>".$row['nama']."</td>";
echo "</tr>";
echo"</table>";
}
?>
You're already echo
the result of function umur
. 您已经在echo
显umur
函数的结果了。 But in the function you are doing it one more time. 但是在该功能中,您需要再执行一次。
Change your umur
function last line 更改您的umur
函数的最后一行
echo floor($tahun).",".floor($bulan);
to 至
return floor($tahun).",".floor($bulan);
And move your <table>
and </table>
outside of while
. 并将<table>
和</table>
移到while
之外。 Otherwise you would have a new table for every row. 否则,您将为每一行都有一个新表。
Just move echo"<table border='1px'>";
只需移动echo"<table border='1px'>";
before while
loop and move echo"</table>";
在while
循环之前并移动echo"</table>";
after while
loop. 在while
循环之后。
Update your code to 将您的代码更新为
<?php
include 'koneksi.php';
$sql=mysql_query("SELECT * FROM pasien");
function umur($tgl_lahir)
{
$parts = split('-', $tgl_lahir);
$thn_lahir='Year: ' + $parts[0];
$bln_lahir='Month: ' + $parts[1];
$tgl_lahir='Day: ' + $parts[2];
$tanggal_today = date('d');
$bulan_today=date('m');
$tahun_today = date('Y');
$harilahir=gregoriantojd($bln_lahir,$tgl_lahir,$thn_lahir);
$hariini=gregoriantojd($bulan_today,$tanggal_today,$tahun_today);
$umur=$hariini-$harilahir;
$tahun=$umur/365;
$sisa=$umur%365;
$bulan=$sisa/30;
$hari=$sisa%30;
return floor($tahun).",".floor($bulan);
}
echo"<table border='1px'>";
while ($row=mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td width='50px'>".umur($row['tgl_lahir'])."</td>";
echo "<td>".$row['nama']."</td>";
echo "</tr>";
}
echo"</table>";
It is recommended to use return
instead of echo
in function to return the value. 建议在函数中使用return
而不是echo
来返回值。 You can use echo umur($row['tgl_lahir']);
您可以使用echo umur($row['tgl_lahir']);
to print the returned value of function. 打印函数的返回值。
try this: You need to return the value from the function rather than echo it 尝试以下操作:您需要从函数返回值,而不是回显它
<?php
include 'koneksi.php';
$sql=mysql_query("SELECT * FROM pasien");
function umur($tgl_lahir){
$parts = split('-', $tgl_lahir);
$thn_lahir='Year: ' + $parts[0];
$bln_lahir='Month: ' + $parts[1];
$tgl_lahir='Day: ' + $parts[2];
$tanggal_today = date('d');
$bulan_today=date('m');
$tahun_today = date('Y');
$harilahir=gregoriantojd($bln_lahir,$tgl_lahir,$thn_lahir);
$hariini=gregoriantojd($bulan_today,$tanggal_today,$tahun_today);
$umur=$hariini-$harilahir;
$tahun=$umur/365;
$sisa=$umur%365;
$bulan=$sisa/30;
$hari=$sisa%30;
return floor($tahun).",".floor($bulan);
}
echo"<table border='1px'>";
while ($row=mysql_fetch_array($sql)){
echo "<tr>";
echo "<td width='50px'>".umur($row['tgl_lahir'])."</td>";
echo "<td>".$row['nama']."</td>";
echo "</tr>";
}
echo"</table>";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.