Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. You reference $result on line 1 before defining it on line 3 or 7 A function exits when return is called. Whether the if() returns true or false you return from the function, so the final line can never be reached.
  2. It works as expected when I run the code
  3. Indeed there is. In fact the email address should not be in two places. The only item that would appear in more than one table is the ID. Just JOIN the tables on the user_id when you need info from both tables.
  4. DO NOT post a question in more than one forum. If you want help, you need to provide more details about the problem. What have you tried so far? What are you having a problem with?
  5. Synopsis, brief, menu, digest, .. ?
  6. If that is the offending code, where is the hidden "test" input of which you speak? You need to post the whole of the code for the form, from <form> to </form>
  7. Why are you re-opening a old post and replying to someone who hasn't revisited for 10 years.
  8. 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; } ?>
  9. 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 | +----------------+ +--------------+
  10. 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> &emsp; <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ć &copy2023. - All rights reserved.</span> </section> </body> </html>
  11. 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] );
  12. 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>'; }
  13. 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> &emsp; <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>&nbsp;</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ć &copy2023. - All rights reserved.</span> </section> </body> </html>
  14. <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>
  15. 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)
  16. 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.
  17. Yes, the function specified in the each() is applied to each element that has class "textarea-container"
  18. Can you post an example of an "irregular character string" that you want to process?
  19. 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>
  20. Your post suggests a single text area, so why are you creating a text area for each row?
  21. 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? ???
  22. 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.
  23. see https://www.php.net/manual/en/language.types.string.php
  24. ‘ ’ != ' '
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.