-
Posts
24,607 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
Why are you re-opening a old post and replying to someone who hasn't revisited for 10 years.
-
Disclaimer: just to say I am reluctantly aiding and abetting this db structure; it needs normalizing. Try <?php const HOST = 'localhost'; const USERNAME = '????'; const PASSWORD = '????'; const DATABASE = '????'; // default db function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } ################################################################################ ### CONNECT TO DB SERVER ### ################################################################################ $pdo = pdoConnect(); $j = '{"androidID":"6e5d819af1afb92d","cardAmount":7.149999999999999,"cashAmount":10.0, "list":[{"barcode":"1111117971111 5010018003165","cat_id":23,"cost":1.45,"id":0,"name":"1.45 CAN","payment_id":"","payment_type":"","product_id":2695,"product_type":"","qty":1,"staff_name":"","total":1.45}, {"barcode":"5054267000704 5054267000681 5449000125019 5449000124999 5038512005041 1111111141159 50271511 5449000107077 5038512000756 40822938 54492493 54493957","cat_id":23,"cost":1.85,"id":0, "name":"1.85 BOTTLE","payment_id":"","payment_type":"","product_id":2694,"product_type":"","qty":1,"staff_name":"","total":1.85}, {"barcode":"54491496 5000112628739","cat_id":23,"cost":1.95,"id":0,"name":"500ml Diet Coke","payment_id":"","payment_type":"","product_id":1758,"product_type":"","qty":1,"staff_name":"","total":1.95}, {"barcode":"1211111111111","cat_id":6,"cost":6.7,"id":0,"name":"WAFFLE 3 SCOOP","payment_id":"","payment_type":"","product_id":1254,"product_type":"","qty":1,"staff_name":"","total":6.7}, {"barcode":"1111111111160","cat_id":6,"cost":5.2,"id":0,"name":"WAFFLE 2 SCOOP","payment_id":"","payment_type":"","product_id":1252,"product_type":"","qty":1,"staff_name":"","total":5.2}], "paymentID":8646434759308,"paymentType":"Split"}'; $data = json_decode($j, 1); // put json data into an array try { $pdo->beginTransaction(); ################################################################################ ### TRANSACTION DATA ### ################################################################################ $tran_data = array_slice($data, 0, 3); $stmt = $pdo->prepare("INSERT INTO transaction (androidid, cardamount, cashamount) VALUES( :androidID, :cardAmount, :cashAmount) "); $stmt->execute($tran_data); ################################################################################ ### ITEM DATA ### ################################################################################ $stmt = $pdo->prepare("INSERT INTO sale_item (barcode, cat_id, cost, name, payment_id, payment_type, product_id, product_type, qty, staff_name, total) VALUES (:barcode, :cat_id, :cost, :name, :payment_id, :payment_type, :product_id, :product_type, :qty, :staff_name, :total) "); foreach ($data['list'] as $item) { unset($item['id']); if ($item['payment_id'] == '') $item['payment_id'] = null; // nullify missing int values if ($item['payment_type'] == '') $item['payment_type'] = null; if ($item['product_type'] == '') $item['product_type'] = null; $stmt->execute($item); } $pdo->commit(); } catch (PDOException $e) { $pdo->rollBack(); throw $e; } ?>
-
I'd love to help but I have no idea what your (five?) tables look like, and why each product has so many barcodes. I would expect it to be like this, given the data, but need to be sure ... +----------------+ | prod_barcode | +----------------+ +-----<| product_id (PK)| +--------------+ +----------------+ | | barcode (PK)| | transaction | | product | | +----------------+ +--------------+ +----------------+ | | id |---+ +-------| id |----+ | cash_amount | | | | product_type | | card_amount | | | | cat_id |>---+ | payment_type | | | | name | | | payment_id | | +--------------+ | | cost | | | staff_name | | | trans_item | | +----------------+ | +----------------+ +--------------+ | +--------------+ | | | category | | | id | | | +----------------+ +-----<| trans_id | | +-------| id | | product_id |>---+ | cat_name | | qty | +----------------+ +--------------+
-
Query your existing bookings and create an array... function getBookedSlots($pdo, $wkcomm) { $res = $pdo->prepare("SELECT datum , vreme FROM tehnicki WHERE datum BETWEEN ? AND ? + INTERVAL 6 DAY "); $res->execute([ $wkcomm, $wkcomm ]); $data = []; foreach ($res as $r) { $data[$r['datum']][$r['vreme']] = 1; } return $data; } $bookings [ '2023-07-05'][ '10:00' ] = 1; $bookings [ '2023-07-06'][ '11:30' ] = 1 then if ($d > $today && !isset($bookings[$dt][$ts])) { // clickable if not booked Incorporating into my previous code... <?php $duration = 45; $cleanup = 0; $start = "10:00"; $end = "15:15"; ################################################################################ # Default week commence date # ################################################################################ $d1 = new DateTime(); if ($d1->format('w') <> 1) { $d1->modify('last monday'); } $wkcomm = $_GET['week'] ?? $d1->format('Y-m-d'); $d1 = new DateTime($wkcomm); $week1 = $d1->sub(new DateInterval('P7D'))->format('Y-m-d'); $week2 = $d1->add(new DateInterval('P14D'))->format('Y-m-d'); function getBookedSlots($pdo, $wkcomm) { $res = $pdo->prepare("SELECT datum , vreme FROM tehnicki WHERE datum BETWEEN ? AND ? + INTERVAL 6 DAY "); $res->execute([ $wkcomm, $wkcomm ]); $data = []; foreach ($res as $r) { $data[$r['datum']][$r['vreme']] = 1; } return $data; } function timeslots($duration, $cleanup, $start, $end) { $start = new DateTime($start); $end = new DateTime($end); $duration += $cleanup; $interval = new DateInterval("PT".$duration."M"); return new DatePeriod($start, $interval, $end); } function daysOfWeek($comm) { $d1 = new DateTime($comm); return new DatePeriod($d1, new DateInterval('P1D'), 6); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html" charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <title>Tehnički pregled</title> <script type='text/javascript'> $(function() { // when page has loaded $(".tslot").click(function() { // define click event listener $("#tVreme").val( $(this).data("timeslot") ) $(".tslot").css('background-color', '#fff') $(this).css('background-color', '#ccc') }) }) </script> <style type='text/css'> td, th { padding: 4px; text-align: center; } th { background-color: black; color: white; } td { color: #999; } </style> </head> <body> <section class="header"> <div class="navbar"> <div class="logo"> <img src="images/logo.png"> <span>Tehnički pregled</span> </div> <div class="nav-links" id="navLinks"> <ul> <li><a aria-current="page" href="index.php">Naslovna</a></li> <li><a href="galerija.php">Galerija</a></li> </ul> </div> </div> <div class="headline"> <h1>Tehnički pregled</h1> <p>Odaberite termin i zakažite tehnički pregled svog vozila brzo i jednostavno na našem sajtu.</p> <a href="#termin" class="btn">Zakažite termin</a> </div> </section> <div id="myModal" class="modal"> <div class="login-box"> <p>Zakazivanje termina</p> <form method="POST"> <div class="user-box"> <input name="tIme" type="text" required="Please"> <label>Ime</label> </div> <div class="user-box"> <input name="tPrezime" type="text" required> <label>Prezime</label> </div> <div class="user-box"> <input name="tTelefon" type="text" required> <label>Broj telefona</label> </div> <div class="user-box"> <input name="tVreme" id="tVreme" type="text" required readonly> <label>Datum i vrijeme</label> </div> <br> <input type='submit' value='POŠALJI'> </form> </div> </div> <section class="content" id="termin"> <br><br> <a href='?week=<?=$week1?>'>Previous Week</a>   <a href='?week=<?=$week2?>'>Next Week</a> <br><br> <table> <?php #################################################################### # BUILD THE TABLE OF TIMESLOTS # #################################################################### $days = daysOfWeek($wkcomm); $times = timeslots($duration, $cleanup, $start, $end); $bookings = getBookedSlots($pdo, $wkcomm); // get current bookings $today = new DateTime(); // table headings echo "<tr>"; foreach ($days as $d) { echo '<th>' . $d->format('l<\b\r>d M Y') . '</th>'; } echo "</tr>\n"; // times foreach ($times as $t) { $ts = $t->format('H:i'); echo "<tr>"; foreach ($days as $d) { $dt = $d->format('Y-m-d'); if ($d > $today && !isset($bookings[$dt][$ts])) { // clickable if not booked $dt = $dt . ' ' . $ts; echo "<td><a href='#' class='tslot' data-timeslot='$dt' >$ts h</a><?td>"; } else { echo "<td>$ts</td>"; } } echo "</tr>\n"; } ?> </table> <span>Odaberite željeno vreme.</span> </section> <section class="footer"> <div class="social"> <ul> <li><a href="#"><img src="images/facebook.png" alt=""></a></li> <li><a href="#"><img src="images/twitter.png" alt=""></a></li> <li><a href="#"><img src="images/gmail.png" alt=""></a></li> </ul> </div> <span>Designed by Filip Glišović ©2023. - All rights reserved.</span> </section> </body> </html>
-
trying to insert multple form data into msyql database
Barand replied to jcarney1987's topic in PHP Coding Help
As gizmola said,name them as array items... name='item_number[$count]' Your post data will then look like this (for brevity, 3 recs with 3 fields) $_POST = Array ( [item_number] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [decription] => Array ( [0] => aaa [1] => bbb [2] => ccc ) [quantity] => Array ( [0] => 10 [1] => 20 [2] => 30 ) ) To make life easier when usin PDO prepared inserts, you could name them like this ... name='record[$count][item_number]' which gives post data like this... Array ( [record] => Array ( [0] => Array ( [item_number] => 1 [description] => aaa [quantity] => 10 ) [1] => Array ( [item_number] => 2 [description] => bbb [quantity] => 20 ) [2] => Array ( [item_number] => 3 [description] => ccc [quantity] => 30 ) ) ) wihich gives convenient arrays to use in your insertion statement $stmt->execute( $_POST['record'][$i] ); -
Try something like $j = '{"androidID":"6e5d819af1afb92d","cardAmount":7.149999999999999,"cashAmount":10.0, "list":[{"barcode":"1111117971111 5010018003165","cat_id":23,"cost":1.45,"id":0,"name":"1.45 CAN","payment_id":"","payment_type":"","product_id":2695,"product_type":"","qty":1,"staff_name":"","total":1.45}, {"barcode":"5054267000704 5054267000681 5449000125019 5449000124999 5038512005041 1111111141159 50271511 5449000107077 5038512000756 40822938 54492493 54493957","cat_id":23,"cost":1.85,"id":0, "name":"1.85 BOTTLE","payment_id":"","payment_type":"","product_id":2694,"product_type":"","qty":1,"staff_name":"","total":1.85}, {"barcode":"54491496 5000112628739","cat_id":23,"cost":1.95,"id":0,"name":"500ml Diet Coke","payment_id":"","payment_type":"","product_id":1758,"product_type":"","qty":1,"staff_name":"","total":1.95}, {"barcode":"1211111111111","cat_id":6,"cost":6.7,"id":0,"name":"WAFFLE 3 SCOOP","payment_id":"","payment_type":"","product_id":1254,"product_type":"","qty":1,"staff_name":"","total":6.7}, {"barcode":"1111111111160","cat_id":6,"cost":5.2,"id":0,"name":"WAFFLE 2 SCOOP","payment_id":"","payment_type":"","product_id":1252,"product_type":"","qty":1,"staff_name":"","total":5.2}], "paymentID":8646434759308,"paymentType":"Split"}'; $data = json_decode($j, 1); // put json data into an array $required = ['androidID', 'cardAmount', 'cashAmount']; foreach ($required as $k) { echo "<b>$k : </b>{$data[$k]}<br>"; } echo '<hr>'; foreach ($data['list'] as $prod) { foreach ($prod as $k => $v) { echo "<b>$k : </b>$v<br>"; } echo '<hr>'; }
-
I was offering up a method rather than a spoonfed solution. This is a fuller solution incorporating the dates <?php $duration = 45; $cleanup = 0; $start = "10:00"; $end = "15:15"; ################################################################################ # Default week commence date # ################################################################################ $d1 = new DateTime(); if ($d1->format('w') <> 1) { $d1->modify('last monday'); } $wkcomm = $_GET['week'] ?? $d1->format('Y-m-d'); $d1 = new DateTime($wkcomm); $week1 = $d1->sub(new DateInterval('P7D'))->format('Y-m-d'); $week2 = $d1->add(new DateInterval('P14D'))->format('Y-m-d'); function timeslots($duration, $cleanup, $start, $end) { $start = new DateTime($start); $end = new DateTime($end); $duration += $cleanup; $interval = new DateInterval("PT".$duration."M"); return new DatePeriod($start, $interval, $end); } function daysOfWeek($comm) { $d1 = new DateTime($comm); return new DatePeriod($d1, new DateInterval('P1D'), 6); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html" charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <title>Tehnički pregled</title> <script type='text/javascript'> $(function() { // when page has loaded $(".tslot").click(function() { // define click event listener $("#tVreme").val( $(this).data("timeslot") ) }) }) </script> <style type='text/css'> td, th { padding: 4px; text-align: center; } th { background-color: black; color: white; } </style> </head> <body> <section class="header"> <div class="navbar"> <div class="logo"> <img src="images/logo.png"> <span>Tehnički pregled</span> </div> <div class="nav-links" id="navLinks"> <ul> <li><a aria-current="page" href="index.php">Naslovna</a></li> <li><a href="galerija.php">Galerija</a></li> </ul> </div> </div> <div class="headline"> <h1>Tehnički pregled</h1> <p>Odaberite termin i zakažite tehnički pregled svog vozila brzo i jednostavno na našem sajtu.</p> <a href="#termin" class="btn">Zakažite termin</a> </div> </section> <div id="myModal" class="modal"> <div class="login-box"> <p>Zakazivanje termina</p> <form method="POST"> <div class="user-box"> <input name="tIme" type="text" required="Please"> <label>Ime</label> </div> <div class="user-box"> <input name="tPrezime" type="text" required> <label>Prezime</label> </div> <div class="user-box"> <input name="tTelefon" type="text" required> <label>Broj telefona</label> </div> <div class="user-box"> <input name="tVreme" id="tVreme" type="text" required readonly> <label>Datum i vrijeme</label> </div> <br> <input type='submit' value='POŠALJI'> </form> </div> </div> <section class="content" id="termin"> <br><br> <a href='?week=<?=$week1?>'>Previous Week</a>   <a href='?week=<?=$week2?>'>Next Week</a> <br><br> <table> <?php #################################################################### # BUILD THE TABLE OF TIMESLOTS # #################################################################### $days = daysOfWeek($wkcomm); $times = timeslots($duration, $cleanup, $start, $end); $today = new DateTime(); // table headings echo "<tr>"; foreach ($days as $d) { echo '<th>' . $d->format('l<\b\r>d M Y') . '</th>'; } echo "</tr>\n"; // times foreach ($times as $t) { $ts = $t->format('H:i'); echo "<tr>"; foreach ($days as $d) { if ($d > $today) { $dt = $d->format('Y-m-d') . ' ' . $ts; echo "<td><a href='#' class='tslot' data-timeslot='$dt' >$ts h</a><?td>"; } else { echo "<td> </td>"; } } echo "</tr>\n"; } ?> </table> <span>Odaberite željeno vreme.</span> </section> <section class="footer"> <div class="social"> <ul> <li><a href="#"><img src="images/facebook.png" alt=""></a></li> <li><a href="#"><img src="images/twitter.png" alt=""></a></li> <li><a href="#"><img src="images/gmail.png" alt=""></a></li> </ul> </div> <span>Designed by Filip Glišović ©2023. - All rights reserved.</span> </section> </body> </html>
-
<body> Timeslot : <input id='timeslot' value=''> <br><br> <a href="#" class='tslot' data-timeslot="2023-07-02 19:00" >19:00</a> </body> script <script type='text/javascript'> $(function() { // when page has loaded $(".tslot").click(function() { // define click event listener let timeslot = $(this).data("timeslot") $("#timeslot").val(timeslot) }) }) </script>
-
I would create a hidden input named "timeslot" in the form. Add a "data-timeslot" attribute to each of your timeslot links with a datetime value for the slot EG... <td><a href="#" onclick="MyFunction();return false;" data-timeslot="<?=$ts->format('Y-m-d H:i')?>" ><?=$ts->format('H:i \h')?></a></td> Have MyFunction() put the data value into the form field. In your timeslots() function I would use a DatePeriod object instead of a for() loop function timeslots($duration, $cleanup, $start, $end){ $start = new DateTime($start); $end = new DateTime($end); $duration += $cleanup; $interval = new DateInterval("PT".$duration."M"); $slots = []; $dp = new DatePeriod($start, $interval, $end); return $dp; } Don't use PHP_SELF. Just leave out the page reference - default is self)
-
mySQL "union all" queries on tables with varying number of columns
Barand replied to ChenXiu's topic in PHP Coding Help
Same as any other column. echo $row['columnName'); In my example above, you would echo $row('ffff'); echo $row('gggg']; echo $row['source']; All the queries in a union must have the same number of columns, and respective columns in each must be of the same type. So you can't select an integer as the first column in one and the select a varchar as the first column in the next query. -
Yes, the function specified in the each() is applied to each element that has class "textarea-container"
-
how to recognize and sort sub-strings in a string?
Barand replied to zoofreek's topic in PHP Coding Help
Can you post an example of an "irregular character string" that you want to process? -
try $res = $pdo->query("SELECT user_review as rvw FROM review "); foreach ($res as $r) { echo "<textarea class='textarea-container'>{$r['rvw']}</textarea><br>"; } ?> <script type='text/javascript'> $(function() { $(".textarea-container").each(function(k,v) { let scrht = v.scrollHeight $(v).height(scrht) }) }) </script>
-
Your post suggests a single text area, so why are you creating a text area for each row?
-
mySQL "union all" queries on tables with varying number of columns
Barand replied to ChenXiu's topic in PHP Coding Help
The first query defines the column names for all results in a union, so in your second example you will have columns ffff, gggg only. Use a dummy identifying column SELECT description as ffff , NULL as gggg , 'A' as source FROM myTable WHERE sku = '1234' UNION ALL SELECT description , weight , 'B' FROM my_other_Table WHERE sku = '1234' [edit] Why have you got multiple tables with the same columns in the first place? ??? -
Your JS script operates on the textarea with id = textarea-container. You create a text area with this id value for each result row. Ids have to be unique - you can have only a single element with a particular id and JS will expect there to be only one. Consider using a classname rather than an id when you have many similar elements.
-
‘ ’ != ' '
-
My main reason is that PDO is consistent. In mysqll, when you use the query() method it produces a result object; when you use prepare() it produces a statement object. It looks like mysqli was developed by two teams who never spoke to ane another as the methods within these two objects are completely different, so you have to process results differently. In PDO you always get a statement object so processing is always the same. The PDO methods are cleaner to use for prepared statements. PDO can be used with many different RDBMS, so if you want to change, the php remains constant (although you may have to adjust the SQL dialect in your queries). Did you read the link I gave you?
-
Welcome. You'll be wanting to look at prepared statements then. Take the PDO route and not mysqli - it's much better.
-
Provide a dump of the table structure and test data.
-
Could really use some basic assistance, please!
Barand replied to WorldwideGaming's topic in PHP Coding Help
Your browser should not be showing any php code if it is executed correctly. Are you just opening the file in the browser (like a text file)? You need to open it as a url, eg http://localhost/myfile.php -
Could really use some basic assistance, please!
Barand replied to WorldwideGaming's topic in PHP Coding Help
You're best bet is to turn on display_errors in your php.ini file and also set the error reporting level to E_ALL. See what error messages you get. -
You need to define the required precision $totalgoal2 = number_format(min($num_rows['count'], 49)/49*100, 2); ^