我是初学者
我想以表格形式显示来自数据库的逗号分隔值
从下面的图片链接中,我们可以看到数据是如何以逗号分隔的:
我想在html / php页面中以表格形式显示,如下所示:
这是我的html页面: –
<form action="insert.php" method="POST">
<div id="items">
<input type="text" placeholder="name" name="user_name[]" />
<input type="text" placeholder="email" name="user_email[]" />
</div>
<input type="button" value="add entry" id="add"/>
<input type="submit" value="submit"/>
用于添加其他输入的Javascript文件: –
$(document).ready(function(){
$("#add").click(function (e){
event.preventDefault()
$('#items').append('<div><input type="text" placeholder="Name" name="user_name[]" ><input type="text" placeholder="email" name="user_email[]">'
+'<input type="button" value="delete" id="delete"/></div>');
});
$('body').on('click','#delete',function(e){
$(this).parent('div').remove();
});
});
以下是插入数据库的PHP代码: –
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dynamic", $con);
$user_name = $_POST["user_name"];
$value = implode(',', $user_name);
$user_email = $_POST["user_email"];
$valueone = implode(',', $user_email);
$sql="INSERT INTO dynamicdata (user_name, user_email)
VALUES
('$value','$valueone')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
解决方法:
只需从表中获取数据并爆炸用户名和useremail来制作数组,然后循环它并显示值.
下面粘贴的代码可能对您有所帮助.
<?php
$ArrUserName=explode($username,','); //exploding the coma separated username to make array of user names
$ArrUserEmail=explode($username,',');//exploding the coma separated user email to make array of user email
echo '<table><tr><td>Name</td><td>EMAIL</td></tr>';
for($i=0;$i<count($ArrUserName);$i++){
echo "<tr>";
echo"<td>".$ArrUserName[$i]."</td>"; //display user name
echo"<td>".$ArrUserEmail[$i]."</td>"; // diaplay user email
echo"</tr>";
}
?>
请理解这一点,并尝试在您的代码中实现.