-
Posts
594 -
Joined
-
Last visited
Community Answers
-
Adamhumbug's post in Seeking Advice - Data Structure was marked as the answer
function submitPass($pdo) { $personId = $_SESSION['portalUId'] ?? null; if ($personId === null) { return json_encode(['status' => 'error', 'message' => 'Invalid or missing person ID.']); } // Start transaction $pdo->beginTransaction(); try { // Retrieve existing person data $sqlGetPerson = "SELECT person_json FROM person WHERE id = :person_id"; $stmtGetPerson = $pdo->prepare($sqlGetPerson); $stmtGetPerson->execute([':person_id' => $personId]); $personData = $stmtGetPerson->fetchColumn(); $personData = $personData ? json_decode($personData, true) : []; // Process each form data entry foreach ($_POST as $key => $value) { if (strpos($key, 'passId-') === 0) { $passTypeId = explode('-', $key)[1]; $sqlPass = "INSERT INTO pass_submission (pass_type_id, person_id, pass_submission_json) VALUES (:pass_type_id, :person_id, :pass_json) ON DUPLICATE KEY UPDATE pass_submission_json = :update_pass_json"; $stmtPass = $pdo->prepare($sqlPass); $stmtPass->execute([ ':pass_type_id' => $passTypeId, ':person_id' => $personId, ':pass_json' => $value, ':update_pass_json' => $value ]); } elseif (strpos($key, 'passForm-') === 0) { $formId = explode('-', $key)[1]; $formData = json_decode($value, true)[0]; $personData['person_data'] = array_merge($personData['person_data'] ?? [], $formData); $sqlForm = "INSERT INTO form_submission (person_id, form_id, form_submission_json, status) VALUES (:person_id, :form_id, :form_json, 'pending') ON DUPLICATE KEY UPDATE form_submission_json = :update_form_json"; $stmtForm = $pdo->prepare($sqlForm); $stmtForm->execute([ ':person_id' => $personId, ':form_id' => $formId, ':form_json' => $value, ':update_form_json' => $value ]); } } // Update the person data in the person table $updatedPersonJson = json_encode($personData); $sqlUpdatePerson = "UPDATE person SET person_json = :person_json WHERE id = :person_id"; $stmtUpdatePerson = $pdo->prepare($sqlUpdatePerson); $stmtUpdatePerson->execute([':person_json' => $updatedPersonJson, ':person_id' => $personId]); $pdo->commit(); // Commit all changes return json_encode(['status' => 'success', 'message' => 'All data processed successfully.']); } catch (Exception $e) { $pdo->rollBack(); // Roll back on error return json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]); } } What i went with in the end was exactly this. I passed the formID for each form through and looped them.
-
Adamhumbug's post in Output php function in JS output was marked as the answer
I am sorry all, i am having a bad month.
Below is the answer
$(document).on('click', '#newFieldRow', function() { console.log('clicked') $html = `<div class='row mb-3'> <div class='col'> <input type='text' class='form-control w-100' name='code[]' required> </div> <div class='col'> <input type='text' class='form-control w-100' name='label[]' required> </div> <div class='col'> <input type='number' class='form-control w-100' name='width[]' min='1' max='12' required> </div> <div class='col'> <?= showDataClassifications($pdo, 'select') ?> </div> <div class='col'> <select class='form-select' name='type[]' required > <option disabled selected value='0'>Please Select</option> <?= getFieldTypes($pdo, $field['Type']) ?> </select> </div> <div class='col-1 text-center'> <i class='fa-solid fa-ban'></i> </div> </div>` $('.fieldContainer').append($html) })
-
Adamhumbug's post in Getting Name of Array was marked as the answer
foreach ($data as $cards) { $cards = json_decode($data['cards'], true); $count = sizeof($cards); $out = ''; if ($count == 4) { $size = "col-xl-3 col-md-6"; } else if ($count == 3) { $size = "col-xl-4 col-md-4"; } else if ($count == 2) { $size = "col-xl-6 col-md-6"; } else if ($count == 1) { $size = "col-12"; } foreach ($cards as $cardName => $cardDetails) { $icon_code = $icons[$cardDetails['Icon']]; $out .= "<div class='$size mb-3'> <div class='card editHeroCard' data-card='$cardName'> <div class='card-body'> <div class='text-center card-title'> <i class='$icon_code fa-2xl'></i> </div> <div class='card-text text-center my-3 fw-bold'>$cardDetails[Name]</div> <div class='card-text text-center'>$cardDetails[Paragraph]</div> </div> </div> <div class='handle text-center'>GRAB</div> </div>"; }
-
Adamhumbug's post in MariaDb JSON Joining Tables was marked as the answer
moving it above the for each does work - thank you x1000 for this. I dont think i would have ever got to this.
This is the complete working function:
function getHeroCardsByPortalId($pdo) { $res = $pdo->query("SELECT code, id FROM icon"); $icons = array_column($res->fetchAll(), 'code', 'id'); //avoiding the need to join the tables $sql = "SELECT json_extract(portal_content_json, '$.\"Portal Content\".\"Pages\".\"Home\".\"Hero\".\"Hero Cards\"') as cards FROM portal_content where portal_attachment_id = :portalId"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':portalId' => $_GET['portalId'] ]); $data = $stmt->fetch(); if (!$data) { return "<div class='col-12'>No hero cards available!</div>"; } foreach ($data as $cards => $row) { $cards = json_decode($row, true); $count = sizeof($cards); $out = ''; if ($count == 4) { $size = "col-xl-3 col-md-6"; } else if ($count == 3) { $size = "col-xl-4 col-md-4"; } else if ($count == 2) { $size = "col-xl-6 col-md-6"; } else if ($count == 1) { $size = "col-12"; } usort($cards, fn ($a, $b) => $a['Order'] <=> $b['Order']); foreach ($cards as $card) { $icon_code = $icons[$card['Icon']]; $out .= "<div class='$size mb-3'> <div class='card editHeroCard' data-order-number='$card[Order]'> <div class='card-body'> <div class='text-center card-title'> <i class='$icon_code fa-2xl'></i> </div> <div class='card-text text-center my-3 fw-bold'>$card[Name]</div> <div class='card-text text-center'>$card[Paragraph]</div> </div> </div> <div class='handle text-center'>GRAB</div> </div>"; } } return $out; }
-
Adamhumbug's post in Modal Launching Without Content was marked as the answer
turn out the show modal needed to be in the success of the ajax - makes perfect sense..
function generateFormModal($formId) { console.log($formId) $.ajax({ type: 'post', data: { 'ajax': 'generateFormModal', 'formId': $formId }, success: function(resp) { $('#createNewFormModal').html(resp) $('#createNewFormModal').modal('show') } }) }
-
Adamhumbug's post in UPDATE - JOIN on WHERE clause was marked as the answer
I knew i was close:
UPDATE quote_items qi inner join items i on qi.item_id = i.id set qi.discounted_price = qi.amount_charged_each * .5, qi.discount_percentage = .5 where qi.quote_id = 89 and i.discountable = 1
-
Adamhumbug's post in Returning HTML not running PHP function was marked as the answer
fixed with
".selectAllItemsBySection()." Thanks Barry for that pointer.
-
Adamhumbug's post in Break a document onto another page was marked as the answer
In the end i created a script that did what i wanted.
$('.section').each(function() { var $el = $(this); var bottom = $el.position().top + $el.outerHeight(true); if (bottom > 950) { $('#pageContent2').append($el) } }) It grabs any element that appears below the page end and moved it into the next page.
I am very sure this is a hack and not the cleanest solution, but it does do what i need.
As always, i am greatful for your feedback and pointers.
-
Adamhumbug's post in Submit large form created with select statement was marked as the answer
So, thank you all for your support here, i am now able to submit this large form successfully.
The submission code is as follows.
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); if (session_status() == PHP_SESSION_NONE) { session_start(); } if (!isset($_SESSION['user_name'])){ header("location: index.php"); } include '../_includes/dbconn.php'; if ($_SERVER['REQUEST_METHOD']=='POST') { $jobId = $_SESSION['current_job_id']; $qty = $_POST['equipmentQty']; // prepare insert query $stmt = $conn->prepare("INSERT INTO ssm_equipment_order (job_id, equipment_id, equipment_quantity) VALUES (?,?,?) "); foreach ($_POST['equipmentId'] as $k => $eid) { if ($qty[$k] > 0) { // $data = [$jobId, $eid, $qty[$k] ] ; $stmt->bind_param("sss", $jobId, $eid, $qty[$k]); $stmt->execute(); } } } header("location: ../order-equipment.php"); My issue now is that when this page loads, it needs to get the information out of the database that builds the large form and fill the inputs with the quantities that were submitted before.
I have written the following:
<?php include '_includes/dbconn.php'; $current_job_id = $_SESSION['current_job_id']; $sql = "SELECT * FROM ssm_equipment_order where job_id = $current_job_id"; if ($result = mysqli_query($conn, $sql)){ if(mysqli_num_rows($result)>0){ $sql = "SELECT * FROM ssm_equipment"; if ($result = mysqli_query($conn, $sql)){ if(mysqli_num_rows($result)>0){ echo "<form method='post' action='actions/submit-equipment-order-action.php' id='equipmentOrderForm'>"; echo "<div class='input-group mt-3 mb-3'>"; echo "<div class='input-group-prepend'>"; echo "<span class='input-group-text'>Search (alt/option+s)</span>"; echo "</div>"; echo "<input id='equipmentTableSearch' onkeyup='searchEquipmentTable()' type='text' class='form-control' placeholder='Plates...Soup Spoon...Red Wine Glass...'>"; echo "<button type='submit' name='equipment_submit_button' class='btn btn-primary'>Submit</button'>"; echo "</div>"; echo "<table id='equipmentTable' class='mt-3 table table-striped table-hover table-bordered'>"; echo "<thead>"; echo "<tr class='text-center'><th>Equipment</th><th>Quantity</th></tr>"; echo "</thead>"; echo "<tbody>"; $current_job_id = $_SESSION['current_job_id']; $sql2 = "SELECT * FROM ssm_equipment_order WHERE job_id = $current_job_id"; $result2 = mysqli_query($conn, $sql2); while ($row2 = mysqli_fetch_array($result2)){ echo "XX "; } while ($row = mysqli_fetch_array($result)){ echo "<tr>"; echo "<td style='width:70%;'>".$row['equipment_name']."</td>"; echo "<input type='hidden' name='equipmentId[]' value='".$row['equipment_id']."'>"; echo "<td class='text-center'><input name='equipmentQty[]' class='eqQty text-center up' type='text'></td>"; echo "</tr>"; } echo "</tbody>"; echo "</table>"; echo "</form>"; } } }else{ //the above is doing an if there is an equipment order already $sql = "SELECT * FROM ssm_equipment"; if ($result = mysqli_query($conn, $sql)){ if(mysqli_num_rows($result)>0){ echo "<form method='post' action='actions/submit-equipment-order-action.php' id='equipmentOrderForm'>"; echo "<div class='input-group mt-3 mb-3'>"; echo "<div class='input-group-prepend'>"; echo "<span class='input-group-text'>Search (alt/option+s)</span>"; echo "</div>"; echo "<input id='equipmentTableSearch' onkeyup='searchEquipmentTable()' type='text' class='form-control' placeholder='Plates...Soup Spoon...Red Wine Glass...'>"; echo "<button type='submit' name='equipment_submit_button' class='btn btn-primary'>Submit</button'>"; echo "</div>"; echo "<table id='equipmentTable' class='mt-3 table table-striped table-hover table-bordered'>"; echo "<thead>"; echo "<tr class='text-center'><th>Equipment</th><th>Quantity</th></tr>"; echo "</thead>"; echo "<tbody>"; while ($row = mysqli_fetch_array($result)){ echo "<tr>"; echo "<td style='width:70%;'>".$row['equipment_name']."</td>"; echo "<input type='hidden' name='equipmentId[]' value='".$row['equipment_id']."'>"; echo "<td class='text-center'><input name='equipmentQty[]' class='eqQty text-center up' type='text'></td>"; echo "</tr>"; } echo "</tbody>"; echo "</table>"; echo "</form>"; } } } } ?> The following line is successfully showing the correct number of results, for how many items have a qty but i do not know how to use this information to populate the correct text boxes in the table.
while ($row2 = mysqli_fetch_array($result2)){ echo "XX "; }
-
Adamhumbug's post in Simple Clone Row was marked as the answer
HI Both,
Thanks for your info here - before seeing your replies, i wrote the following which seems to do what i want but as you said, it copies all of the content with it.
I will have a look at cleaning this up and will post the new version once finalized.
i=0; function myF() { console.log(i) var chunk = document.getElementById('newMenuItemLine'+i); var clone = chunk.cloneNode(true); i++; chunk.setAttribute("id", "newMenuItemLine" +i); document.getElementById('wrapper').appendChild(clone); }
-
Adamhumbug's post in Load more content button JS and PHP was marked as the answer
i went this route in the end and didnt bother with data attributes.
function getMoreNews(limit, offset){ $.ajax({ type: 'post', data: {"ajax" : 'one', "offset" : offset, "limit" : limit}, success: function(resp){ offset = offset + 1 limit = limit + 1 $('.moreNews').append(resp + "<div class='btn btn-secondary w-100 text-center loadMoreNewsTrigger mb-3' onclick='getMoreNews(1,"+offset+");' data-limit='1' data-offset="+offset+">-- Load More News --</div>") } }) };
-
Adamhumbug's post in Inserting Array into MySQL was marked as the answer
Turns out that i have solved this one myself also, the issue was that i was passing a string in the array and not integers. When i changed the data type to 'is' rather than 'ii' the insert worked.
-
Adamhumbug's post in AJAX Success returning whole page html was marked as the answer
Turned out to be a miss-capitalised bit of data
createNewgame should have been createNewGame
Sorry all.
Thanks for looking anyway.
-
Adamhumbug's post in Html function call on keyup - get keycode from input was marked as the answer
I am sorry all, as is always the case i have answered by own question shortly after posting.
function barCodeVal(e){ var code = e.keyCode; console.log(e.keyCode); } thanks anyway, i am sure i will be back.