4

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:

Databaseimage

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

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

Html page

Javascript file for adding additional input:-

$(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();
});

});

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

 <?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)
?>
4
  • 8
    Since you are just starting out, let me gently point out that SQL tables shouldn't be like that. Please find a tutorial on database normalization and read that before writing any more code or trying to get this fixed. Commented Jan 5, 2017 at 10:49
  • Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here Commented Jan 5, 2017 at 10:51
  • Hi , Thanks for quick reply . I need this done urgently because there are thousands of code in our project . Am not in a position to change since we have meet the deadline .On the later stage we can go through database normalization and change the code . Is there any alternate solution for this . Commented Jan 5, 2017 at 10:54
  • You may also want to check your browsers debug console and fix the javascript error as well Commented Jan 5, 2017 at 10:54

2 Answers 2

1

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.

<?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>";        
}
?>

Please understand this nd try to implement in your code.

Sign up to request clarification or add additional context in comments.

2 Comments

This code was very useful and it gave me the result exactly what i wanted. .
If the answer is correct then please do up vote for the same. Then only then stack overflow newbie s can reffer this .
1

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:

<?php 

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.