Question: PHP mysql - Display comma separated value from the database in tabular form


PHP mysql - Display comma separated value from the database in tabular form

Answers 2
Added at 2017-01-05 11:01

I am complete beginner I want to display comma separated values from database in tabular form

From the below image link we can see how the data is getting added separated by commas:


I want to display in tabular form in html/php page just like below:

Table image

This is my html page below:-

<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[]" />   

<input type="button" value="add entry" id="add"/>
<input type="submit"  value="submit"/>

Html page

Javascript file for adding additional input:-


$("#add").click(function (e){
$('#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>');    




And below is the PHP code for inserting to database:-

$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)

if (!mysql_query($sql,$con))
  die('Error: ' . mysql_error());
echo "1 record added";

Answers to

PHP mysql - Display comma separated value from the database in tabular form

nr: #1 dodano: 2017-01-05 12:01

Just fetch the data from the table and explode both username and useremail to make array and just loop it and diplay the values.

Below pasted code may help you.

$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>';
    echo "<tr>";
        echo"<td>".$ArrUserName[$i]."</td>"; //display user name
        echo"<td>".$ArrUserEmail[$i]."</td>"; // diaplay user email

Please understand this nd try to implement in your code.

nr: #2 dodano: 2017-01-05 12:01

Ideally, you would want the username and email stored in a new row for each user. This would make it a lot simpler for yourself.

However, try something like:


$query   = mysql_query('SELECT * FROM dynamicdata LIMIT 1', $con);
$results = mysql_fetch_assoc($query);

$usernames = explode(',', $results['user_name']);
$emails    = explode(',', $results['user_email']);

//Now we should be able to loop over them.
for($row = 0; $row <= count($usernames); $row++) {
    echo $usernames[$row] . ' : ' . $emails[$row];

I haven't had chance to test this, but hopefully it works.

Source Show
◀ Wstecz