PHP mysql - displays a comma separated value from a database in a table

I'm a complete newbie. I want to display comma separated values ​​from a database in tabular form

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

Databaseimage

I want to display in tabular form on an html / php page as shown 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 to add 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 to insert into the 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) ?> 
+5
source share
2 answers

Just extract the data from the table and explode both username and useremail to create an array, and just loop it and compare the values.

The inserted code below 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 that this will try to implement in your code.

+2
source

Ideally, you would like your username and email address to be stored on a new line for each user. That would make it a lot easier for you.

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 have not had the opportunity to verify this, but hopefully it works.

+2
source

Source: https://habr.com/ru/post/1262348/


All Articles