Trying to divide a string - php

I am pulling information from my table. It is setup as a varchar. I can't change it because it 0's everything out when I do. I need to be able to divide the result by 12. It is the MY-TotalPrem that needs to be divided.
I tried
<td>'.$row["MS-TotalPrem" / 12].'
</td>, <td>'.$row["MS-TotalPrem"] / 12.'</td>,
<td>'.$row[int("MS-TotalPrem") / 12].
'</td>
and none work.
function fetch_customer_data($connect)
{
$query = "SELECT * FROM mytable";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>Address</th>
<th>Policy Number</th>
<th>Total Monthly</th>
<th>Expiration Date</th>
</tr>
';
foreach($result as $row)
{
$output .= '
<tr>
<td>'.$row["AC-AcctName"].'</td>
<td>'.$row["PM-PropertyAddr1"].'</td>
<td>'.$row["PO-Addr1"].'</td>
<td>'.$row["PO-PrefixPolNum"].'</td>
<td>'.$row["MS-TotalPrem"].'</td>
<td>'.$row["PO-ExpDate"].'</td>
</tr>
';
}
$output .= '
</table>
</div>
';
return $output;
}
I just want the output to be a division of 12, or any other number I put in. Thank you.

function fetch_customer_data($connect)
{
$query = "SELECT * FROM mytable";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>Address</th>
<th>Policy Number</th>
<th>Total Monthly</th>
<th>Expiration Date</th>
</tr>
';
foreach($result as $row)
$output .= '
<tr>
<td>'.$row["AC-AcctName"].'</td>
<td>'.$row["PM-PropertyAddr1"].'</td>
<td>'.$row["PO-Addr1"].'</td>
<td>'.$row["PO-PrefixPolNum"].'</td>
<td>'.($row["MS-TotalPrem"] / 12).'</td>
<td>'.$row["PO-ExpDate"].'</td>
</tr>
';
$output .= '
</table>
</div>
';
return $output;
}

Related

Exporting data from MySQL to Excel with PHP

I want to export the data in the gotten from det SQL to a spreadsheet, using PHP.
$sql_export = "SELECT id_employee, firstname, email FROM employees WHERE id_employee = 1";
$export_result = $db->query($sql_export);
This code working for me.Try this code and make some changes as you needed.
**excl_1.php**
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
?>
</table>
<br />
<form method="post" action="export.php">
<input type="submit" name="export" class="btn btn-success" value="Export" />
</form
**export.php**
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if(isset($_POST["export"]))
{
$query = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
?>
Test Result
There are a couple of libraries for that, first and foremost https://packagist.org/packages/phpoffice/phpspreadsheet

Cannot upload csv file using codeigniter

I try to import my.csv file but I cannot upload file and find out my problem.
No error show, if I print my code see all empty value don't show anything.
Here is my code:
public function load_data(){
$result = $this->web->select();
// print_r($result);exit;
$output = '
<h3 align="center">Imported User Details from CSV File</h3>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Sr. No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email Address</th>
</tr>
';
$count = 0;
if($result->num_rows() > 0)
{
foreach($result->result() as $row)
{
$count = $count + 1;
$output .= '
<tr>
<td>'.$count.'</td>
<td>'.$row->first_name.'</td>
<td>'.$row->last_name.'</td>
<td>'.$row->phone.'</td>
<td>'.$row->email.'</td>
</tr>
';
}
}
else
{
$output .= '
<tr>
<td colspan="5" align="center">Data not Available</td>
</tr>
';
}
$output .= '</table></div>';
echo $output;
}
public function import() {
$file_data = $this->csvimport->get_array($_FILES["csv_file"]["tmp_name"]);
foreach($file_data as $row){
$data[] = array(
'first_name' => $row["First Name"],
'last_name' => $row["Last Name"],
'phone' => $row["Phone"],
'email' => $row["Email"]
);
}
$this->web->insert($data);
}
How can I upload csv and xlsx file by using codeigniter in easy and simple way?

Passing ID variable to the modal page to perform date filter by include this php page

I have no idea why this is not working. Maybe I'm lack of PHP experience but I tried to follow as much as I can from the one which works, but it doesn't work.
What I wish to perform is that getting the ID from the list of page after the user presses the button and pass the variable to the bootstrap modal page. I manage to figure out on displaying the table of the ID.
But when it comes to date filtering, it has failed.
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'],$_POST["From"], $_POST["to"])) {
$result = '';
$id = intval($_REQUEST['id'],$_POST["From"], $_POST["to"]);
$query = "SELECT * FROM history WHERE ID=:id and date BETWEEN '".$_POST["From"]."' AND '".$_POST["to"]."'";
//
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
$result .='
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result .='
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .='</table>';
echo $result;
}?>
But it is working this way:
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'])) {
$id = intval($_REQUEST['id']);
$query = "SELECT * FROM history WHERE ID=:id";
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
<div id="purchase_order">
<table class="table table-bordered">
<tr>
<th width="10%">ID</th>
<th width="20%">Location</th>
<th width="20%">Status</th>
<th width="10%">Date</th>
</tr>
<?php
// while($row= mysqli_fetch_array($sql))
// {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row["ID"]; ?></td>
<td><?php echo $row["location"]; ?></td>
<td><?php echo $row["currentStatus"]; ?></td>
<td><?php echo $row["date"]; ?></td>
</tr>
<?php
}
?>
I believe the problem is with your use of date in your SQL.
<?php
require_once 'dbconfig.php';
// enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_REQUEST['id'], $_POST['From'], $_POST['to'])) {
$result = '';
// intval() only has 2 arguments
$id = intval($_REQUEST['id']);
$from = $_POST['From'];
$to = $_POST['to'];
// more likely the problem: date is a reserved word in MySQL -- so wrap it in backquotes
// also, use placeholders for From and To
$query = "SELECT * FROM history WHERE ID = ? AND `date` BETWEEN ? AND ?";
$stmt = $DBcon->prepare($query);
$stmt->execute([$id, $from, $to])); // simplified
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// unnecessary and dangerous
//extract($row);
$result .= '
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
// you already fetched the row - no need to do it again
if ($row) {
$result .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .= '</table>';
echo $result;
}
?>
Useful links:
PDO and MySQL 'between'
Is “Date” a keyword in mysql?
Converting to boolean

Syntax error when using if and using query inside another query

I'm working with displaying an output when I count the value of the second query is greater than or equal to 2 , using the first query where second query schedule number is equal to the first query schedule number.
The line that I got an error:
if ($x>=2) {.'<button onclick="CreateRevision('.$row['id'].')" type="button" class="btn btn-primary"><span class="fa fa-copy"></span></button>'.} else{}.'.
I just got this message syntax error, unexpected'.'
My whole code is this:
<?php
//open connection to mysql db
include("db_connection.php");
$data = '
<table id="tb_jobsched" class="display nowrap table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</thead>
<tfoot>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</tfoot>';
$query = "SELECT * FROM prepressjobs WHERE pj_deleted = 0 ORDER BY pj_datecreated DESC";
if (!$result = mysqli_query($db,$query)) {
exit(mysqli_error());
}
// if query results contains rows then featch those rows
if(mysqli_num_rows($result) > 0)
{
$number = 1;
while($row = mysqli_fetch_assoc($result))
{
$data .= '<tr>
<td>'.$number.'</td>
<td>'.$row['pj_schednum'].'</td>
<td>'.$row['pj_itemname'].'</td>
<td>'.$row['pj_output'].'</td>
<td>'.$row['pj_datecreated'].'</td>
<td>'.$row['pj_expectedfinished'].'</td>
<td>'.$row['pj_preparedby'].'</td>
<td>'.$row['pj_status'].'</td>
<td>
<div class="btn-group" role="group" aria-label="Button group with nested dropdown">
<button onclick="GetJobDetails('.$row['id'].')" type="button" class="btn btn-primary"><span class="fa fa-gear"></span></button>
'.
$querya = "SELECT COUNT(rm_schednum) AS jobrevision FROM jobrevisions WHERE rm_schednum ='{$row['pj_schednum']}'";
$resulta= mysqli_query($db, $querya);
$rowa = mysqli_fetch_assoc($resulta);
$x=$rowa['jobrevision'];
if ($x>=2) {.'<button onclick="CreateRevision('.$row['id'].')" type="button" class="btn btn-primary"><span class="fa fa-copy"></span></button>'.} else{}.'
</div>
</td>
</tr>';
$number++;
}
}
else
{
// records now found
$data .= '<tr><td colspan="8">Records not found!</td></tr>';
}
$data .= '</table>';
echo $data;
?>
What is the following solution about this?
<?php
<?php
//open connection to mysql db
include("db_connection.php");
$data = '
<table id="tb_jobsched" class="display nowrap table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</thead>
<tfoot>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</tfoot>';
$query = "SELECT * FROM prepressjobs WHERE pj_deleted = 0 ORDER BY pj_datecreated DESC";
if (!$result = mysqli_query($db,$query)) {
exit(mysqli_error());
}
// if query results contains rows then featch those rows
if(mysqli_num_rows($result) > 0)
{
$number = 1;
while($row = mysqli_fetch_assoc($result))
{
$data .= "<tr>
<td>{$number}</td>
<td>{$row['pj_schednum']}</td>
<td>{$row['pj_itemname']}</td>
<td>{$row['pj_output']}</td>
<td>{$row['pj_datecreated']}</td>
<td>{$row['pj_expectedfinished']}</td>
<td>{$row['pj_preparedby']}</td>
<td>{$row['pj_status']}</td>
<td>
<div class='btn-group' role='group' aria-label='Button group with nested dropdown'>
<button onclick='GetJobDetails({$row['id']})' type='button' class='btn btn-primary'><span class='fa fa-gear'></span></button>
";
$querya = "SELECT COUNT(rm_schednum) AS jobrevision FROM jobrevisions WHERE rm_schednum ='{$row['pj_schednum']}'";
$resulta= mysqli_query($db, $querya);
$rowa = mysqli_fetch_assoc($resulta);
$x = $rowa['jobrevision'];
if ($x>=2) {
$data .= "<button onclick='CreateRevision({$row['id']})' type='button' class='btn btn-primary'><span class='fa fa-copy'></span></button>";
} else{}
$data .= "</div>
</td>
</tr>";
$number++;
}
}
else
{
// records now found
$data .= "<tr><td colspan='8'>Records not found!</td></tr>";
}
$data .= "</table>";
echo $data;
?>

How to retrieve data from database using Ajax and OOP php

I am going to develop web page with ajax and php. but there is problem in my get_data_in_table function. it will show data in a table structure. but it shows error when i run this code. others are working properly. it shows error in while loop. but i am unable to find any issue.
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
?>
I guess I found it:
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
} // This was missing
?>

Categories