Jump to content

Adamhumbug

Members
  • Posts

    597
  • Joined

  • Last visited

Everything posted by Adamhumbug

  1. I have rewritten the function and done exactly this. Closer looking and there were several problems with it. All of your pointers have got me to the bottom of it - thanks all.
  2. HI All, I have a php function. function removeItemFromQuote($itemId) { include 'includes/dbconn.php'; $sql = "DELETE from quote_items where id = :itemId"; $stmt = $pdo->prepare($sql); $stmt->execute([ ":itemId" => $itemId ]); $sql2 = "SELECT id, amount_charged_each, quantity, chargable_units from quote_items where quote_id = :qId"; $stmt2 = $pdo -> prepare($sql2); $stmt2 -> execute([ ':id' => $_GET['quoteId'] ]); $total = 0; while($row = $stmt2 -> fetch()){ $total += floatval($row['amount_charged_each']) * floatval($row['quantity']) * floatval($row['chargable_units']); } $sql3 = "UPDATE quote SET total_value = :total_value where id = :id"; $stmt3 = $pdo -> prepare($sql3); $stmt3 -> execute([ ':total_value' => $total, ':id' => $_GET['quoteId'] ]); } The first part is running, the item is being deleted. The second and third dont seem to be working, certainly the total_value column is not being updated. I am not seeing any errors and i have this at the top of the page calling the function and in my functions file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); Is there something obvious that i am doing wrong?
  3. I enjoyed this - ill try this one. Thanks for the detailed explanation though.
  4. String needs to be in quotes?
  5. I have a php function that creates a chunk of html including a onclick call. The onclick function curerntly just console.logs what has been passed in. I am getting a "uncaught SyntaxError: missing ) after argument list" error and i cant for the life of me figure out why. $out .="<tr class='align-middle custom-lc' onclick='triggerClientContextMenu($cId, $coName)'> <td> <div class='companyName'>$coName</div> ...... </td> </tr>"; When i inspect what is being put on the page i see <tr class="align-middle custom-lc" onclick="triggerClientContextMenu(1, Company Name)"> <td> <div class="companyName">Company Name</div> all looks good to me asside from the error in the console. Annoyingly, the error in the console doesnt take me to a useful line of code just to the <!DOCTYPE html>. when i remove this onclick from the PHP, i dont get any errors and everything runs fine. Any pointers?
  6. Hi All, I took on board your feedback here and basically rewrote the whole statement. I would share my resultant code but the whole query is now very different. I appreiciate your pointers and assistance as always.
  7. Yes, yes it should!! Thank You
  8. I have a function that lists out all items that should appear on a quote - i know the sql is correct as when i was using a whille($stmt -> fetch()...... everything worked fine. I am now building an array so that i can display it by a certain value but i am only getting 2 results output and there should be 16. $row = $stmt -> fetchAll(); foreach($row as $item){ if(isset($data[$item['sectionName']])){ $data[$item['sectionName']] = []; } $data[$item['sectionName']][]= ["itemName" =>$item['name'], "quantity" => $item['quantity']]; } foreach($data as $section => $items){ $out.= "<br/>".$section."<br/>"; foreach($items as $i){ $out.= "-".$i['itemName']; } } Below are the rows i am expecting to see. This is what is being output using $out = "<pre>".print_r($data,1)."</pre>";
  9. Apologies, i am using JQuery. $weeks = Math.floor($days/7) + ($days%7 >= 3 ? 1 : 0); The above is working beautifully.
  10. Thank you, this is awesome - helped a lot
  11. Ah, this is a PHP function? I am in JS at the mo
  12. Sorry my testing was wrong here - i am gettin intdiv is undefined. Looking into it - will report back once tested properly.
  13. Amazing - as ever!!
  14. Hi All, As i am trying to learn more and more i wanted to know if there was a way of looking at a raw fetchall() when running functions in php. I have a function call on my page which is set to var_dump the return. Is it possible to show the whole array on that page?
  15. Hi All, I have an insert query where sometimes all of the fields will have values to insert and sometimes they will not. I have allowed null on those fields in the database but i still get a warning (i have them turned on but ideally i would like to remedy the warning rather than turn it off). $stmt->execute([ ':quote_id' => $_GET['quoteid'], ':item_id' => $_POST['itemId'], ':quantity' => $_POST['quantity'], ':start_date' => $_POST['startDate'], ':end_date' => $_POST['endDate'] ]); Start and End date may not have values so i would like to pass in something intentional like 0 or NULL but im not sure how to do that or what doing so is called.
  16. Hi All, I have a rental system - if you rent for 3 or more days over a week you pay for 2 weeks, 3 or more days over 2 weeks and you pay for 3 and so on. As i need to be able to see how many weeks someone is renting equipment for i have built a js function but wondered if there was a more effience way of getting the weeks value than the following. if ($days == 0){ $weeks = 0 }else if ($days < 10) { $weeks = 1 } else if ($days >= 10 && $days < 16) { $weeks = 2 } else if ($days >= 17 && $days < 23) { $weeks = 3 } else if ($days >= 24 && $days < 30) { $weeks = 4 } Thanks as always
  17. Thank you all - issue resolved.
  18. Thats my issue right there - i was relying on the primary key (id) - i didnt realise that it would automatically increment when using this method.
  19. The value in question that is not working correctly is the small grey value - the others are set using queries from other functions called at the top of this one. I take your point on the prepared not being required and in terms of the bind column, i am brand new to PDO - actually made the switch due to @Barand suggestions over the years. I am still finding my feet.
  20. I will take your word for this as i am here for help - however, when i have logged out what this sql produces, it seems that i am getting the data that i expect and only this. When i change the data in the database and re run the query i am stil getting the data that i expect. if it helps here is the complete function. function showClientList(){ include 'includes/dbconn.php'; $grandTotal = sumAllQuoteValues(); $sumOpenAndAcceptedValueByClient = sumOpenAndAcceptedValueByClient(); echo "<pre>"; var_dump($sumOpenAndAcceptedValueByClient); echo "</pre>"; $sql = " SELECT client.id as clientid, company_name, country, DATE_FORMAT(client.date_created, '%d %M %Y') as date_created, sum(total_value) as tval from client left join quote on client.id = quote.client_id and 1 = quote.open group by client.id "; $stmt = $pdo -> prepare($sql); $stmt -> execute(); $stmt -> bindColumn('company_name', $coName); $stmt -> bindColumn('country', $country); $stmt -> bindColumn('date_created', $dCreated); $stmt -> bindColumn('tval', $tval); $stmt -> bindColumn('clientid', $cId); $out = ""; while ($stmt -> fetch()){ foreach ($sumOpenAndAcceptedValueByClient as $row){ if($row['client_id'] == $cId){ $valByClient = $row['sum(total_value)']; } } if($cId != null){ $created = new DateTime($dCreated); $now = new DateTime(date("Y-m-d H:i:s")); if($created->modify('+3 year') < $now){ $age = "Long Standing Client"; }else if ($created->modify('+2 year') < $now) { $age = "Established Client"; }else if ($created->modify('+1 year') < $now) { $age = "Second Year"; }else{ $age = "New Client"; } $percentageOfGrandTotal = ($valByClient/$grandTotal)*100; $roundedPercentage = round($percentageOfGrandTotal,2); $out .=<<<EOD <tr class='align-middle'> <td> <div>$coName</div> <div class='small text-medium-emphasis'><span>$age</span> | Created: $dCreated</div> </td> <td class='text-center'> <svg class='icon icon-xl'> <use xlink:href='vendors/@coreui/icons/svg/flag.svg#cif-$country'></use> </svg> </td> <td> <div class='clearfix'> <div class='float-start'> <div type="button" tabindex="0" data-coreui-toggle="popover" data-coreui-placement="top" data-coreui-custom-class="custom-popover" data-coreui-title="Potential New Revenue Value Of Client" data-coreui-trigger="focus" data-coreui-content="Total open quotes come to £$tval." class='fw-semibold'>£$tval</div> </div> <div type="button" tabindex="0" data-coreui-toggle="popover" data-coreui-placement="top" data-coreui-custom-class="custom-popover" data-coreui-title="Total Value Of Client" data-coreui-trigger="focus" data-coreui-content="Total open and accepted quotes come to £$valByClient." class='float-end'><small class='text-medium-emphasis'>£$valByClient</small></div> </div> <div class='progress progress-thin'> <div type="button" tabindex="0" data-coreui-toggle="popover" data-coreui-placement="top" data-coreui-custom-class="custom-popover" data-coreui-title="Total Value Of Client" data-coreui-trigger="focus" data-coreui-content="This client makes up $roundedPercentage% of total revenue." class='progress-bar bg-success' role='progressbar' style='width: $percentageOfGrandTotal%'></div> </div> </td> <td class='text-center'> <svg class='icon icon-xl'> <use xlink:href='vendors/@coreui/icons/svg/brand.svg#cib-cc-mastercard'></use> </svg> </td> <td> <div class='small text-medium-emphasis'>Active/Completed</div> <div class='fw-semibold'>2/8</div> </td> <td> <div class='dropdown'> <button class='btn btn-transparent p-0' type='button' data-coreui-toggle='dropdown' aria-haspopup='true' aria-expanded='false'> <svg class='icon'> <use xlink:href='vendors/@coreui/icons/svg/free.svg#cil-options'></use> </svg> </button> <div class='dropdown-menu dropdown-menu-end' style=''><a class='dropdown-item' href='#'>Info</a><a class='dropdown-item' href='#'>Edit</a><a class='dropdown-item text-danger' href='#'>Delete</a></div> </div> </td> </tr> EOD; } } return $out; }
  21. Hi All, I have a php function to insert data into my database which works fine but i want to update the record using the same button and have thus used on duplicate key update. if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['createNewQuote'])) { $sql = "INSERT into quote (name, internal_ref, currency, kit_delivery, kit_return, job_id) values (:name, :internal_ref, :currency, :kit_delivery, :kit_return, :jobId) ON DUPLICATE KEY UPDATE name=:name "; $stmt = $pdo->prepare($sql); $stmt->execute([ ':name' => $_POST['quoteName'], ':internal_ref' => $_POST['internalReference'], ':currency' => $_POST['currency'], ':kit_delivery' => $_POST['kitDeliveryDate'], ':kit_return' => $_POST['kitCollectionDate'], ':jobId' => $_POST['jobId'] ]); } } The issue that i have is that everytime i click the save button that triggers this function, i am getting a new row in the database created - is there something obvious i am doing wrong? I have attached the structure just in case that helps
  22. i have done that, i am now seeing: array(2) { [0]=> array(2) { ["client_id"]=> int(1) ["sum(total_value)"]=> string(4) "1000" } [1]=> array(2) { ["client_id"]=> int(2) ["sum(total_value)"]=> string(3) "211" } } But still have the extra value.
  23. Which outputs this array(2) { [0]=> array(4) { ["client_id"]=> int(1) [0]=> int(1) ["sum(total_value)"]=> string(4) "1000" [1]=> string(4) "1000" } [1]=> array(4) { ["client_id"]=> int(2) [0]=> int(2) ["sum(total_value)"]=> string(3) "211" [1]=> string(3) "211" } }
  24. it comes from this function function sumOpenAndAcceptedValueByClient(){ include 'includes/dbconn.php'; $sql = "SELECT client_id, sum(total_value) from quote where accepted = 1 or open = 1 group by client_id"; $stmt = $pdo-> prepare($sql); $stmt -> execute(); $out = $stmt -> fetchAll(); return $out; }
  25. Hi All, Sorry for the rubbish title, wasnt sure how else to word it. I have a select statement grabbing some info from my DB. $sql = " SELECT client.id as clientid, company_name, country, DATE_FORMAT(client.date_created, '%d %M %Y') as date_created, sum(total_value) as tval from client left join quote on client.id = quote.client_id and 1 = quote.open group by client.id "; $stmt = $pdo -> prepare($sql); $stmt -> execute(); $stmt -> bindColumn('company_name', $coName); $stmt -> bindColumn('country', $country); $stmt -> bindColumn('date_created', $dCreated); $stmt -> bindColumn('tval', $tval); $stmt -> bindColumn('clientid', $cId); $out = ""; i then have a while loop that is comparing the output from another function to match IDs and give a value to the output if they match. while ($stmt -> fetch()){ foreach ($sumOpenAndAcceptedValueByClient as $row){ if($row['client_id'] == $cId){ $valByClient = $row['sum(total_value)']; echo $cId; echo " ".$row['client_id']; echo " ".$row['sum(total_value)']; echo "<br/><br/>"; } } It seems to be working, however i have a client in the list that has no value so its sum(total_value) should be blank, actually it wont even show in the array i am foreaching. The issue is that in the table it is producing, it is giving me the same value of whatever comes before it - you see the £211 in the table image below. I have tried debugging this by echoing the values that are getting passed around which gives me the following output. 1 1 1000 2 2 211 I am therefore not sure why i am getting a value put into the last row. I have tried adding and else to the for each making it while ($stmt -> fetch()){ foreach ($sumOpenAndAcceptedValueByClient as $row){ if($row['client_id'] == $cId){ $valByClient = $row['sum(total_value)']; echo $cId; echo " ".$row['client_id']; echo " ".$row['sum(total_value)']; echo "<br/><br/>"; }else{ $valByClient = 0; } } that gives me the same echo values but now the first row =0 as well as the last. I am a bit stumped on this and would appreciate your help. If you need more info or for me to show more code, i will be happy to do so. As always, thanks in advance.
×
×
  • 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.