print a table on php from data retrieved from xml - php

I am trying to print a table with the list of customer oder from data retrieved from an xml file. I think my attempt at retrieving data has failed as it did not show anything. I also wanted to add two buttoms at the end of each order (delet and edit):
<?php
// Loading the XML file
$xml = simplexml_load_file("database/orderlist.xml");
echo "
<div style='overflow-x:auto;'>
<table id='order'>
<tr>
<th>Order #</th>
<th>Customer ID# </th>
<th>Items</th>
<th>Total Prices (CND)</th>
</tr>
foreach($xml->children() as $ftpxml)
{
echo '<td>".$ftpxml->attributes()->id."</td>';
echo '<td> ".$ftpxml->attributes()->customerID." </td>';
echo '<td>".$ftpxml->attributes()->products." </td>';
echo '<td>".$ftpxml->attributes()->totalprice." </td>';
echo '<a href="backStoreOrderProfile.html" ><button class="btn Edit" id="btn" input value="Check" type=submit > Edit </button></a>';
echo '<button class="btn Delete" id="btn" input value="Check" type=submit > Delete</button></td>';
}
</table>
</div>
";
?>

You have a number of problems in your codes
make sure you end each php statement by a semi-colon
make sure you enclose the string in same quotation marks
make sure you traverse the XML hierarchy properly
<?php
// Loading the XML file
$xml = simplexml_load_file("database/orderlist.xml");
echo "
<div style='overflow-x:auto;'>
<table border=1 cellpadding=5 id='order' style='border-collapse: collapse;' bordercolor='#DDDDDD'>
<tr>
<th>Order #</th>
<th>Customer ID# </th>
<th>Items</th>
<th>Total Prices (CND)</th>
<th>Edit
<th>Delete
</tr>";
foreach ($xml->oder as $ftpxml) {
?>
<tr>
<td><?php echo $ftpxml->id; ?>
<td><?php echo $ftpxml->customerID; ?>
<td>
<?php
foreach ($ftpxml->products->product as $ftpxml2) {
echo $ftpxml2->productName . " ";
} ?>
<td>
<?php echo $ftpxml->totalprice; ?>
<?php
echo '<td><a href="backStoreOrderProfile.html" ><button class="btn Edit" id="btn" input value="Check" type=submit > Edit </button></a>';
echo '<td><button class="btn Delete" id="btn" input value="Check" type=submit > Delete</button></td>';
?>
<?php
} ?>

Related

date filter not wroking for mysql in php

Table name :- add_user
data is inserting in it, there is a column due_date storing date in (dd-mm-yyyy) format
now i want to create a filter for due_date. But my code is not working. When i'm running the code it is showing all data, it is not filtering it.
here is my code
<form class="form-inline" method="post" action="">
<label> <b>From Date :  </b></label>
<input type="date" name="s_date" class="form-control"> 
<input type="date" name="e_date" class="form-control">
<button type="submit" name="search" class="btn btn-success">Search</button>
</form>
<?php
error_reporting(0);
include_once "config2.php";
if(count($_POST)>0) {
$s_date=date("dd-mm-yyyy", strtotime($_POST['s_date']));
$e_date=date("dd-mm-yyyy", strtotime($_POST['e_date']));
$result = mysqli_query($conn,"SELECT * FROM add_user WHERE due_date BETWEEN '$s_date' AND '$e_date'");
}
?>
<center>
<table class='table table-bordered'>
<thead class="thead-dark">
<tr>
<th>Action</th>
<th>Pet Photo</th>
<th>Owner</th>
<th>Mobile Number</th>
<th>Category</th>
<th>Pet Id</th>
<th>Pet Name</th>
<th>Address</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><button class="btn btn-info">View</button></td>
<td><img src="/pet_img/<?php echo $row["signature"]; ?>"width="60px"height="80px"> </td>
<td><img src="/pet_img/<?php echo $row["photo"]; ?>"width="60px"height="80px"> <br><b> <?php echo $row["name"]; ?> </td>
<td><?php echo $row["mobile"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><b><font color="green"><?php echo $row["pet_id"]; ?></font></b></td>
<td><?php echo $row["pet_name"]; ?></td>
<td><?php echo $row["add1"]; ?> <?php echo $row["add2"]; ?> <?php echo $row["add3"]; ?> <?php echo $row["pin"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
Your column data type needs to be either timestamp or date/datetime. You can't compare date strings in this format.

Warning: Invalid argument in php

This file has an error invalid argument supplied for foreach. Does anyone solve this issue. I tried lot of time but i could not find it,I don't know how to solve this. Here is My code:
<?php
include('connect.php');
$query = "SELECT * FROM product";
$result = mysqli_query($connect,$query);
$output ='<table class="table table-stripped table-bordered">
<tr><th>Product Name</th>
<th>Product Type</th>
<th>Product Rate</th>
<th>Brand</th>
<th>Edit</th>
<th>Insert</th>
</tr>';
if(mysqli_num_rows($result)>0)
{
foreach (mysqli_num_rows($result) as $row) {
$output .= '<tr>
<td width="40%">'.$row["product_name"].'</td>
<td width="40%">'.$row["product_type"].'</td>
<td width="40%">'.$row["product_rate"].'</td>
<td width="40%">'.$row["brand"].'</td>
<td width="10%">
<button type="button" name="edit" class="btn btn-primary btn-xs edit" id="'.$row["id"].'">Edit</button>
</td>
<td width="10%">
<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["id"].'">Delete</button>
</td>
</tr>';
}
}
else
{
$output .='<tr>
<td colspan ="4" align="center"> No data Found</td>
</tr>' ;
}
$output .= '</table>';
echo $output;
?>
mysqli_num_rows function returns a number, not an array. You probably want to use mysqli_fetch_all:
foreach (mysqli_fetch_all($result) as $row) {
}
The mistake you made is that you use the num_rows function in the foreach. I always prefer to use a while-loop with a fetch.
while ($row = mysqli_fetch_assoc($result)) {
$output .= '<tr>
<td width="40%">'.$row["product_name"].'</td>
<td width="40%">'.$row["product_type"].'</td>
<td width="40%">'.$row["product_rate"].'</td>
<td width="40%">'.$row["brand"].'</td>
<td width="10%">
<button type="button" name="edit" class="btn btn-primary btn-xs edit" id="'.$row["id"].'">Edit</button>
</td>
<td width="10%">
<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["id"].'">Delete</button>
</td>
</tr>';
}
mysqli_fetch_array is the solution to your problem. foreach requires an array to iterate it and the earlier method provides you the same.
mysqli_fetch_assoc will return row as associative array, if you want complete rows as associative array you must use mysqli_fetch_all($res, MYSQLI_ASSOC);

How would i add a drop down menu to my jquery table which uses mysql?

So currently I am trying to figure out how I would add a dropdown menu to the code I've posted below. I've searched the internet but could not find a way that worked for me, I'm very new to coding so this is kind of difficult for me others may find this easy.
<table class="table table-bordered table-striped">
<thead> <!-- add class="thead-inverse" for a dark header -->
<tr>
<th>ID</th>
<th>USERNAME</th>
<th>Crime(s)</th>
<th>Active</th>
<th>EDIT</th>
</tr>
</thead>
<tfoot>
<tr>
</tr>
</div>
</th>
</tfoot>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td class="username" data-id1="'.$row["id"].'" contenteditable>'.$row["name"].'</td>
<td class="crime" data-id2="'.$row["id"].'" contenteditable>'.$row["crime"].'</td>
<td class="activated" data-id2="'.$row["id"].'" contenteditable>'.$row["activated"].'</td>
<td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">DELETE</button></td>
</tr>
';
}
$output .= '
<tr>
<td></td>
<td id="name" contenteditable></td>
<td id="crime" contenteditable></td>
<td id="activated" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">ADD ACCOUNT</button></td>
</tr>
';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
Since you mentioned that you'd like to add the dropdown to the activated section of your table and that it should contain Yes, No and Being Verified, you can update that table column to be
<td class="activated" data-id2="'.$row["id"].'" contenteditable>
<select name="activated" id="activated">
<option value="Yes" '.(($row["activated"] == "Yes") ? 'selected="selected"':"").'>Yes</option>
<option value="No" '.(($row["activated"] == "No") ? 'selected="selected"':"").'>No</option>
<option value="Being Verified" '.(($row["activated"] == "Being Verified") ? 'selected="selected"':"").'>Being Verified</option>
</select>
</td>
This would allow the value from the db $row["activated"] get automatically selected in the dropdown.

PHP - DataTables Post username from table row for a mysql_query?

I am trying to add a delete button to my DataTables table, but I don't know how I can post the username of the selected row when Delete button is pressed.
Here is how the DataTables is printed:
<div class="panel-body">
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Fullname</th>
<th>Class ID</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
while($listDetailsRow = mysql_fetch_array($listDetails)){
?>
<tr>
<td class="success"><?=$listDetailsRow['username']?></td>
<td class="info"><?=$listDetailsRow['email']?></td>
<td class="success"><?=$listDetailsRow['fullname']?></td>
<td class="info"><?=$listDetailsRow['class_id']?></td>
<td>
<form id='delete' action='deleteUser.php' method='post'>
<button type="submit" name="submit" class="btn btn-danger" href="deleteUser.php">
Delete
</button>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
So it is printed by php until all has been printedm then it stops...
The deleteUsers.php file is as follows:
include('../details.php');
$userDel = $_POST($listDetailsRow['username']);
echo $userDel;
// mysql_query("DELETE * FROM users WHERE username='$userDel'") or die(mysql_error());
I am just trying to get it to print the username from the row of which the delete button was pressed.
Is this possible? Not very good with JS or Ajax so all help is appreciated.
<form id='delete' action='deleteUser.php' method='post'>
<input type='hidden' name='username' value="<?=$listDetailsRow['username'];?>" > <--add this line
<button type="submit" name="submit" class="btn btn-danger" href="deleteUser.php">
Delete
</button>
</form>
php:
$userDel = $_POST['username'];
You can use an a element instead of a form within a button element. See this code:
<tbody>
<?php
while($listDetailsRow = mysql_fetch_array($listDetails)){
?>
<tr>
<td class="success"><?=$listDetailsRow['username']?></td>
<td class="info"><?=$listDetailsRow['email']?></td>
<td class="success"><?=$listDetailsRow['fullname']?></td>
<td class="info"><?=$listDetailsRow['class_id']?></td>
<td>
<!------------- New -------------->
<?php
$params = array( 'username' => $listDetailsRow['username'] );
$href = "deleteUser.php?" . http_build_query($params);
?>
<a class="btn btn-danger" href="<?php echo $href; ?>" > Delete </a>
<!------------- End New -------------->
</td>
</tr>
<?php
}
?>
</tbody>
In deleteUser.php do this:
$userDel = isset($_GET['username']) ? $_GET['username'] : null;

echo -> html = chaos

I try to output a database Table in PHP within a HTML Table. When I view the output in HTML, It is totally messed up. In my opinion everything is correct. Maybe someone of you see the mistake.
My PHP file:
<table id="table_id" class="display table table-hover table-responsive table-striped">
<thead>
<tr>
<th class="col-md-3">Gespeichert am</th>
<th class="col-md-5">Text</th>
<th class="col-md-3">Kategorie</th>
<th class="col-md-1"><span class='glyphicon glyphicon-remove' aria-hidden='true'></span></th>
</tr>
</thead>
<tbody>
<?php
while($row=mysql_fetch_object($ergebnis))
{
echo "<tr>";
echo "<td>".date("H:i - d.m.y",strtotime($row->Zeit) + 60*60)."</td>";
echo "<td><a href='".$row->url."' target='_blank'>".$row->text."</a></td>";
echo "<td>".$row->kategorie."</td>";
echo "<form action='delete.php' method='post'>";
echo "<td>";
echo "<input type='hidden' name='delete_id' value='".$row->id."'</td>";
echo "<button onClick='submit();' style='border:none; background-color:transparent; padding:0px;' type='submit' value='Delete'><span class='glyphicon glyphicon-remove delete' aria-hidden='true'></span></button>";
echo "</td>";
echo "</form>";
echo "</tr>";
}
?>
</tbody>
</table>
My output in HTML looks like this:
<table id="table_id" class="display table table-hover table-responsive table-striped">
<thead>
<tr>
<th class="col-md-3">Gespeichert am</th>
<th class="col-md-5">Text</th>
<th class="col-md-3">Kategorie</th>
<th class="col-md-1"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></th>
</tr>
</thead>
<tbody>
<tr>
<td>15:46 - 26.03.15</td>
<td>Google link</td>
<td>Web</td>
<form action="delete.php" method="post"></form>
<td><input type="hidden" name="delete_id" value="37" <="" td="">
<button onclick="submit();" style="border:none; background-color:transparent; padding:0px;" type="submit" value="Delete">
<span class="glyphicon glyphicon-remove delete" aria-hidden="true"></span>
</button>
</td>
</tr>
</tbody>
</table>
Either put <form> inside cell of table (inside the <td>), or wrap it around entire table.
The problem is that <tr> allows only <th> and <td> nodes inside and that's why your <form> is getting messed up.
Also, as someone already mentioned in comments, the following line has errors which result in broken html:
echo "<input type='hidden' name='delete_id' value='".$row->id."'</td>";
should be:
echo "<input type='hidden' name='delete_id' value='".$row->id."'></td>";

Categories