Group two column and distinct - php

I tried this code to output this (Correct Output)
But it gave me this which is wrong output
I need to distinct the name and group the item by brand but it did not happen in my query see the picture above.
$datefilter=mysqli_query($link,"select
SUM(INVOICE_ITEM_AMOUNT_MX) AS totalamount,
INVOICE_NO_MX,
INVOICE_CUSTOMER_NAME
,INVOICE_DATE_MX,ITEM_BRAND_MX
,GROUP_CONCAT(INVOICE_ITEM_CODE_MX SEPARATOR '<BR>') as itemcode
,GROUP_CONCAT(INVOICE_ITEM_QTY_MX SEPARATOR '<BR>') as quantity
,GROUP_CONCAT(INVOICE_ITEM_UPRICE_MX SEPARATOR '<BR>') as price
,GROUP_CONCAT(INVOICE_ITEM_SERIAL_MX SEPARATOR '<BR>') as serial
,GROUP_CONCAT( DISTINCT (INVOICE_SALES_REP_MX) SEPARATOR '<BR>') as salesrep
,GROUP_CONCAT(DISTINCT (ITEM_BRAND_MX) SEPARATOR '<BR>') as brand
from invoice where INVOICE_DATE_MX BETWEEN '" .$datefrom. "' AND '" . $dateto ."' and
BRANCH_CODE_MX='".$display_branchcode."' AND INVOICE_STATUS_MX='PAID' GROUP BY INVOICE_SALES_REP_MX,ITEM_BRAND_MX ");
$count=mysqli_num_rows($datefilter);
echo '<div class="col-xs-12">
<br>
<div class="col-xs-6">
<label> Total Invoice Number: </label> '.$count.'
</div>
<div class="col-xs-6">
<label> Date Range: </label> '.$convertedfrom.' - '.$convertedto.'
</div>
</div>';
while($daterow=mysqli_fetch_array($datefilter)){
$date=$daterow["INVOICE_DATE_MX"];
$customername=$daterow["INVOICE_CUSTOMER_NAME"];
$invoiceref=$daterow["INVOICE_NO_MX"];
$salesrep=$daterow["salesrep"];
$itemcode=$daterow["itemcode"];
$imeiserial=$daterow["serial"];
$totalnotformat=$daterow["totalamount"];
$upricenotformat=$daterow["price"];
$quantity=$daterow["quantity"];
$itembrand=$daterow["brand"];
$total=number_format($totalnotformat,2);
$uprice=number_format($upricenotformat,2);
echo'
<div class="col-xs-12">
<br>
<div class="panel panel-warning">
<div id="shade" class="panel-body"><label>'.$salesrep.'</label></div>
<div id="shade" class="panel-body">
<div class="col-xs-10">
<div class="col-xs-4">';
echo '
<label>Brand</label>: '.$itembrand.'<br>
<div class="col-xs-7">
<label>Item Code</label> <br>'.$itemcode.'
</div>
<div class="col-xs-1">
<label> Quantity</label> <br>'.$quantity.'
</div>
</div>
</div>
</div>

I guess, the following should help and what you wanted I mean to show distinct names. But you could do it with PHP easily:
SELECT CASE WHEN t.id = (SELECT t2.id FROM process t2
WHERE t2.projectNo = t.projectNo
GROUP BY t2.projectNo LIMIT 1) THEN t.projectNo
ELSE
''
END AS Deatils, t.stuff
FROM process t
Sample input and output:

Related

Create date range report column using codeigniter 3 excel

I want to create export file date-range report using ci.
views.php
<div class="form-group row">
<label for="attn_date" class="col-sm-4 col-form-label">From</label>
<div class="col-sm-8">
<input type="date" class="form-control" id="attn_from" name="attn_from">
<?= form_error('attn_from'); ?>
</div>
</div>
<div class="form-group row">
<label for="attn_date" class="col-sm-4 col-form-label">To</label>
<div class="col-sm-8">
<input type="date" class="form-control" id="attn_to" name="attn_to">
<?= form_error('attn_to'); ?>
</div>
</div>
<div class="form-group row">
<label for="daily_export_file" class="col-sm-4 col-form-label">Export Data</label>
<div class="col-sm-8">
<?= form_dropdown('export_file', ['report' => 'Report'], set_value('export_file'), 'class="form-control" id="export_file"'); ?>
<?= form_error('export_file'); ?>
</div>
</div>
controller.php
$querydata = $this->db->query("select a.*, b.* from db_attn a
LEFT JOIN tbl_wrkhour b
ON b.id_wrkhour = a.wrk_hour WHERE attn_date between '". $this->input->post('attn_from')."'
AND '". $this->input->post('attn_to')."'order by attn_date desc")->result();
$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet_name = $spreadsheet->getActiveSheet()->setTitle("Attendance Report");
$sheet = $spreadsheet->getActiveSheet();
$highestRow = $sheet->getHighestRow();
$highestColumm = $sheet->getHighestColumn();
$dataattn = $querydata;
$sheet->setCellValue('A5', 'No');
$sheet->setCellValue('B5', 'Employee Name');
$sheet->setCellValue('C5', 'Date');
$sheet->setCellValue('D5', 'Detail');
$no = 1;
$rowx = 6;
foreach ($dataattn as $rowattn) {
$sheet->setCellValue('A' . $rowx, $no++);
$sheet->setCellValue('B' . $rowx, $rowattn->emp_name);
$sheet->setCellValue('C' . $rowx, $rowattn->date);
$sheet->setCellValue('D' . $rowx, $rowattn->attn_detail);
}
I've successfully show date range of all employee details. How to transpose the detail of each date?
current view in my export excel file:
No
Employee Name
Date
Detail
1
aaaa
03-01-2023
Present
2
bbbb
03-01-2023
leave
3
cccc
03-01-2023
late
4
aaaa
04-01-2023
Present
5
bbbb
04-01-2023
Present
6
cccc
04-01-2023
Present
expected change:
No
Employee Name
03-01-2023
04-01-2023
next column date range
1
aaaa
Present
Present
next
2
bbbb
leave
Present
next
3
cccc
late
Present
next
Any idea? Please help...
You can probably get that from SQL directly
select
no,
emp_name,
case
when exists (select user was present on time on 03-01-2023) then 'Present'
when exists (select user was present late on 03-01-2023) then 'Late'
else 'Absent'
end as '03-01-2023',
....
create a helper array, fill it in foreach function with rules and colums, then set this array to $dataattn.

Display inline arrays

var html3 = '<div class="row" id="parent-autor"> <p id="auth-del"><i class="fa fa-minus-square" aria-hidden="true"></i></p> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" name="altautorprenume[]" placeholder="Prename"> </div> </div> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" name="altautornume[]" placeholder="Name"> </div> </div><div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" name="institutie[]" id="institutie" placeholder="Institutions"> </div> </div></div>';
$("#add_aut").click(function(e){
$('#auth-rows').append(html3);
});
$('#auth-rows').on('click', '#auth-del', function(E){
$(this).parent('div').remove();
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<div class="add-plus"></div>
<div class="col-md-12">
<h5>Add more persons </h5>
<span class="btn btn-primary" id="add_aut" style="">Click here to add</span>
</div>
</div>
<div id="auth-rows"></div>
i have an formular which user can add more clients to invite at party when user click on Add a new row with 3 fields appear he can add how many he want,
these values dynamically i want to show on email, for example:
User fill all fields and add 3 more users where they have Name PreName Instituional,look what i did:
$personName= $_POST['personname'];
$personPrename= $_POST['personprename'];
$institution= $_POST['institution'];
$otherPersonName=" ";
$otherPersonPreName="";
$otherPersonInstitution= "";
foreach($personName as $product) {
$otherPersonPreName.= "$product";
}
foreach($personPrename as $products) {
$altiautoriNume .= "$products";
}
foreach($institution as $productss) {
$otherPersonInstitution.= "$productss";
}
EXAMPLE:
And in email content i want to display like this:
<hr>
John(first name of person added) Doe(first Prename of person added) Microsoft(first Institution of person added)
<hr>
Mike(second name of person added) Stuart(second Prename of person added) Facebook(second Institution of person added)
<hr>
I know i did something wrong.
I put even html to see truly what i want to do, i want to group Name Prename Institution per line
You can use array-map with null as first parameter to group the array and then use implode to concat the strings.
Consider the following:
$arr = array_map(null, $personName, $personPrename, $institution);
foreach($arr as $e)
echo implode(" ", $e) . PHP_EOL;
Live example: 3v4l

Associate a text field with a primary key in a row using codeigniter

I am trying to build a bidding application. I want to a associate a textfield with the primary key of the row outputted. e.g textfield is bid_amount.
When entering the bid amount I want the bid_amount entered and the person who bid (truckerid) and the product bid on (luggage_id) to be stored in a bids table.
The outputted products to bid for are stored in another table (consignment table) Below is my model, view and controller.
View
<h2 class="page-header">
<i class="fa fa-legal"></i> &nbsp <font color="orange"> Loads To Bid For </font>
</h2>
</div><!-- /.col -->
</div>
<?php
foreach ($h->result() as $row) {}
?>
<div class="row invoice-info">
<div class="col-sm-1 invoice-col">
</div>
<div class="col-sm-3 invoice-col">
<img src="<?php echo base_url()?>/res/images/goods/1.png">
</div>
<div class="col-sm-4 invoice-col">
<address>
Description: <?php echo $row->description;?><br>
Location Address: <?php echo $row->l_area;?><br>
Destination Address: <?php echo $row->d_area;?><br>
Date: <?php echo $row->dom;?><br>
Time: <?php echo $row->tom;?>
</address>
</div>
<div class="col-sm-2 invoice-col">
<address>
</address>
</div><!-- /.col -->
<div class="col-sm-2 invoice-col">
<?php echo form_open('truckeraccount_ctrl/bid'); ?>
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="text" class="form-control" name="bid_amount[$row->luggage_id]" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</div>
</div>
Controller
public function bid() {
$this->load->model('Truckeraccount_model');
// $luggage_id = $this->uri->segment(3);
$this->Truckeraccount_model->bid();
redirect('truckeraccount_ctrl');
}
Model
function bid() {
$data = array(
'luggage_id' => $this->input->post('luggage_id'),
'bid_amount' => $this->input->post('bid_amount'),
'truckerid' => $this->input->post('truckerid')
);
//$this->db->where('luggage_id', $luggage_id);
$query=$this->db->update('bids', $data);
return $query;
}
You can use the input as an associative array by changing the name. This will make the keys in the array the product ids
try
<input type="text" name="bidAmount[$row->productId]" />
then in your PHP you can loop through the values
foreach ($_POST['bidAmount'] as $productId => $bidAmount) {
// do your logic here
}
You can use hidden text box to get bid.
<?php
foreach ($products->result() as $row)
{ ?>
<?php echo $row->price;?>
<input type="hidden" name="bid[]" value="<?php echo $row->bid;?>">
<input type="text" name="bidAmount[]" placeholder="place bid amount">
<?php } ?>
Now bid and bidAmount both will have same counts of array. So that use for loop to get bid value and bidAmount value.
for ($i=0; $i < count($_POST['bid']); $i++) {
$bid = $_POST['bid'][$i]; \\ You can get bid value here
$bidAmount = $_POST['bidAmount'][$i]; \\ You can get bid amount value here
}
Hope this answer will be helpful. If it is correct then don't forget to mark as correct answer. Cheers.

How to add an PHP IF statement in this Script?

$sql = "SELECT * FROM item";
$stmt = $conn->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$data['result_2'] .= '
<div class="col-sm-4 col-md-4">
<div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
<div class="content-boxes-text">
<form action="php/additem.php" method="post" class="form-inline pull-right">
<h4>'.$row['itemName'].'</h4><input type="hidden" name="itemName" value="'.$row['itemName'].'">
<img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
<p>'.$row['description'].'</p><input type="hidden" name="description" value="'.$row['description'].'">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Qty</label>
<div class="input-group">
<input type="number" name="qty" class="form-control" id="exampleInputAmount" placeholder="How Many?">
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<!-- //.content-boxes-text -->
</div>
<!-- //.content-boxes -->
</div>
';
}
I want to be able to add an if statement in this result_2 string.
This is for displaying a product, and i would like to display price depending on users session value?
eg.
if ($_SESSION['customer_x'] == a) {
display price a
}
else if ($_SESSION['customer_x'] == b) {
display price b
}
Is this the correct method to be able to add an if statement to a JSON query?
After Starting your while loop, put a if else there,
$price ="";
if ($__SESSION['customer_x'] == a) {
$price='display price a';
}
else if ($_SESSION['customer_x'] == b) {
$price='display price b';
}
and now echo this price where ever you want to in your html
this is more neet and less messy way
You can use a ternary if operator to have conditional statements inside strings, example
$bool = false;
echo "The value of \$bool is " . ($bool == true ? "TRUE" : "FALSE");
Example in use
To add that you can keep doing the same but:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ // the next of the content
$priceOfSession = ($__SESSION['customer_x'] == a) ? 'pricea' : 'priceb';
$data['result_2'] .= '
<div class="col-sm-4 col-md-4">
'.$priceOfSession.'
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<!-- //.content-boxes-text -->
</div>
</div>
';
}
So if you want to evaluate only two conditions if otherwise the if you want, simply add it there. Do as you add the $ row but before defining value.

Linking data within MySQL tables into 1 table with PHP Code

Background info:
I am trying to create a digital job raising system. Admin raises a job sheet and pulls in a client from "tblclient" table, fills out the details of the job and then assigns the job sheet to a employee user.
With this being said I am having difficulty pulling data from the clients table and users table then linking them to a row in the jobs table. I am new to PHP coding and am unsure how to script it out so my user can select the client and all the client details are attached to the job sheet and then coding it so the job sheet can be moved around between users.
This is so that when the job is complete the Admin can preview the job sheet with all the client details on it, job details and print with all the information on screen.
Is there any tutorials I can use for this procedure?
Notes:
I currently have a form that will insert the job data into the job table, I just don't know what code to use to pull in users/clients and link it to that specific job.
Thanks
<?php
include 'core/init.php';
include 'include/overall/overall_header.php';
protect_page();
admin_protect();
$today_date = date("Y-m-d");
?>
<div id="container">
<div id="content-container">
<div id="sidenav">
<?php include 'include/sidenav.php'; ?>
</div>
<div id="content">
<h1>Add a new job</h1>
<?php echo $today_date; ?>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
echo 'Job added successfully!';
} else {
if (empty($_POST) === false && empty($errors) === true) {
$job_data = array(
'date' => $_POST['date'],
'description' => $_POST['description'],
'artworkbrief' => $_POST['artworkbrief'],
'extracosts' => $_POST['extracosts'],
'stock1' => $_POST['stock1'],
'orderno1' => $_POST['orderno1'],
'ordered1' => $_POST['ordered1'],
'stock2' => $_POST['stock2'],
'orderno2' => $_POST['orderno2'],
'ordered2' => $_POST['ordered2'],
'stock3' => $_POST['stock3'],
'orderno3' => $_POST['orderno3'],
'ordered3' => $_POST['ordered3'],
'subtotal' => $_POST['subtotal'],
'extracosts1' => $_POST['extracosts1'],
'total' => $_POST['total']
);
raise_job($job_data);
header('Location: raise_job.php');
exit();
}
}
?>
<select id="clientname" name="clientname">
<?php
$pdo = new PDO('mysql:host=localhost;dbname=formation_ims', 'root', 'form8tion');
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('Select clientname from tblclients');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['clientname'].'</option>';
}
?>
</select>
<div id="job-sheet">
<form action="" method="post">
<div class="row-3">
<div class="description">
<p>Description:</p>
<textarea type="text" name="description"></textarea>
</div>
</div>
<div class="row-4">
<div class="artwork-brief">
<p>Artwork Brief:</p>
<textarea type="text" name="artworkbrief"></textarea>
</div>
</div>
<div class="row-5">
<div class="extra-costs-container">
<div class="extra-costs">
<p>Extra Costs:</p>
<textarea type="text" name="extracosts"></textarea>
</div>
</div>
<div class="total-container">
<div class="sub-total">
Sub Total:
<input type="text" name="subtotal">
</div>
</div>
<div class="extra-num-container">
<div class="extra-costs-num">
Extra Costs:
<input type="text" name="extracosts1">
</div>
</div>
</div>
<div class="row-6">
<div class="stock-material-container">
<div class="stock-material">
<p>Stock/Material:</p>
<div class="stock-material-row">
<div class="stock-material-col01">
<input type="text" name="stock1">
</div>
<div class="stock-material-col02">
Order No.<input type="text" name="orderno1">
</div>
<div class="stock-material-col03">
Ordered:<input type="checkbox" name="ordered1">
</div>
</div>
<div class="stock-material-row">
<div class="stock-material-col01">
<input type="text" name="stock2">
</div>
<div class="stock-material-col02">
Order No.<input type="text" name="orderno2">
</div>
<div class="stock-material-col03">
Ordered:<input type="checkbox" name="ordered2">
</div>
</div>
<div class="stock-material-row">
<div class="stock-material-col01">
<input type="text" name="stock3">
</div>
<div class="stock-material-col02">
Order No.<input type="text" name="orderno3">
</div>
<div class="stock-material-col03">
Ordered:<input type="checkbox" name="ordered3">
</div>
</div>
</div>
</div>
<div class="total-container">
<div class="sub-total">
Sub Total:
<input type="text" name="subtotal">
</div>
</div>
<div class="extra-num-container">
<div class="extra-costs-num">
Extra Costs:
<input type="text" name="extracosts1">
</div>
</div>
</div>
</div>
<input type="submit" value="Submit">
</form>
</div>
</div>
<div class="clear"></div>
<?php include 'include/overall/overall_footer.php'; ?>
ahh yes, I understand what this code is doing but not familiar with the syntax. I got an error:
Parse error: syntax error, unexpected ';' in...
I think you left out an ")" but even after reinstating it, my dropdown menu isn't populated:
<select name='clientid'>
<?php
$query="SELECT * FROM tblclients";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row['Client_ID']."'>".stripslashes($row['ClientName']." </option>";
}
?>
`
When inputting your job, you will want a field in your form containing clientid and another containing userid. The following is an example of how that might be done.
<select name='clientid'>
<?php
$query="SELECT * FROM ClientTable";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row['ClientID']."'>".stripslashes($row['ClientName']."</option>";
}
?>
</select>
Then when you want to output the job with all it's information, when you run the query you should use the LEFT JOIN command, for example
$query="SELECT * FROM JobTable
LEFT JOIN UserTable ON UserTable.UserID=JobTable.UserID
LEFT JOIN ClientTable ON ClientTable.ClientID=JobTable.ClientID
WHERE JobID='".mysql_real_escape_string($jobid)."'";
$result=mysql_query($query) or die(mysql_error());
That grabs the appropriate client and user informations by their IDs (which I hope you've set up with auto-increment by the way)

Categories