Jump to content

All Activity

This stream auto-updates

  1. Past hour
  2. Yes, you can't put double quotes inside a double-quoted string without excaping them. mysql> select * from json_test where id = 2; +----+-----------------------------------------------------------------------------------------------------------------------+ | id | jstuff | +----+-----------------------------------------------------------------------------------------------------------------------+ | 2 | {"Portal Content": {"Colours": {"Primary": "Red", "Secondary": "Green", "Tertiary": "Blue", "Quaternary": "String"}}} | +----+-----------------------------------------------------------------------------------------------------------------------+ $stmt = $pdo->prepare("UPDATE json_test SET jstuff = JSON_SET(jstuff, ?, ?, ?, ?, ?, ?, ?, ?) WHERE id = ? "); $stmt->execute([ '$."Portal Content"."Colours"."Primary"', 'Orange', '$."Portal Content"."Colours"."Secondary"', 'Limegreen', '$."Portal Content"."Colours"."Tertiary"', 'Hotpink', '$."Portal Content"."Colours"."Quaternary"', 'Cornsilk', 2 ]); mysql> select * from json_test where id = 2; +----+-----------------------------------------------------------------------------------------------------------------------------------+ | id | jstuff | +----+-----------------------------------------------------------------------------------------------------------------------------------+ | 2 | {"Portal Content": {"Colours": {"Primary": "Orange", "Secondary": "Limegreen", "Tertiary": "Hotpink", "Quaternary": "Cornsilk"}}} | +----+-----------------------------------------------------------------------------------------------------------------------------------+
  3. Escaping seemed to work here function savePortalOptions($pdo, $defaultColour, $primaryColour, $secondaryColour) { $portalId = $_GET['portalId']; $sql = "UPDATE portal_content SET portal_content_json = JSON_SET(portal_content_json, '$.\"Portal Content\".\"Colours\".\"Primary\"', :primaryColour, '$.\"Portal Content\".\"Colours\".\"Secondary\"', :secondaryColour, '$.\"Portal Content\".\"Colours\".\"Default\"', :defaultColour) WHERE portal_attachment_id = :portalId"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':defaultColour' => $defaultColour, ':primaryColour' => $primaryColour, ':secondaryColour' => $secondaryColour, ':portalId' => $portalId ]); return "Portal Options Updated"; }
  4. Today
  5. This is my current function for reference. function savePortalOptions($pdo, $defaultColour, $primaryColour, $secondaryColour) { $portalId = $_GET['portalId']; $sql = "UPDATE portal_content SET portal_content_json = JSON_SET(portal_content_json, '$."Portal Content"."Colours"."Primary"', :primaryColour, '$."Portal Content"."Colours"."Secondary"', :secondaryColour, '$."Portal Content"."Colours"."Default"', :defaultColour,) WHERE portal_attachment_id = :portalId"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':defaultColour' => $defaultColour, ':primaryColour' => $primaryColour, ':secondaryColour' => $secondaryColour, ':portalId' => $portalId ]); return "Portal Options Updated"; }
  6. So this works great putting it directly into phpmyadmin but when trying to put this into php as a prepared statement and put the variables in, i run into an issue with the quotes used. I have tried combinations of single, double and backticks but cannot get this to run from php.
  7. Yesterday
  8. or UPDATE portal_content SET portal_content_json = JSON_SET(portal_content_json, '$."Portal Content"."Colours"."primary"', 'limegreen', '$."Portal Content"."Colours"."secondary"', 'hotpink'), '$."Portal Content"."Colours"."tertiary"', 'orange') WHERE `portal_attachment_id` = 25 ; When all else fails... https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html
  9. This is the update example from my earlier post UPDATE json_test SET jstuff = JSON_SET(jstuff, '$."card".4."cardName"', 'This is a different card') WHERE id = 1; Note use of ' and " . I'll need to experiment further. One way would be to update all colors in one JSON_SET UPDATE portal_content SET portal_content_json = JSON_SET(portal_content_json, '$."Portal Content"."Colours"', '{"primary":"limegreen", "secondary":"hotpink", "tertiary":"orange", "quaternary":"lavender"}') WHERE `portal_attachment_id` = 25;
  10. Is that going to be an issue in the php $sql ="UPDATE portal_content SET portal_content_json = JSON_SET(portal_content_json, "$.'Portal Content'.'Colours'.'Primary'", 'Green') where `portal_attachment_id` = 25"; $stmt = $pdo->prepare($sql); im not going to be able to have the double and single quotes inside the sql in php right?
  11. So now that i have this working, how would i go about updating multiple parts of the JSON in one go. So as an example if i have the primary, secondary and tertiary colours to update in one query?
  12. Yes, JSON is picky when it comes to single and double quotes preferring "" inside the JSON.
  13. UPDATE portal_content SET portal_content_json = JSON_SET(portal_content_json, '$."Portal Content"."Colours"."Primary"', 'Green') where `portal_attachment_id` = 25 This worked in the end - typo my side
  14. I found the problem, other if statements were using something similar to the function that I've shown above. I hadn't completed the code for the other if statements, my bad
  15. I amended the original JSON to be {'Portal Content':{'Colours':{'Primary':'String','Secondary':'String','Tertiary':'String','Quaternary':'String'}}} Removed the start and end ". Now when i run the query, it is just blanking the column.
  16. I am having some trouble with this. I have a column called portal_content_json with the content "{'Portal Content':{'Colours':{'Primary':'String','Secondary':'String','Tertiary':'String','Quaternary':'String'}}}" and i am trying to update the primary colour through phpMyAdmin with the following: UPDATE portal_content SET portal_content_json = JSON_SET(portal_content_json, '$.PortalContent.Colours.Primary', 'Green') where `portal_attachment_id` = 25 I am getting no errors but nothing is happening. Is there something obvious that is wrong here?
  17. <div class="row"> <label for="a" id="lbName">Name:</label> <input type="text" name="name" id="name"> </div> The if statement sending variables, lblTag and errorLbl to function "errorMessage", the error message I am getting is "TypeError: Cannot read properties of null (reading 'style'), which also means that the last line in the errorMessage function is also going to getting an error message, but won't show until the first error is cleared. The error message is for variable lblTag if (name ==""){ lblTag = "lbName" errorLbl = "Name" errorMessage(lblTag, errorLbl) } function errorMessage(lblTag, errorLbl){ console.log(errorLbl) // getting undefined in the console here and the other variable as well and getting error message // error message: TypeError: Cannot read properties of null (reading 'style') document.getElementById(`${lblTag}`).style.color = `red` document.getElementById('error-lbl-message').innerHTML = errorLbl + " is empty" }
  18. Yes - the code I told you to use instead. You'll benefit more if you read the replies.
  19. What does the values in 0,5 in substr($v, 0, 5) refers to?
  20. You must be using an outdated version of php. Str_starts_with() requires version 8. Use this instead $required = array_filter($files, fn($v)=>substr($v, 0, 5) == 'alg00');
  21. What you are trying to do is ill advised. Perhaps if you explained the problem you are trying to solve, we might be able to provide a better option. It would help you a good deal, if you understood what namespaces are for. Namespaces were added in php (as in many other languages) so that library developers could use the same names for classes (or functions) and not have collisions in the global name space. Use statements are ways of referencing classes that are defined within a namespace. Then there is autoloading, which is built into PHP. PHP comes with the option to configure a set of directories that will be searched, should the code reference a class that it does not have loaded. There are now standards for how autoloaders can work with namespacing to determine where classes are, and how they are laid out in the filesystem. As to what you appear to be trying to do, the standard way of wrapping a component library, would be to create your own class, which does all the things you are trying to do with the function. It could be as simple as the class definition, with a constructor method, and the method you want to call to wrap all the code you've shown. While it goes against the best practice pattern of Dependency injection, this new class could instantiate phpmailer in it's constructor, and store it in a private variable. Then you would just have change the code to use the private variable. <?php // Class MyMailer namespace MyOrg; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; // Include PHPMailer autoloader or include necessary PHP files require_once 'PHPMailer/src/PHPMailer.php'; require_once 'PHPMailer/src/SMTP.php'; require_once 'PHPMailer/src/Exception.php'; class MyMailer { private $mail; private $config = array(); public function __construct(PHPMailer $mail=null) { if (!$mail) { $this->mail = new PHPMailer(true); // Enable exceptions $this->mail->isSMTP(); $this->mail->Host = 'mail.qss.mgo.mybluehost.me'; // Your SMTP server host $this->mail->SMTPAuth = true; $this->mail->Username = 'xxxxx'; // Your SMTP username $this->mail->Password = 'xxxxx'; // Your SMTP password $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption $this->mail->Port = 587; // TCP port to connect to } } public function send($from, $fromName, $to, $toName, $subject, $body, $isHTML=true) { try { // Sender and recipient addresses $mail->setFrom($from, $fromName); // Sender's email address and name $mail->addAddress($to, $toName); // Recipient's email address and name // Email content $mail->isHTML($isHTML); // Set email format to HTML $mail->Subject = $subject; $mail->Body = $body; // Send the email if ($mail->send()) { echo 'Email sent successfully!'; } else { echo 'Error: Email not sent.'; } } catch (Exception $e) { echo 'Error: ' . $e->getMessage(); } } public function test() { $this-send('xxx.com', 'Your Name', 'xxx.com', 'Recipient Name', 'Test Email', 'This is a test email sent using PHPMailer'); } } At this point, you can require_once your class where you need it, and you are ready to test with something as simple as this: require_once('/path/to/MyMailer.php'); use MyOrg\MyMailer; $mail = new MyMailer(); $mail->test();
  22. I tried as suggested: $arrFiles = array(); $dirPath = "./"; $files = scandir($dirPath); $required = array_filter($files, fn($v)=>str_starts_with($v, 'alg00')); echo '<pre>' . print_r($required, 1) . '</pre>'; The error: Fatal error: Uncaught Error: Call to undefined function str_starts_with() ...php:29 Stack trace: #0 [internal function]: {closure}('.') #1 /...php(29): array_filter(Array, Object(Closure)) #2 {main} thrown in ...php on line 29
  23. Easier with str_starts_with() $files = [ 'alg001.php', 'alg002.php', 'alg003.php', 'alg004.php', 'alg005.php', 'algComp.php', 'ranFrac.php', 'ranalg.php', 'randec.php', 'randint.php' ]; $required = array_filter($files, fn($v)=>str_starts_with($v, 'alg00')); echo '<pre>' . print_r($required, 1) . '</pre>'; Gives Array ( [0] => alg001.php [1] => alg002.php [2] => alg003.php [3] => alg004.php [4] => alg005.php )
  24. $radint = mt_rand(1,33); $flip = mt_rand(1,2); $radint *= ($flip == 2) ? -1 : 1;
  25. Hi guysm I have a folder containing these files: alg001.php alg002.php alg003.php alg004.php alg005.php algComp.php ranFrac.php ranalg.php randec.php randint.php I would only like to store the names of files alg001.php, alg002.php, alg003.php, alg004.php, alg005.php into an array. How do i achieve this with preg match?
  26. how do i convert the following script to adopt ternary operator to shorten it? $radint = mt_rand(1,33); $flip = mt_rand(1,2); if($flip == 2){ $radint *= -1; } Thanx in advance.
  27. This is where an MVC-style approach to the server-side code comes in handy. The display logic is in the view, and the controller decides what to return. If the request to the controller is AJAX-based (or POST, in this case) the controller returns JSON-encoded data whereas if it's not, it returns the full compiled HTML.
  28. Last week
  29. provided the code for the page is laid out in this general order - initialization post method form processing get method business logic - get/produce data needed to display the page html document at the end of item #2, if the request was an AJAX request, you would build and output the response to the ajax request, then exit/die, so that the rest of the code on the page isn't executed. here's a snippet of code to detect an AJAX request - define('IS_AJAX_REQUEST', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); you can then conditionally run AJAX request only code using - if(IS_AJAX_REQUEST) { // build and output the response to the AJAX request // typeically json_encode([some meaningful error or success data]); // stop php code execution die; }
  1. Load more activity
×
×
  • 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.