-
Posts
24,551 -
Joined
-
Last visited
-
Days Won
821
Community Answers
-
Barand's post in Extract email address from Stripe API was marked as the answer
The string you posted appears to be missing its enclosing { .. }. With those it becomes a correctly formatted JSON string...
$details = '{"customer_details":{"email":"emailaddress@gmail.com","tax_exempt":"none","tax_ids":[]}}'; you can then decode the string...
$data = json_decode($details); then access the email address with...
$email = $data->customer_details->email;
-
Barand's post in Creating neat little sub-array from big array was marked as the answer
I would ...
foreach ($array as $k => $v) { $id = substr($k, -5); if (ctype_digit($id)) { $len = strlen($k); $k = substr($k, 0, $len-5); $sku[$id][$k] = $v; } else $sku[$k] = $v; } echo '<pre>' . print_r($sku, 1) . '</pre>'; giving...
Array ( [77777] => Array ( [newQuantity] => 3 [newPrice] => 5 [usedQuantity] => 1 [usedPrice] => 3.99 [total] => 18.99 ) [88888] => Array ( [newQuantity] => 0 [newPrice] => 0 [usedQuantity] => 4 [usedPrice] => 12 [total] => 48 ) [44444] => Array ( [newQuantity] => 2 [newPrice] => 4 [usedQuantity] => 0 [usedPrice] => 3.99 [total] => 8 ) [date] => July 25 2021 [address] => 123 Anystreet Avenue [address2] => [zipcode] => 90210 [city] => Beverly Hills [state] => CA [planet] => Mars )
-
Barand's post in MYSQL Insert: Whatever's inside JSON array? was marked as the answer
Here's a shortened version. It also uses json_decode without the "true" to give an object (containing objects)
$all = json_decode($all_items_sold); $placeholders = $values = []; foreach ($all as $pid => $prod){ if (in_array($pid, $referred_by_Affiliate)) { $placeholders[] = "(?,?,?)"; array_push($values, $pid, $prod->item, $prod->price); } } $stmt = $db->prepare("INSERT INTO mytable (prod_id, item, price) VALUES " . join(',', $placeholders)); $stmt->execute($values);
A PHP join has nothing to do with MySQL joins.
-
Barand's post in PHP not installed correctly? was marked as the answer
Your file name is index.html and not index.php.
You need a php file for php code to be executed.
-
Barand's post in Problem transferring data between pages using PHP session was marked as the answer
What if no new value has been posted but your session value already contains 50?
Perhaps...
$get_ppp = $_POST['ppp'] ?? $_SESSION['ppp'] ?? 15; $_SESSION['ppp'] = $get_ppp;
-
Barand's post in Get ID from jobs and insert it to Bookings was marked as the answer
You're making it difficult for yourself.
The tour/job name should not be in the booking table. The only place that should occur in your database is in the tour/job table. It is just the tour id that be in the booking table.
+------------------+ | booking | +----------------+ +------------------+ | tour/job | | booking_id | +----------------+ | tour_id |>----------------| tour_id | | cust_name | | tour_name | | cust_address | +----------------+ | order_at | | ... | | etc | +------------------+ When you create your dropdown options, the values of the options should be the id and not the name.
Then all you do is insert $_POST['tour_name'] (which will actually be the id) into the booking data. Job done.
-
Barand's post in FullCalendar not displaying data from MySQL was marked as the answer
Do not use SELECT *
Specify what you want and your (PDO) code becomes
$query = "SELECT id , name as title , dep_date as start , ret_date as end FROM tours ORDER BY id"; $result = $con->query($query); echo json_encode($result->fetchAll());
-
Barand's post in Selecting from duplicate values but wanting the max was marked as the answer
Try
TABLE: production_data_archive; +----+----------+------------+-----------+---------------------+ | id | order_id | job_number | line_item | insert_time | +----+----------+------------+-----------+---------------------+ | 1 | 16824 | 22000412 | A | 2021-03-26 00:00:00 | | 2 | 16824 | 22000412 | A | 2021-03-30 00:00:00 | | 3 | 16824 | 22000412 | A | 2021-04-09 00:00:00 | | 4 | 16825 | 22000412 | B | 2021-03-26 00:00:00 | | 5 | 16825 | 22000412 | B | 2021-03-29 00:00:00 | | 6 | 16825 | 22000412 | B | 2021-04-06 00:00:00 | +----+----------+------------+-----------+---------------------+ SELECT a.id , a.order_id , a.job_number , a.line_item , a.insert_time FROM production_data_archive a LEFT JOIN production_data_archive b ON a.job_number = b.job_number AND a.line_item = b.line_item AND a.insert_time < b.insert_time WHERE b.job_number IS NULL; +----+----------+------------+-----------+---------------------+ | id | order_id | job_number | line_item | insert_time | +----+----------+------------+-----------+---------------------+ | 3 | 16824 | 22000412 | A | 2021-04-09 00:00:00 | | 6 | 16825 | 22000412 | B | 2021-04-06 00:00:00 | +----+----------+------------+-----------+---------------------+
-
Barand's post in Best of 2 Tables? Most Efficient. was marked as the answer
SELECT fruit , MIN(price) as price FROM ( SELECT fruit, price FROM fruit1 UNION ALL SELECT fruit, price FROM fruit2 ) x WHERE fruit = 'grape'; +-------+------------+ | fruit | price | +-------+------------+ | grape | 3.00 | +-------+------------+
-
Barand's post in See number of different types of booking per organisation was marked as the answer
I'lll state my assumptions
1 ) The data looks like this
prs_o prs_op prs_pr +------+-----------+ +-------+---------+ +-------+----------+------------+--------------+ | o_id | o_name | | op_id | op_o_id | | pr_Id | pr_op_id | pr_ptd_id | pr_ip2ptd_id | +------+-----------+ +-------+---------+ +-------+----------+------------+--------------+ | 1 | Company A | | 1 | 1 | | 1 | 1 | 641 | NULL | | 2 | Company B | | 2 | 1 | | 2 | 2 | 641 | NULL | | 3 | Company C | | 3 | 1 | | 3 | 3 | NULL | 1101 | +------+-----------+ | 4 | 1 | | 4 | 4 | NULL | 1101 | | 5 | 2 | | 5 | 5 | NULL | 1101 | | 6 | 2 | | 6 | 6 | NULL | 1101 | | 7 | 2 | | 7 | 7 | NULL | 1101 | | 8 | 3 | | 8 | 8 | NULL | 1101 | | 9 | 3 | | 9 | 9 | NULL | 1101 | | 10 | 3 | | 10 | 10 | NULL | 1101 | | 11 | 3 | | 11 | 11 | NULL | 1101 | | 12 | 3 | | 12 | 12 | 641 | NULL | | 13 | 3 | | 13 | 13 | 641 | NULL | +-------+---------+ +-------+----------+------------+--------------+ 2 ) The table and join structure looks like this
+--------------+ +--------------+ +--------------+ | prs_o | | prs_op | | prs_pr | +--------------+ +--------------+ +--------------+ | o_id |-----\ | op_id |------\ | pr_id | | o_name | \---<| op_o_id | \-----<| pr_op_id | +--------------+ +--------------+ | pr_ptd_id | | pr_ip2ptdid | +--------------+ Query
SELECT o_id , o_name , SUM(pr_ptd_id = 641) as Tot641 , SUM(pr_ip2ptd_id = 1101) as Tot1101 FROM prs_pr pr JOIN prs_op op ON pr.pr_op_id = op.op_id JOIN prs_o o ON op.op_o_id = o.o_id GROUP BY o.o_id; Results
+------+-----------+--------+---------+ | o_id | o_name | Tot641 | Tot1101 | +------+-----------+--------+---------+ | 1 | Company A | 2 | 2 | | 2 | Company B | | 3 | | 3 | Company C | 2 | 4 | +------+-----------+--------+---------+
-
Barand's post in Web UI like Windows View? was marked as the answer
You could use CSS, for example
#table1 { transform : scale(0.5); } Simple example
Code
-
Barand's post in Help with code was marked as the answer
If you want to return all jobs for all employees each time it is called, the yes.
If you just want jobs for that employee, then no.
Please use code tags button "<>" when posting code and use indentations to render it more readable
-
Barand's post in Looking for solution for how to execute SQL for running total... was marked as the answer
If you want it in a single query, initialize the variables in a joined subquery
SELECT , (@csumA := @csumA + A) as cumulative_A , (@csumM := @csumM + M) as cumulative_M , (@csumE := @csumE + E) as cumulative_E , (@csumW := @csumW + W) as cumulative_W FROM ( SELECT WEEK(s.date) week, SUM(CASE WHEN s.user_id = 50 THEN s.points ELSE 0 END) AS A, SUM(CASE WHEN s.user_id = 51 THEN s.points ELSE 0 END) AS M, SUM(CASE WHEN s.user_id = 52 THEN s.points ELSE 0 END) AS E, SUM(CASE WHEN s.user_id = 53 THEN s.points ELSE 0 END) AS W FROM users u JOIN scores s ON u.user_id = s.user_id JOIN league l ON l.league_id = s.league_id AND and l.league_name = 'Sunday League' WHERE year(s.date) = YEAR(sysdate()) GROUP BY s.date ORDER BY s.date ASC ) PTS JOIN ( SELECT @csumA:=0, @csumM:=0, @csumE := 0, @csumW:=0 ) INIT;
-
Barand's post in Filter Php Array from json was marked as the answer
Since you want to filter an array, I suggest array_filter()
$times = [ '2021-06-02T19:40:00Z', '2021-06-03T02:10:00Z', '2021-06-03T01:10:00Z', '2021-06-02T23:05:00Z', '2021-06-02T23:05:00Z', '2021-06-02T23:07:00Z', '2021-06-02T23:20:00Z', '2021-06-02T18:20:00Z', '2021-06-03T00:10:00Z', '2021-06-03T00:40:00Z' ]; $d = new DateTime('23:59:59', new DateTimeZone('Z')); $newtimes = array_filter($times, function($v) use ($d) { return new DateTime($v) <= $d; });
-
Barand's post in How to Save Results from Foreach into Txt File was marked as the answer
Open the txt file before the loop and close it after the loop.
-
Barand's post in Select id that has more than one value in an in clause was marked as the answer
Your hopes are not in vain
select person_id , zones from ( select person_id , zones , find_in_set('301', zones) > 0 as has301 , find_in_set('401', zones) > 0 as has401 , find_in_set('501', zones) > 0 as has501 from ahtest ) checks WHERE has301 + has401 + has501 > 1;
-
Barand's post in Multiple pdf was marked as the answer
It doesn't let you create a PDF once output has already been sent, therefore creating multiple pdfs in a single script doesn't appear to be an option.
As an alternative I would suggest
give users the option to select each product and create their pdfs separately, or create one pdf but put each product on a separate page Also, I prefer to put my pdf code in a separate file and create the pdf via a "create PDF" link and give the user the option of viewing or downloading the PDF.
This version gives both the above options
aswin_1.php
<?php // // // CREATE YOUR OWN MYSQL CONNECTION HERE // // // function fetch_data($conn){ $output=''; $sql="SELECT * FROM product"; $result=mysqli_query($conn,$sql); $sno=0; while($row=mysqli_fetch_array($result)){ $output.= '<tr> <td><a href="aswin_2.php?product=' . $row["product_id"] . '">' . $row["product_id"] . '</a></td> <td>' .$row["product_name"].'</td> </tr>'; } return $output; } ?> <!DOCTYPE html> <html> <head> <title>Pdf Generation</title> <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <br> <table style='width:400px; margin: 50px auto;'> <tr> <th>Id</th> <th>Name</th> </tr> <?php echo fetch_data($conn); ?> </table> <div style='width: 400px; margin: 20px auto;'> <a href="aswin_2.php?product=all" download>All products</a> </body> </html>
aswin_2.php (creates the pdf)
<?php // // // CREATE YOUR OWN MYSQL CONNECTION HERE // // // require_once '../tcpdf/tcpdf.php'; if (!isset($_GET['product'])) { exit; } if ($_GET['product']=='all') { $stmt = $conn->query("SELECT product_id , product_name FROM product ORDER BY product_id "); $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false); $obj_pdf->SetCreator(PDF_CREATOR); $obj_pdf-> SetTitle("Product PDF"); $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN)); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA)); $obj_pdf->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT); $obj_pdf->SetPrintHeader(false); $obj_pdf->SetPrintFooter(false); $obj_pdf->SetAutoPageBreak(TRUE,10); $obj_pdf -> SetFont('helvetica', '',12); foreach ($stmt as $row) { $obj_pdf->AddPage(); $content=''; $content .='<h4 align="center">Generate Pdf</h4><br> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Id</th> <th>Name</th> </tr>'; $content .= "<tr> <td>{$row['product_id']}</td> <td>{$row['product_name']}</td> </tr> "; $content .= '</table>'; $obj_pdf->writeHTML($content); } $obj_pdf->Output('All Products','D'); } else { $stmt = $conn->prepare("SELECT product_id , product_name FROM product WHERE product_id = ? "); $stmt->bind_param('s', $_GET['product']); $stmt->execute(); $stmt->bind_result($product_id, $product_name); $stmt->fetch(); $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false); $obj_pdf->SetCreator(PDF_CREATOR); $obj_pdf-> SetTitle("Product PDF"); $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN)); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA)); $obj_pdf->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT); $obj_pdf->SetPrintHeader(false); $obj_pdf->SetPrintFooter(false); $obj_pdf->SetAutoPageBreak(TRUE,10); $obj_pdf -> SetFont('helvetica', '',12); $obj_pdf->AddPage(); $content=''; $content .='<h4 align="center">Generate Pdf</h4><br> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Id</th> <th>Name</th> </tr>'; $content .= "<tr> <td>$product_id</td> <td>$product_name</td> </tr> "; $content .= '</table>'; $obj_pdf->writeHTML($content); $obj_pdf->Output("Product $product_id",'D'); } ?>
-
Barand's post in Multiple pdf was marked as the answer
It doesn't let you create a PDF once output has already been sent, therefore creating multiple pdfs in a single script doesn't appear to be an option.
As an alternative I would suggest
give users the option to select each product and create their pdfs separately, or create one pdf but put each product on a separate page Also, I prefer to put my pdf code in a separate file and create the pdf via a "create PDF" link and give the user the option of viewing or downloading the PDF.
This version gives both the above options
aswin_1.php
<?php // // // CREATE YOUR OWN MYSQL CONNECTION HERE // // // function fetch_data($conn){ $output=''; $sql="SELECT * FROM product"; $result=mysqli_query($conn,$sql); $sno=0; while($row=mysqli_fetch_array($result)){ $output.= '<tr> <td><a href="aswin_2.php?product=' . $row["product_id"] . '">' . $row["product_id"] . '</a></td> <td>' .$row["product_name"].'</td> </tr>'; } return $output; } ?> <!DOCTYPE html> <html> <head> <title>Pdf Generation</title> <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <br> <table style='width:400px; margin: 50px auto;'> <tr> <th>Id</th> <th>Name</th> </tr> <?php echo fetch_data($conn); ?> </table> <div style='width: 400px; margin: 20px auto;'> <a href="aswin_2.php?product=all" download>All products</a> </body> </html>
aswin_2.php (creates the pdf)
<?php // // // CREATE YOUR OWN MYSQL CONNECTION HERE // // // require_once '../tcpdf/tcpdf.php'; if (!isset($_GET['product'])) { exit; } if ($_GET['product']=='all') { $stmt = $conn->query("SELECT product_id , product_name FROM product ORDER BY product_id "); $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false); $obj_pdf->SetCreator(PDF_CREATOR); $obj_pdf-> SetTitle("Product PDF"); $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN)); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA)); $obj_pdf->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT); $obj_pdf->SetPrintHeader(false); $obj_pdf->SetPrintFooter(false); $obj_pdf->SetAutoPageBreak(TRUE,10); $obj_pdf -> SetFont('helvetica', '',12); foreach ($stmt as $row) { $obj_pdf->AddPage(); $content=''; $content .='<h4 align="center">Generate Pdf</h4><br> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Id</th> <th>Name</th> </tr>'; $content .= "<tr> <td>{$row['product_id']}</td> <td>{$row['product_name']}</td> </tr> "; $content .= '</table>'; $obj_pdf->writeHTML($content); } $obj_pdf->Output('All Products','D'); } else { $stmt = $conn->prepare("SELECT product_id , product_name FROM product WHERE product_id = ? "); $stmt->bind_param('s', $_GET['product']); $stmt->execute(); $stmt->bind_result($product_id, $product_name); $stmt->fetch(); $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false); $obj_pdf->SetCreator(PDF_CREATOR); $obj_pdf-> SetTitle("Product PDF"); $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN)); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA)); $obj_pdf->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT); $obj_pdf->SetPrintHeader(false); $obj_pdf->SetPrintFooter(false); $obj_pdf->SetAutoPageBreak(TRUE,10); $obj_pdf -> SetFont('helvetica', '',12); $obj_pdf->AddPage(); $content=''; $content .='<h4 align="center">Generate Pdf</h4><br> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Id</th> <th>Name</th> </tr>'; $content .= "<tr> <td>$product_id</td> <td>$product_name</td> </tr> "; $content .= '</table>'; $obj_pdf->writeHTML($content); $obj_pdf->Output("Product $product_id",'D'); } ?>
-
Barand's post in Can't get a real value with: document.getElementById(keyword1).value; was marked as the answer
Are you sure you don't want the values for cost and quantity?
-
Barand's post in Problem with sub query was marked as the answer
OK. I wanted to know if I was on the right track.
SELECT pd.enterprise, COUNT(*) AS total FROM production_data pd LEFT JOIN production_status ps ON ps.order_id = pd.id AND ps.dept_code = 13 AND ps.status_id = 3 WHERE pd.enterprise = "EXXON" AND pd.as400_ship_date = '2021-03-02' AND pd.hold_date = "0000-00-00" AND ps.order_id IS NULL;
-
Barand's post in exceptions with prepare was marked as the answer
Too many "="s.
$stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?"); ^ REMOVE EDIT: Turn on your error reporting.
-
Barand's post in Get information from array was marked as the answer
foreach (array as $recno => $record) { echo "<h3>Record $recno</h3>"; foreach ($record as $field => $value) { echo "$field : $value<br>"; } }
-
Barand's post in sql query issues was marked as the answer
Define "it's not working".
Are you getting a PHP error, or a MySQL error message?
Why are you ordering by stat twice?
As an aside, your case statement looks weird in that you give numeric vlue to all statuses to sort on but as a default you have the string value "Cancelled". Have you considered making the stat column type ENUM?
Or, perhaps
ORDER BY FIELD(stat, 'Checked in', 'Booked', 'Deposit Confirmation', 'Email/phone', 'Checked Out', 'Cancelled')
-
Barand's post in LAN Web page worked PHP 5, not happy with PHP 7. was marked as the answer
This reply to an earlier post may be relevant to your problem