Add button in a table retrieved from database with php - php

I am able to get all the content of my table from the database using php.
How can I add a button in one of the column of the table , or either another component like a select or a checkbox....
I post a pic to make it more clear.
I get the table using php but how to insert the component in the row?
$db = new mysqli("...", "...", "...", "...");
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * from ...";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>";
}
echo "
</tr>
</thead>
<tbody>";
while($row = $result->fetch_assoc()){
echo "<tr class='info'>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['mail'] . "</td>
<td>" . $row['number'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['price'] . "</td>
<td>" . $row['paymenttype'] . "</td>
<td>" . $row['status'] . "</td>
<td> add </td>
</tr>";
}
echo "
</tbody>
</table>";
?>
-----------------update
echo "<tr class='info'>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['mail'] . "</td>
<td>" . $row['number'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['price'] . "</td>
<td>" . $row['paymenttype'] . "</td>
<td>" . $row['status'] . "</td>
<td><input type='checkbox'></td>
<td><a href='http://yourlink' >delete</a></td>
</tr>";

<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>";
}
echo "
</tr>
</thead>
<tbody>";
while($row = $result->fetch_assoc()){
echo "<tr class='info'>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['mail'] . "</td>
<td>" . $row['number'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['price'] . "</td>
<td>" . $row['paymenttype'] . "</td>
<td>" . $row['status'] . "</td>
<td> <button class='btn' >Button</button> </td>
</tr>";
}
echo "
</tbody>
</table>";
?>

Found this solution
<td><a class='btn btn-primary btn-lg' href='send.php?name=".$row['name']."'>Send</a></td>

Related

How can I check if all checkboxes have been checked in PHP?

I am implementing a list of items and each has a checkbox. I currently can see which checkboxes have been checked but what I want to do is check if all of them have been checked. How can I implement that?
Here is my code:
<form action="" method="post">
<?php
echo "<table>
<tr>
<th>Customer ID</th>
<th>Report ID</th>
<th>Report message</th>
<th>Device</th>
<th>Device no.</th>
<th>Barcode</th>
<th>IMEI</th>
<th>Sale-date</th>
</tr>";
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
?>
<?php if ($row['acceptance'] == 3) {
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
}
</form>
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['rejected'])) {
if (count($count_devices) == 1) {
...
}
}
<?php
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
$i = 0; // initiate the variable here
$total_number_of_rows = $message->num_rows(); // Get total number of rows from your object
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
if ($row['acceptance'] == 3) {
$i++; // When conditions trues, increment the variable.
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
if($i == $total_number_of_rows){ // Here implement this condition, If both equal then all inputs have checked.
echo "Check box checked all inputs";
}
}
?>
I think you expecting the same as above. We need to check the Total number of rows with Incremental variable value.
Please review my comment inside the code part, so that you can understand terms.

How to return average data through the loop in mysqli and php

I am a beginner programmer, and i want to fetch AVG() data in every rows created by the while loop,
this is the code,
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, AVG(perpanjangan_pagu) FROM site";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows = $row["AVG(perpanjangan_pagu)"];
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $rows . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
//echo "0 results";
}
$conn->close();
?>
and this is the image, you can see that it only return one row.
and this is the image when I deleted the avg function.
please help me to fetch the average data in every row in the table, not just only one table,
Thanks.
can you try this I have modified user code & added the AVG() column.
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, (SELECT AVG(perpanjangan_pagu) FROM site) AS 'AVG_pagu' FROM site";
$result = $conn->query($sql);
// AVG_pagu has the AVG value of all columns of `perpanjangan_pagu` in table `site`
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
// $rows = array(); // This is not actually required
while ($row = $result->fetch_assoc()) {
//$rows[] = $row["AVG_pagu"]; // This is not actually required
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $row["AVG_pagu"] . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
echo "No records found!";
}
$conn->close();
?>

where to enter "&nbsp" in the following code?

Can some one kindly help me find a place to add &nbsp between each of the rows in the echo statement. where and how can I add blank space between each row? kindly ignore the first set of echo statements that have been commented out. Thank you...
<link rel="stylesheet" href="docfullcss.css">
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'employee_info';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_connect_error());
}
$sql = 'SELECT * FROM `docfull` ORDER BY `COL 7` ASC';
$retval = mysqli_query($conn,$sql);
if(! $retval ) {
die('Could not get data: ' . mysqli_error($conn));
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
/* (ignore this commented section)
echo "<table>";
echo "<tr>";
echo "<td>COL 1</td>";
echo "<td>COL 2</td>";
echo "</tr>";
echo "</table>";
*/
echo "<tr><td>" . $row['COL 1'] . "</td>
<td>" . $row['COL 2'] . "</td>
</tr>" . $row['COL 3'] . "</td>
<td>" . $row['COL 4'] . "</td>
<td>" . $row['COL 5'] . "</td>
<td>" . $row['COL 6'] . "</td>
<td>" . $row['COL 7'] . "</td>
<td>" . $row['COL 8'] . "</td>
<td>" . $row['COL 9'] . "</td>
<td>" . $row['COL 10'] . "</td>
<td>" . $row['COL 11'] . "</td>
<td>" . $row['COL 12'] . "</td>
<td>" . $row['COL 13'] . "</td>
<td>" . $row['COL 14'] . "</td>
<td>" . $row['COL 15'] . "</td>
<td>" . $row['COL 16'] . "</td><td>";
echo "<br><br>";
}
mysqli_close($conn);
?>
Problem is here
echo "<tr><td>" . $row['COL 1'] . "</td><td>" . $row['COL 2'] .
"</td></tr>" . $row['COL 3'] .
You end row after COL 2 and never start again till another iteration. You need to remove this </tr> after COL 2 and add <td> to this place and add </tr> after . $row['COL 16'] . "</td><td>"; , also you don'd need last td and those <br> tags on next line.
To have simple table you need something like this:
echo '<table>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
echo "<tr><td>" . $row['COL 1'] . "</td>
<td>" . $row['COL 2'] . "</td>
<td>" . $row['COL 3'] . "</td>
<td>" . $row['COL 4'] . "</td>
<td>" . $row['COL 5'] . "</td>
<td>" . $row['COL 6'] . "</td>
<td>" . $row['COL 7'] . "</td>
<td>" . $row['COL 8'] . "</td>
<td>" . $row['COL 9'] . "</td>
<td>" . $row['COL 10'] . "</td>
<td>" . $row['COL 11'] . "</td>
<td>" . $row['COL 12'] . "</td>
<td>" . $row['COL 13'] . "</td>
<td>" . $row['COL 14'] . "</td>
<td>" . $row['COL 15'] . "</td>
<td>" . $row['COL 16'] . "</td></tr>";
}
echo '</table>';

I want to upload a file and when the file upload, the values of the file has to be put in my array

Below you can see my formpage, at this page you upload a xml file.
<html>
<body>
<form enctype="multipart/form-data" action="arrayincludefiletest.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<table width="600">
<tr>
<td>File name:</td>
<td><input type="file" name="file" /></td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>
</body>
</html>
Below you can see my PHP code, if you take a look you can see that now everything is manual. I have to fill the array by myself. You can also see that I insert the array into my database. I want to upload a xml file and that the values of the xml file automatic be put in my array.
<html>
<head>
<title> Bom Array </title>
</head>
<body>
<?php
$bom= array(
array("Aantal" =>1, "Manufactorer" =>"Panasonic", "Partno" =>"EEEFC1H1R0R",
"Description" =>"Capacitor 0603","Footprint" =>"CAP0603", "Refdes" =>"B1"),
array("Aantal" =>2, "Manufactorer" =>"Vishay", "Partno" =>"MAL215371228E3",
"Description" =>"Capacitor 1210","Footprint" =>"CAP1210", "Refdes" =>"C6,C7"),
array("Aantal" =>3, "Manufactorer" =>"Ferroxcube", "Partno" =>"MAL215371109E3",
"Description" =>"Buzzer 80dB 3,4 KHz","Footprint" =>"KPEG238", "Refdes" =>"C8,C25"),
array("Aantal" =>4, "Manufactorer" =>"Philips", "Partno" =>"EEEFC1E101P",
"Description" =>"Tantaal 100uF, 6,3V Case_B","Footprint" =>"Case_B", "Refdes" =>"C1")
);
echo "<table border='1'>";
echo "<tr>
<th>Aantal</th>
<th>Manufactorer</th>
<th>Partno</th>
<th>Description</th>
<th>Footprint</th>
<th>Refdes</th>
</tr>";
echo "<tr>
<td>" . $bom[0]['Aantal'] . "</td>
<td>" . $bom[0]['Manufactorer'] . "</td>
<td>" . $bom[0]['Partno'] . "</td>
<td>" . $bom[0]['Description'] . "</td>
<td>" . $bom[0]['Footprint'] . "</td>
<td>" . $bom[0]['Refdes'] . "</td>
</tr>";
echo "<tr>
<td>" . $bom[1]['Aantal'] . "</td>
<td>" . $bom[1]['Manufactorer'] . "</td>
<td>" . $bom[1]['Partno'] . "</td>
<td>" . $bom[1]['Description'] . "</td>
<td>" . $bom[1]['Footprint'] . "</td>
<td>" . $bom[1]['Refdes'] . "</td>
</tr>";
echo "<tr>
<td>" . $bom[2]['Aantal'] . "</td>
<td>" . $bom[2]['Manufactorer'] . "</td>
<td>" . $bom[2]['Partno'] . "</td>
<td>" . $bom[2]['Description'] . "</td>
<td>" . $bom[2]['Footprint'] . "</td>
<td>" . $bom[2]['Refdes'] . "</td>
</tr>";
echo "<tr>
<td>" . $bom[3]['Aantal'] . "</td>
<td>" . $bom[3]['Manufactorer'] . "</td>
<td>" . $bom[3]['Partno'] . "</td>
<td>" . $bom[3]['Description'] . "</td>
<td>" . $bom[3]['Footprint'] . "</td>
<td>" . $bom[3]['Refdes'] . "</td>
</tr>";
echo "</table>";
// Connectie database and Insert into database
$con = mysqli_connect("localhost", "csa", "csa", "csa");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
if(is_array($bom))
{
$sql= "INSERT INTO bom (Aantal, Manufactorer, Partno, Description, Footprint, Refdes) values";
$valuesArr = array();
foreach ($bom as $row)
{
$aantal = (int) $row['Aantal'];
$manufactorer = mysqli_real_escape_string($con, $row['Manufactorer']);
$partno = mysqli_real_escape_string($con, $row['Partno']);
$description = mysqli_real_escape_string($con, $row['Description']);
$footprint = mysqli_real_escape_string($con, $row['Footprint']);
$refdes = mysqli_real_escape_string($con, $row['Refdes']);
$valuesArr[] = "('$aantal', '$manufactorer', '$partno', '$description', '$footprint', '$refdes')";
}
$sql .= implode(',', $valuesArr);
mysqli_query($con, $sql) or die('Error:' . mysqli_errno($con));
}
mysqli_close($con);
?>
</body>
</html>
Use xml_parse_into_struct() function for this.
$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();
$fp = fopen($xmlfile, 'r');
$xmldata = fread($fp, 4096);
xml_parse_into_struct($xmlparser,$xmldata,$values);
xml_parser_free($xmlparser);
print_r($values);

Creating a function that contains a while loop

I am new to PHP and trying to learn it by creating a database of all my jobs (I'm a freelance designer). I have created the code below to generate a table to display all jobs where the description contains logo which works fine and generates several rows....
<?php
$logojobs = mysql_query("SELECT * FROM hlgd_projects WHERE description LIKE '%logo%'", $connection);
?>
View logo jobs
<?php
if ($report == 'logo') {
echo "<h1>Logo jobs</h1>";
echo "<table id='report'>
<tr>
<th></th>
<th>Status</th>
<th>Lead</th>
<th>Start</th>
<th colspan='2'>Codes</th>
<th>Client</th>
<th>Description</th>
<th>Fee</th>
<th>Contact</th>
<th colspan='2'>Invoice</th>
<th>Paid</th>
</tr>";
while ($logojob = mysql_fetch_array($logojobs)) {
echo "<tr>
<td class='id'>" . $logojob['id'] . "</td>
<td>" . $logojob['status_id'] . "</td>
<td>" . $logojob['lead_id'] . "</td>
<td>" . $logojob['date_start'] . "</td>
<td>" . $logojob['code_lead'] . "</td>
<td>" . $logojob['code_hlgd'] . "</td>
<td>" . $logojob['client'] . "</td>
<td>" . $logojob['description'] . "</td>
<td>£" . $logojob['fee'] . "</td>
<td>" . $logojob['contact'] . "</td>
<td>" . $logojob['invoice'] . "</td>
<td>" . $logojob['date_inv'] . "</td>
<td>" . $logojob['date_paid'] . "</td>
</tr>";
}
echo "</table>";
}
?>
However I would like to be able to generate reports for lots of different things and so would like to create a function to generate the table and pass the relevant arguments each time. I've done this as follows but it only generates the first row so I presume it's ignoring the while? Can anyone tell me where I'm going wrong or how to better put the code above into a function?
<?php
function report_bytype($title,$job_set,$job_name) {
$reporthead = "<h1>$title</h1>
<table id='report'>
<tr>
<th></th>
<th>Status</th>
<th>Lead</th>
<th>Start</th>
<th colspan='2'>Codes</th>
<th>Client</th>
<th>Description</th>
<th>Fee</th>
<th>Contact</th>
<th colspan='2'>Invoice</th>
<th>Paid</th>
</tr>";
while ($job_name = mysql_fetch_array($job_set)) {
$reportrows = "
<tr>
<td class='id'>" . $job_name['id'] . "</td>
<td>" . $job_name['status_id'] . "</td>
<td>" . $job_name['lead_id'] . "</td>
<td>" . $job_name['date_start'] . "</td>
<td>" . $job_name['code_lead'] . "</td>
<td>" . $job_name['code_hlgd'] . "</td>
<td>" . $job_name['client'] . "</td>
<td>" . $job_name['description'] . "</td>
<td>£" . $job_name['fee'] . "</td>
<td>" . $job_name['contact'] . "</td>
<td>" . $job_name['invoice'] . "</td>
<td>" . $job_name['date_inv'] . "</td>
<td>" . $job_name['date_paid'] . "</td>
</tr>";
}
$reportfoot = "</table>";
$reporttable = $reporthead . $reportrows . $reportfoot;
echo $reporttable;
return $reporttable;
}
?>
<?php
if($report == 'logo') {
report_bytype("logo",$logojobs,$logojob);
}
if($report == 'stationery') {
report_bytype("stationery",$stationeryjobs,$stationeryjob);
}
?>
Many thanks in advance,
Helen
The problem is this line:
while ($job_name = mysql_fetch_array($job_set)) {
$reportrows = "lots of html"
}
Every time this loops $reportrows is set to the html for that row only.
Use $reportrows .= "some html"; instead which will add each row to $reportrows rather than replace $reportrows with that row.
Edit: Replacing += with .=.
I can see why you have errors you are not concatenating your result
Please see http://php.net/manual/en/language.operators.string.php for more detailed explanations
Replace
$reportrows = "
With
$reportrows .= "
Full Script
function report_bytype($title, $job_set, $job_name) {
$reporthead = "<h1>$title</h1>
<table id='report'>
<tr>
<th></th>
<th>Status</th>
<th>Lead</th>
<th>Start</th>
<th colspan='2'>Codes</th>
<th>Client</th>
<th>Description</th>
<th>Fee</th>
<th>Contact</th>
<th colspan='2'>Invoice</th>
<th>Paid</th>
</tr>";
$reportrows = "";
while ( $job_name = mysql_fetch_array ( $job_set ) ) {
$reportrows .= "
<tr>
<td class='id'>" . $job_name ['id'] . "</td>
<td>" . $job_name ['status_id'] . "</td>
<td>" . $job_name ['lead_id'] . "</td>
<td>" . $job_name ['date_start'] . "</td>
<td>" . $job_name ['code_lead'] . "</td>
<td>" . $job_name ['code_hlgd'] . "</td>
<td>" . $job_name ['client'] . "</td>
<td>" . $job_name ['description'] . "</td>
<td>£" . $job_name ['fee'] . "</td>
<td>" . $job_name ['contact'] . "</td>
<td>" . $job_name ['invoice'] . "</td>
<td>" . $job_name ['date_inv'] . "</td>
<td>" . $job_name ['date_paid'] . "</td>
</tr>";
}
$reportfoot = "</table>";
$reporttable = $reporthead . $reportrows . $reportfoot;
echo $reporttable;
return $reporttable;
}
Thanks

Categories