how to implement a explode function in a table in php? - javascript

I have comma based string which is separated by comma, i need the staring to be converted in each single row and add against to the id ,how should i rotate the inner loop for code after explode function
<?php $conn = new mysqli("localhost","root","","test"); if($conn->connect_error){
die("Connection Failed<br>".$conn->connect_error); }
$sql = 'SELECT id,mobile,code,createdon FROM tset';
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$mobile = $row['mobile'];
$code = $row['code'];
$date = $row['date'];
echo "<tr><td>".$id."</td><td>".$mobile."</td>
<td>".$code."</td><td>".$date."</td></tr>";
}
mysqli_close($conn); ?>
Sample Data
id:10542
Mobile: 1234567890
Code: H5413910102,H5413910201,H5413910301,H5413910701,H5413910802,
dateL:2017-08-30 16:57:00
I need to get the data in this format by using the sample data
<table>
<tr>
<th>ID</th>
<th>MOBILE</th>
<th>CODE</th>
<th>DATE</th>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910102</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910201</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910301</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910701</td><td>9/4/2017</td>
<tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910802</td><td>9/4/2017</td>
</tr>
</table>
$myString = "H5413910102,H5413910201,H5413910301,H5413910701,H5413910802,";
$myArray = explode(',', $myString);
foreach($myArray as $my_Array){
echo $my_Array.'<br>';
}

From the database's perspective, it's not recommended and certainly not a good practice to have data as comma separated list. You should consider normalizing your database. Having said that, you should follow the below procedure to achieve the desired result as of now(or for the time being).
<?php
$conn = new mysqli("localhost","root","","test");
if($conn->connect_error){
die("Connection Failed<br>".$conn->connect_error);
}
$sql = 'SELECT id,mobile,code,createdon FROM tset';
$result = $conn->query($sql);
?>
<table>
<tr>
<th>ID</th>
<th>MOBILE</th>
<th>CODE</th>
<th>DATE</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)){
$codes = explode(",", $row['code']);
foreach($codes as $code){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $code; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
<?php
}
}
?>
</table>
<?php
mysqli_close($conn);
?>

Hope this code helps you :
$sql = 'SELECT id,mobile,code,createdon FROM tset';
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$mobile = $row['mobile'];
$code = $row['code'];
$date = $row['date'];
$code = explode(",",$code);
foreach($code as $key=>$value){
echo "<tr><td>".$id."</td><td>".$mobile."</td><td>".$value."</td><td>".$date."</td></tr>";
}
}

Related

how to update accept/reject of table of db in PHP

i created an appointments table where user1 would add appointments and user2 would accept/reject the appointment. now my problem is when the accept and reject are clicked it is displayed in the table but it is not updated in the db I've did a lot of research over this but hit a road block after road block, so I really hope your help in this, many thanks!
Here is an image of my appointments table:
Here is my code:
<table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
<thead>
<tr>
<th>appoinment ID</th>
<th>Date</th>
<th>time</th>
<th>teacher</th>
<th>parent</th>
<th> accept/reject </th>
<th>state</th>
</tr>
</thead>
<tbody>
<?php
$query=mysqli_query($conn, "select * from `app` left join `par` on par.par_id=app.par_id
left join `tea` on tea.tea_id=app.tea_id
ORDER BY app_id DESC");
if($query === false)
{
throw new Exception(mysqli_error($conn));
}
while($row=mysqli_fetch_array($query))
{
$ann_id=$row['app_id'];
$date=$row['date'];
$msg=$row['time'];
$username = $row['username'];
$username = $row['p_username'];
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<td> <?php echo date('j/m/y',strtotime($row['date'])); ?></td>
<td><?php echo $row['time'] ?></td>
<td><?php echo $row['p_username'] ?></td>
<td><?php echo $row['username'] ?></td>
<td>
reject
accept
</td>
<td>
<div class="chgtext">PENDING</div>
</td>
</tr>
<?php
if (isset($_GET['state'], $_GET['app_id'])) {
$stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['s.app_id']);
$stmt->execute();
$stmt->close();
}
}
?>
</tbody>
</table>
</div>
</form>
You have an error there. You are using $_GET request with setting it in url. Change your html tag like below
reject
accept <!-- I assumed that column names in database were app_id and state
And in php code
if (isset($_GET['state'], $_GET['app_id'])) {
$stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['app_id']);
$stmt->execute();
$stmt->close();
}
}
I hope it will help you. If there is any problem then kindly tell me

Deleting row from mysql using jquery and php

I want to create delete feature using function and jquery
My jquery works and show messages but nothing happen "Nothing Deleted"
Jquery Code
<script type="text/javascript">
$(".remove").click(function(){
var id = $(this).parents("tr").attr("id");
if(confirm('Are you sure to remove this record?'))
{
$.ajax({
url: 'delete.php',
type: 'GET',
data: {id: id},
error: function() {
alert('Something is wrong');
},
success: function(data) {
$("#"+id).remove();
alert("Record removed successfully");
}
});
}
});
PHP Function Code
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
Delete.php Code
include ('function.php');
$id = $_GET['id'];
$table = 'msg';
delete($table,$id);
HTML Code
<table class="table table-striped" style="background-color: #ffffff;">
<tr>
<th>ID</th>
<th>From</th>
<th>Title</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php
$i = '1';
$username = $user_data['username'];
$query = "SELECT * FROM msg WHERE `go_to` = '$username' Order by id";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['come_from']; ?></td>
<td>
<a href="read_message/<?php echo $row['id']; ?>"><?php if(count_msg_not_opened($username, $row['id']) > '0')
{
echo $row['title'];
}
else
{
echo '<b>' . $row['title'] . '</b>';
} ?></a></td>
<td><?php echo $row['date']; ?></td>
<td>
<button class="btn btn-danger btn-sm remove">Delete</button>
</td>
</tr>
<?php } ?>
</table>
I also include "jquery.min.js"
When I press "Delete" bottom this message appears "Are you sure to remove this record?"
I pressed "Yes" then this message appears "Record removed successfully", but nothing was deleted.
I don't know where the problem is.
You forgot to add the id attribute to the <tr>
<tr id="<?php echo $row['id']; ?>">
You should also add error checking and prepared statements to your PHP code.
Are you sure that you have connected your PHP-code to your SQL-database?
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
The code above is relying on a connection already existing within your PHP-file. See this to find out how to apply a connection.

How to make linkable data from a query in a table

How can i make the data from my query linkable/clickable so that i can run a function when I click it. The data that needs to be linkable/clickable is the date and after clicking it will run a function.
Below is my code in PHP:
if (isset($_POST['go'])){
if (((!empty($_POST['from'])&&!empty($_POST['to'])))&&((!empty($_POST['from'])&&!empty($_POST['to'])))){
$from = $_POST['from'];
$to = $_POST['to'];
$from =explode('/',$from);
$from = "$from[2]-$from[0]-$from[1]";
$to = explode ('/',$to);
$to = "$to[2]-$to[0]-$to[1]";
$query= mysqli_query($con,"SELECT Date, Time, Latitude,Longitude,Depth,Magnitude
FROM bulletin WHERE Date between '$from' and '$to';");
$the_array = array();
while($row = mysqli_fetch_array($query)){
$date = $row['Date'];
$time = $row['Time'];
$latitude= $row['Latitude'];
$longitude= $row['Longitude'];
$depth =$row['Depth'];
$magnitude = $row['Magnitude'];
$the_arraypei[] = array($row['Date'] ); //added
echo '<tr class="normalRow">
<td onClick="document.location.href="http://www.yoursite.com";">'.$date.'</a></td>
<td border="1">'.$time.'</td>
<td border="1">'.$latitude.'</td>
<td border="1">'.$longitude.'</td>
<td border="1">'.$depth.'</td>
<td border="1">'.$magnitude.'</td>
</tr>';
}
$js_array1 =$the_arraypei;//added
}
Looks like you haven't opened up an anchor tag <a> within the td.
Also, avoid writing your HTML within the PHP echo statement.
<?php
if (isset($_POST['go'])){
/* Other code */
while($row = mysqli_fetch_array($query)){
/* Loop statements... */
$the_arraypei[] = array($row['Date'] );
?>
<tr class="normalRow">
<td>
<?php echo $date; ?>
</td>
<td border="1"><?php echo $time ?> </td>
<!-- Other tds.... -->
<td border="1"><?php echo $magnitude; ?></td>
</tr>
<?php }
js_array1 = $the_arraypei; //added
}
?>
I think that you have to modify your code like that:
echo '<tr class="normalRow"><td>'.$date.'</td><td border="1">'.$time.'</td><td border="1">'.$latitude.'</td><td border="1">'.$longitude.'</td><td border="1">'.$depth.'</td><td border="1">'.$magnitude.'</td></tr>';
If you want to open a new window without javascript. Otherwise if you want to execute a javascript function you can do:
echo '<tr class="normalRow"><td><button onClick="executeMe()">'.$date.'</button></td><td border="1">'.$time.'</td><td border="1">'.$latitude.'</td><td border="1">'.$longitude.'</td><td border="1">'.$depth.'</td><td border="1">'.$magnitude.'</td></tr>';
And on page you should add
<script type="text/javascript">
function executeMe() {
console.log('Hello World!');
}
</script>
Try it here

Adding and accessing buttons IDs in a PHP-generated table

I have problems with my php that generates a table, requests data from a SQL database, and stores data in the table.
The first cell of each row in the table contains a dropdown button which links to a delete.php script that deletes the row. It also links to a modif.php script used to modify the row's cells.
My problem is that i need to access the dropdown buttons with IDs to know which row to delete.
And i don't really know how to link my dropdown buttons with IDs and access them in my scripts.
Here's the code :
<?php
$con=mysqli_connect("localhost","root","icare","icare1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM magasin");
echo "<table border='1'>
<tr>
<th>code</th>
<th>ip</th>
<th>ads</th>
<th>region</th>
<th>adress</th>
<th>name</th>
<th>email</th>
<th>number</th>
<th>gtc</th>
<th>date</th>
</tr>";
$indexB = array();
$i = 0;
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>
<div class='dropdown'>
<button id=$indexB[$i] class='dropbtn'>▶</button>
<div class='dropdown-content'>
<a href='modif.php'>Modifier</a>
<a href='delete.php'>Supprimer</a>
</div>
".$row['code']."
</div>
</td>";
echo "<td><div>" . $row['ip'] . "</div></td>";
echo "<td><div>" . $row['ads'] . "</div></td>";
echo "<td><div>" . $row['region'] . "</div></td>";
echo "<td><div>" . $row['adress'] . "</div></td>";
echo "<td><div>" . $row['name'] . "</div></td>";
echo "<td><div>" . $row['email'] . "</div></td>";
echo "<td><div>" . $row['number'] . "</div></td>";
echo "<td><div>" . $row['gtc'] . "</div></td>";
echo "<td><div>" . $row['date'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
mysqli_close($con);
?>
And here is the delete.php :
<?php
$connection = mysqli_connect("localhost", "root", "icare", "icare1");
if($connection === false){
die("Connection failed " . mysqli_connect_error());
};
//$id =
$sql = "DELETE FROM magasin WHERE Code=".$id;
//$result = mysqli_query($connection,$sql);
if(mysqli_query($connection, $sql)){
echo "Done !";
} else{
echo "Failed : $sql. " . mysqli_error($connection);
}
mysqli_close($connection);
?>
I started an indexB[] to store the dropdowns IDs but i'm not sure that i'm doing it right.
In the end I want to link my buttons to the code attribute and then delete the row with my sql query using the code attribute.
I'm new to this so ... sorry if i did or ask something plain stupid.
UPDATE :
To mikrafizik :
I tried your answer but it doesn't work. I only get "1">Supprimer". It seemsi have a problem with the href but i just can't find why.
I don't know what i forgot, so if you see something wrong :
<?php
$con=mysqli_connect("localhost","root","icare","icare1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM magasin");
echo "<table border='1'>
<tr>
<th>Code</th>
<th>Adresse IP</th>
<th>Adresse ADS</th>
<th>Région</th>
<th>Adresse</th>
<th>Nom du directeur</th>
<th>Mail</th>
<th>Téléphone</th>
<th>GTC</th>
<th>Date d'installation</th>
</tr>";
$data = mysqli_fetch_array($result);
?>
<table>
<?php foreach ($data as $key => $row):?>
<tr>
<td>
<div class='dropdown-content'>
<button class='dropbtn'>▶</button>
<!-- Modifier -->
Supprimer
</div>
</td>
<td><div><?php echo $row['AdresseIP'];?></div></td>
<td><div><?php echo $row['AdresseADS'];?></div></td>
<td><div><?php echo $row['Region'];?></div></td>
<td><div><?php echo $row['Adresse'];?></div></td>
<td><div><?php echo $row['NomDirecteur'];?></div></td>
<td><div><?php echo $row['Mail'];?></div></td>
<td><div><?php echo $row['Tel'];?></div></td>
<td><div><?php echo $row['Gtc'];?></div></td>
<td><div><?php echo $row['DateInstall'];?></td>
</tr>
<?php endforeach; ?>
</table>
<?mysqli_close($con);?>
delete.php :
<?php
$connection = mysqli_connect("localhost", "root", "icare", "icare1");
if($connection === false){
die("Connexion échouée " . mysqli_connect_error());
};
$id = $_GET['id'];
$sql = "DELETE FROM magasin WHERE Code=".$id;
$result = mysqli_query($connection,$sql);
if($result){
echo "Enregistrement réussi !";
} else{
echo "Enregistrement échoué : $sql. " . mysqli_error($connection);
}
mysqli_close($connection);
?>
At first, divide query and form building like that
$data = mysqli_fetch_array($result)
then
<?php foreach ($data as $key => $row): ?>
<tr>
<td>
<div class='dropdown-content'>
<a href='modif.php?id=<?=$row['id']?>'>Modifier</a>
<a href='delete.php?id=<?=$row['id']?>'>Supprimer</a>
</div>
</td>
</tr>
<?php endforeach ?>
And in your modif.php
$id = $_GET['id'];
(Concerns Flumble_'s answer, that I can't comment because of my low rep)
Maybe the <?= ?> are the problem. Try replacing them with <?php ?>
UPDATE :
You should also never use short open tags (<? ?>) : See the answer to this question.
Also, when you write <?php $row['id'] ?>, you are not printing the value. You must write <?php echo $row['id']; ?>.
The same thing applies with short open tags (but not with the <?= syntax).
Hope this helps further. I will continue reviewing your code.
UPDATE 2 :
Alright I think I got it.
mysqli_fetch_array returns a row, not the entire result set. So you have to loop through the rows until mysqli_fetch_array returns NULL :
while($data = mysqli_fetch_array($result)) {
?>
<tr>
<!-- ... -->
</tr>
<?php
}

How to insert data into an existing html table in php?

I want to insert data which returns from the php code, into an existing html table (name = "userDetails").
Number of rows in the html table would be different according to the results receiving from the mysql database. How can I do that?
Code is as follows,
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
$userDetailsArray = array();
if(mysqli_num_rows($result) > 0){
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
$userID = $row['id'];
$firstName = $row['firstname'];
$email = $row['email'];
$index++;
}
}
?>
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
</table>
</body>
<html/>
Looks like you're looking for something like this:
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>'. $row['id'] .'</td>';
echo '<td>'. $row['firstname'] .'</td>';
echo '<td>'. $row['email'] .'</td>';
echo '</tr>';
}
}
?>
</table>
</body>
<html/>
You can simply generate HTML in your while loop. Not the best possible strategy, but it certainly gives results.
using same page means it will work. try this
while ($row = mysqli_fetch_assoc($result))
{
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['firstname']."</td>"";
echo "<td>".$row['email']."</td></tr>"";
$index++;
}
what are the requirements of these lines in your code actualy?
$userDetailsArray = array();
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
??and even what is the requirement of $index variable?
i am adding changes to you program here.
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
?>
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
<?php
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td>$row['id'];</td>
<td>$row['firstname'];</td>
<td>$row['email'];</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan=3>Data is not available</td>
</tr>
<?php
}
?>
</table>
</body>
<html/>

Categories

Resources