Jump to content

terungwa

Members
  • Posts

    90
  • Joined

  • Last visited

Profile Information

  • Gender
    Male
  • Interests
    PHP and RUBY programming

terungwa's Achievements

Regular Member

Regular Member (3/5)

0

Reputation

  1. I have created a phone number as Buddypress Profile field. I need to check that no two or more user accounts share the same phone number. My query below is not enforcing the restriction. I need help to resolve this. function bp_phone_number_validate() { global $bp; global $wpdb; // check if phone number is in use $result=$bp->get_var("SELECT COUNT(*) FROM {$bp->profile->wp_bp_xprofile_data} WHERE value = '{$_POST['field_2']}' GROUP BY id;"); if($result > 0){ $bp->signup->errors['field_2'] = __( 'Phone number is already in use.', 'buddypress' ); } } add_action( 'bp_signup_validate', 'bp_phone_number_validate');
  2. I have used this tutorial here (https://codex.wordpress.org/Integrating_WordPress_with_Your_Website) as a guide to set up non-wordpress pages to access some wordpress resources on non-wordpress pages. Eg I can view wordpress posts on non-wordpress page. I have wordpress installed in a sub-folder( ./root - PHP pages ./root/wordpres - Wordpress installation} In addition, I need to be able to detect if a user is logged into wordpress on the non-wordpress page. I am using this code below on non-wordpress pages for that: if(is_user_logged_in()){ $current_user = wp_get_current_user(); /** * @example Safe usage: $current_user = wp_get_current_user(); * if ( !($current_user instanceof WP_User) ) * return; */ echo 'Username: ' . $current_user->user_login . '<br />'; echo 'User email: ' . $current_user->user_email . '<br />'; echo 'User first name: ' . $current_user->user_firstname . '<br />'; echo 'User last name: ' . $current_user->user_lastname . '<br />'; echo 'User display name: ' . $current_user->display_name . '<br />'; echo 'User ID: ' . $current_user->ID . '<br />'; echo get_avatar( $current_user->user_email, 32 ). '<br />'; echo '<hr />'; } else{ echo 'Not logged-in'; } Even though I am logged into wordpress, when I navigate to the non-wordpress page, I see wordpress posts listed but, the is_user_logged_in() function returns false and prints 'Not logged-in'!!! Is there a way of achieving this besides the approach I have adopted? I need guidance to resolve this.
  3. I had downloaded this version before. But for the avoidance of doubt, I followed your link here to download then install. But I have the same error.
  4. I am trying to install WAMP on a 64bit Computer with windows version 8.1 installed. I have downloaded the WAMP version for a 64bit system. Following installation, This "httpd.exe-System Error pops up "The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing from your computer. Try reinstalling the program to fix this problem". Fixing the problem above requires installation of the Visual C++ Redistributable for Visual Studio 2015 update 3. I have also installed other recommended runtime libraries. I have downloaded this runtime library (Visual C++ Redistributable for Visual Studio 2015 update 3) from microsoft website but the runtime library installation keeps failing (Setup Failed error). I have pasted below details of the log file for review as well. [129C:2760][2016-10-03T09:07:12]i001: Burn v3.7.3813.0, Windows v6.3 (Build 9600: Service Pack 0), path: C:\ProgramData\Package Cache\{e46eca4f-393b-40df-9f49-076faf788d83}\VC_redist.x64.exe, cmdline: '/uninstall -burn.unelevated BurnPipe.{40571A66-B687-451B-A73F-AB3C54C08E62} {63634010-28A6-4D8E-84CD-A74B4B69E29C} 10004' [129C:2760][2016-10-03T09:07:12]i000: Setting string variable 'WixBundleLog' to value 'C:\Users\BEMAST~1\AppData\Local\Temp\dd_vcredist_amd64_20161003090712.log' [129C:2760][2016-10-03T09:07:13]i100: Detect begin, 10 packages [129C:2760][2016-10-03T09:07:13]i000: File search: windows_uCRT_DetectKey, did not find path: C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll [129C:2760][2016-10-03T09:07:13]i000: File search: windows_uCRT_DetectKeyExists, did not find path: C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll [129C:2760][2016-10-03T09:07:13]i000: Setting numeric variable 'windows_uCRT_DetectKeyExists' to value 0 [129C:2760][2016-10-03T09:07:13]i102: Detected related bundle: {3ee5e5bb-b7cc-4556-8861-a00a82977d6c}, type: Upgrade, scope: PerMachine, version: 14.0.23506.0, operation: None [129C:2760][2016-10-03T09:07:13]i102: Detected related bundle: {d992c12e-cab2-426f-bde3-fb8c53950b0d}, type: Upgrade, scope: PerMachine, version: 14.0.24215.1, operation: None [129C:2760][2016-10-03T09:07:13]i108: Detected compatible package: vcRuntimeMinimum_x64, provider: Microsoft.VS.VC_RuntimeMinimumVSU_amd64,v14, installed: {50A2BC33-C9CD-3BF1-A8FF-53C10A0B183C}, version: 14.0.24215, chained: {0D3E9E15-DE7A-300B-96F1-B4AF12B96488} [129C:2760][2016-10-03T09:07:13]i103: Detected related package: {50A2BC33-C9CD-3BF1-A8FF-53C10A0B183C}, scope: PerMachine, version: 14.0.24215.0, language: 0 operation: Downgrade [129C:2760][2016-10-03T09:07:13]i108: Detected compatible package: vcRuntimeAdditional_x64, provider: Microsoft.VS.VC_RuntimeAdditionalVSU_amd64,v14, installed: {EF1EC6A9-17DE-3DA9-B040-686A1E8A8B04}, version: 14.0.24215, chained: {BC958BD2-5DAC-3862-BB1A-C1BE0790438D} [129C:2760][2016-10-03T09:07:13]i103: Detected related package: {EF1EC6A9-17DE-3DA9-B040-686A1E8A8B04}, scope: PerMachine, version: 14.0.24215.0, language: 0 operation: Downgrade [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i052: Condition '(windows_uCRT_DetectKeyExists AND windows_uCRT_DetectKey >= v10.0.10137.0)' evaluates to false. [129C:2760][2016-10-03T09:07:13]i101: Detected package: vcRuntimeMinimum_x64, state: Obsolete, cached: Complete [129C:2760][2016-10-03T09:07:13]i101: Detected package: vcRuntimeAdditional_x64, state: Obsolete, cached: Complete [129C:2760][2016-10-03T09:07:13]i101: Detected package: Windows81_x86, state: Absent, cached: None [129C:2760][2016-10-03T09:07:13]i101: Detected package: Windows81_x64, state: Absent, cached: Complete [129C:2760][2016-10-03T09:07:13]i101: Detected package: Windows8_x86, state: Absent, cached: None [129C:2760][2016-10-03T09:07:13]i101: Detected package: Windows8_x64, state: Absent, cached: None [129C:2760][2016-10-03T09:07:13]i101: Detected package: Windows7_MSU_x86, state: Absent, cached: None [129C:2760][2016-10-03T09:07:13]i101: Detected package: Windows7_MSU_x64, state: Absent, cached: None [129C:2760][2016-10-03T09:07:13]i101: Detected package: WindowsVista_MSU_x86, state: Absent, cached: None [129C:2760][2016-10-03T09:07:13]i101: Detected package: WindowsVista_MSU_x64, state: Absent, cached: None [129C:2760][2016-10-03T09:07:13]i052: Condition 'VersionNT64 >= v6.0 OR (VersionNT64 = v5.2 AND ServicePackLevel >= 1)' evaluates to true. [129C:2760][2016-10-03T09:07:13]i199: Detect complete, result: 0x0 [129C:2760][2016-10-03T09:07:14]i200: Plan begin, 10 packages, action: Repair [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.3 AND NOT VersionNT64' evaluates to false. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: Windows81_x86 [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.3 AND VersionNT64' evaluates to true. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: Windows81_x64 [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.2 AND NOT VersionNT64' evaluates to false. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: Windows8_x86 [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.2 AND VersionNT64' evaluates to false. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: Windows8_x64 [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.1 AND NOT VersionNT64' evaluates to false. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: Windows7_MSU_x86 [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.1 AND VersionNT64' evaluates to false. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: Windows7_MSU_x64 [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.0 AND NOT VersionNT64' evaluates to false. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: WindowsVista_MSU_x86 [129C:2760][2016-10-03T09:07:14]i052: Condition 'VersionNT = v6.0 AND VersionNT64' evaluates to false. [129C:2760][2016-10-03T09:07:14]w321: Skipping dependency registration on package with no dependency providers: WindowsVista_MSU_x64 [129C:2760][2016-10-03T09:07:14]i201: Planned package: vcRuntimeMinimum_x64, state: Obsolete, default requested: None, ba requested: None, execute: None, rollback: None, cache: No, uncache: No, dependency: Register [129C:2760][2016-10-03T09:07:14]i201: Planned package: vcRuntimeAdditional_x64, state: Obsolete, default requested: None, ba requested: None, execute: None, rollback: None, cache: No, uncache: No, dependency: Register [129C:2760][2016-10-03T09:07:14]i201: Planned package: Windows81_x86, state: Absent, default requested: Absent, ba requested: Absent, execute: None, rollback: None, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i201: Planned package: Windows81_x64, state: Absent, default requested: Repair, ba requested: Repair, execute: Install, rollback: Uninstall, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i201: Planned package: Windows8_x86, state: Absent, default requested: Absent, ba requested: Absent, execute: None, rollback: None, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i201: Planned package: Windows8_x64, state: Absent, default requested: Absent, ba requested: Absent, execute: None, rollback: None, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i201: Planned package: Windows7_MSU_x86, state: Absent, default requested: Absent, ba requested: Absent, execute: None, rollback: None, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i201: Planned package: Windows7_MSU_x64, state: Absent, default requested: Absent, ba requested: Absent, execute: None, rollback: None, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i201: Planned package: WindowsVista_MSU_x86, state: Absent, default requested: Absent, ba requested: Absent, execute: None, rollback: None, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i201: Planned package: WindowsVista_MSU_x64, state: Absent, default requested: Absent, ba requested: Absent, execute: None, rollback: None, cache: No, uncache: No, dependency: None [129C:2760][2016-10-03T09:07:14]i207: Planned related bundle: {3ee5e5bb-b7cc-4556-8861-a00a82977d6c}, type: Upgrade, default requested: None, ba requested: None, execute: None, rollback: None, dependency: None [129C:2760][2016-10-03T09:07:14]i207: Planned related bundle: {d992c12e-cab2-426f-bde3-fb8c53950b0d}, type: Upgrade, default requested: None, ba requested: None, execute: None, rollback: None, dependency: None [129C:2760][2016-10-03T09:07:14]i299: Plan complete, result: 0x0 [129C:2760][2016-10-03T09:07:14]i300: Apply begin [2714:2474][2016-10-03T09:07:15]i360: Creating a system restore point. [2714:2474][2016-10-03T09:07:16]i361: Created a system restore point. [2714:2474][2016-10-03T09:07:16]i371: Updating session, registration key: SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{e46eca4f-393b-40df-9f49-076faf788d83}, resume: Active, restart initiated: No, disable resume: No [2714:0330][2016-10-03T09:07:16]i304: Verified existing payload: Windows81_x64 at path: C:\ProgramData\Package Cache\FC6260C33678BB17FB8B88536C476B4015B7C5E9\packages\Patch\x64\Windows8.1-KB2999226-x64.msu. [2714:2474][2016-10-03T09:07:16]i325: Registering dependency: {e46eca4f-393b-40df-9f49-076faf788d83} on package provider: Microsoft.VS.VC_RuntimeMinimumVSU_amd64,v14, package: vcRuntimeMinimum_x64 [2714:2474][2016-10-03T09:07:16]i325: Registering dependency: {e46eca4f-393b-40df-9f49-076faf788d83} on package provider: Microsoft.VS.VC_RuntimeAdditionalVSU_amd64,v14, package: vcRuntimeAdditional_x64 [2714:2474][2016-10-03T09:07:16]i301: Applying execute package: Windows81_x64, action: Install, path: C:\ProgramData\Package Cache\FC6260C33678BB17FB8B88536C476B4015B7C5E9\packages\Patch\x64\Windows8.1-KB2999226-x64.msu, arguments: '"C:\Windows\SysNative\wusa.exe" "C:\ProgramData\Package Cache\FC6260C33678BB17FB8B88536C476B4015B7C5E9\packages\Patch\x64\Windows8.1-KB2999226-x64.msu" /quiet /norestart' [2714:2474][2016-10-03T09:07:20]e000: Error 0x80240017: Failed to execute MSU package. [129C:2760][2016-10-03T09:07:20]e000: Error 0x80240017: Failed to configure per-machine MSU package. [129C:2760][2016-10-03T09:07:20]i319: Applied execute package: Windows81_x64, result: 0x80240017, restart: None [129C:2760][2016-10-03T09:07:20]e000: Error 0x80240017: Failed to execute MSU package. [2714:2474][2016-10-03T09:07:20]i372: Session end, registration key: SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{e46eca4f-393b-40df-9f49-076faf788d83}, resume: ARP, restart: None, disable resume: No [2714:2474][2016-10-03T09:07:20]i371: Updating session, registration key: SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{e46eca4f-393b-40df-9f49-076faf788d83}, resume: ARP, restart initiated: No, disable resume: No [129C:2760][2016-10-03T09:07:20]i399: Apply complete, result: 0x80240017, restart: None, ba requested restart: No What might I do to fix this problem and successfully install WAMP server? . I have attached a screenshot of the error popups for you review.
  5. Yea I shall definitely validate server side. The html5 pattern attribute is for user experience improvement on supported browsers
  6. I need to create a pattern attribute that specifies a regular expression for an <input> element's value that accepts mobile phone numbers. Jacques1 provided this PHP regex function which works great. I attempted to use this as a value for the pattern attribute but is is failing to check properly. Here is the regex code below: <!DOCTYPE html> <html> <body> <form action="demo_form.asp"> Country code: <input type="text" name="country_code" pattern="\\A(?:\\+?234|0)?(?:704|702|803|806|703|706|813|816|810|814|903|802|708|808|812|701|902|809|817|818|909|908|805|705|815|807|811|905)\\d{7}\\z" title="Allowed formats are: 09031111111, 2340801111111, or +2340801111111"> <input type="submit"> </form> </body> </html> What is the proper format to use as a pattern attribute value. Note a number as this should be valid (2349098766543) but it is failing. Thanks
  7. Thanks Jacques1, but Your pattern /\\A(?:\\+?234|0)?(?:704|803)\\d{7}\\z/ does not match this subject string (2347048134704); when tested here https://www.regex101.com/ Or am I missing something?
  8. I have a regex expression that validates a set of mobile numbers: The numbers have optional prefixes (+234 or 234 or 0) and I have used the alternation with the pipe character. Immediately following the international code or zero character is a set of service provider specific identifiers that I have also specified the options using the alternation character. Based on the regex I have, this number should not be valid (12349098766543); but the regex returns it as valid. It appears the there is a match on 909 in the code. But shouldn't the presence of 1 at the beginning of the test string enforce a validation failure? What is wrong with the regex i have here? function check_number($number) { if(!preg_match('/[+234|234|0]?(704|702|803|806|703|706|813|816|810|814|903|802|708|808|812|701|902|809|817|818|909|908|805|705|815|807|811|905)\d{7}$/', $number)) { return 'Invalid Phone Number'; }else{ return 'valid Phone Number';} The execution below should fail, but is is passed as valid. $str = '12349098766543'; echo check_number($str);
  9. I had successfully set up and integrated a third party payment integration system on WHMCS. Inorder to Validate callback authenticity, the gateway provides a method of verifying that a callback originated from them as shown in code below: $mertid ='secretmerchantID'; $amt = '10000'; $tranxid = 'gtPay130958397220820'; $hashkey ='secretclientkey'; $hash = hash('sha512', $mertid . $tranxid . $hashkey); $url = 'https://ibank.gtbank.com/GTPayService/gettransactionstatus.xml?mertid='.$mertid.'&amount='.$amt.'&tranxid='.$tranxid.'&hash='.$hash; $xmlString = file_get_contents($url); if($xmlString === false) { echo "Response Description: GTPAY Verification service failed to open. } else { $xml = simplexml_load_string($xmlString ); var_dump($xml); } I am using the file_get_contents function to read the API response into a string, then simplexml_load_string function to Interprets the string of XML into an object with properties containing the data held within the xml string. On each successful transaction run $xml=simplexml_load_string($jString), returns FALSE. However when I call this code in a file directly in a browser on WAMP using the same transaction values, then $xml=simplexml_load_string($jString), returns the string of XML into an object as expected. Also when I place the file on a different remote host and call it in browser, it also returns the string of XML into an object. Strangely, when I place this code in a file within the WHMCS root folder and I attempt to call directly in a browser using the same successful transaction values used above, then $xml=simplexml_load_string($jString), returns FALSE again. What may be restricting this call to the Third party API call within the WHMCS install? Thanks.
  10. I wish to redirect to a custom page following the failure in payment as returned by payment gateway. To this end, I am using the ShoppingCartCheckoutCompletePage hook to check this using the variable $vars['ispaid']. When payment is successful, $vars['ispaid'] is true and the redirect to the thankyou page works. add_hook('ShoppingCartCheckoutCompletePage', 1, function ($vars) { # Will be true if the order has been paid if($vars['ispaid'] == true) { #redirect to thank you page header('location:'.thankYouPage); die; } else { header('location:'.transactionFailedPage); die; } } However the else statement is not executing when payment obviously fails, rather the viewinvoice.php page is loaded and payment status set to 'unpaid' Am I missing something? How can I redirect to the transaction failed page?
  11. Hello, I could not figure wether this question should be posted in the javascript forum or here. I am building a simple blog commenting application that displays a comment reply box whose textarea has a dynamically generated ID when a reply link is clicked Take a look at my html code below: <div> <div> Some message over here </div> <div style = "height:10px;"> <a class="reply" id="myHeader1" href="javascript:showonlyone('newboxes1');" >Reply</a></div> </div> <div class ="replymsgbox" id="newboxes1"> <form method="POST"> <textarea id="" class="content"></textarea> </form> </div> <div> <div> Some message over here </div> <div style = "height:10px;"> <a class="reply" id="myHeader2" href="javascript:showonlyone('newboxes2');" >Reply</a></div> </div> <div class="replymsgbox" id="newboxes2"> <form method="POST"> <textarea id="" class="content" ></textarea> </form> </div> And this is the javascript dynamically generating the ID: The javascript script below uses the parent(); children() and next() jquery methods to traverse the DOM and select the textarea element of the displayed form so as to assign the 'ID attribute' to it. <script type="text/javascript"> $('a.reply').on("click", function(e){ $(this).parent("div").parent("div").next("div").children('form').children('textarea').attr( "id", "ckeditor" ); }); </script> When the reply link is clicked, the comment box with a form is toggled using the jquery show() and hide() functions. See code snippet below: <script src="views/js/jquery-1.11.2.min.js" type="text/javascript"></script> <script type="text/javascript"> function showonlyone(thechosenone) { $('.replymsgbox').each(function(index) { if ($(this).attr("id") == thechosenone) { $(this).show(200); } else { $(this).hide(600); } }); } </script> I then wish to replace the dynamic (textarea id="comment") with a CKEditor instance using the code below: <script src="libraries/ckeditor/ckeditor.js"></script> <script type="text/javascript"> $(document).ready(function() { if(CKEDITOR) { // Replace the <textarea id= "ckeditor"> with a CKEditor instance. CKEDITOR.replace('ckeditor'); } }); </script> However, when I toggle the comment form, (the id="ckeditor") attribute is assigned, but what appears is a simple textarea not with ckeditor enabled on it. How can I make each new dynamically created textareas use CKeditor? or Why is the CKEDITOR replace() method not detecting the textarea ID? Perhaps someone here may have any clues? Note: that when I hardcode the id='ckeditor' attribute value pair into any of the text areas, that textarea is converted into a rich text editor.
  12. Thank you mac_gyver, your observation is valid, on each page reload an instance of the class is created and a call to the connected() method creates a new database connection and as you observed, this doesn't know anything about anything from any previous call. What eventually worked was to check for an existing database connection each time the connected() method is called(using if/else statement). public function connected() { if ($this->db_connection) { return $this->db_connection; } else { try { return $this->db_connection = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8mb4', $this->username, $this->password); } catch (PDOException $e) { echo "Unable to connect to the PDO database: " . $e->getMessage(); } } }
  13. Thank you Muddy_Funster, I have effected the synthax corrections ( $this->db_connection = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8mb4', $this->username, $this->password); ) but the errors still persit.
  14. Here's my example of using transactions in PDO: I am getting the two errors below when I run my scripts in the browser. PDOException: There is no active transaction in...... Fatal error: Uncaught exception 'PDOException' with message 'There is no active transaction' in ...... here is ny pdo database connection class: <?php class conn { public $host = ''; public $dbname = ''; public $username = ''; public $password = ''; /** * @var object $db_connection The database connection */ private $db_connection = null; public function __construct($host, $dbname, $username, $password) { $this->host = $host; $this->dbname = $dbname; $this->username = $username; $this->password = $password; } public function connected() { try { $this->db_connection = @new PDO('mysql:host='.$this->hos.';dbname='.$this->dbname.';charset=utf8mb4', $this->username, $this->password); return $this->db_connection; } catch (PDOException $e) { echo "Unable to connect to the PDO database: " . $e->getMessage(); } } } And below is the database queries: <?php require('config/conn.php'); $host = 'localhost'; $dbname = 'dbname'; $username = 'username'; $password = 'password'; $db = new conn($host, $dbname, $username, $password); try { //note that calling beginTransaction() turns off auto commit automatically $db->connected()->beginTransaction(); $stmt = $db->connected()->prepare("INSERT INTO category_types (name, cat_id) VALUES (:name, :value)"); $stmt->bindParam(':name', $name); $stmt->bindParam(':value', $value); // insert one row $name = 'one'; $value = 1; $stmt->execute(); // insert another row with different values $name = 'two'; $value = 2; $stmt->execute(); $stmt = $db->connected()->prepare("INSERT INTO category_types2 (name, cat_id) VALUES (:name, :value)"); $stmt->bindParam(':name', $name); $stmt->bindParam(':value', $value); // insert one row $name = 'one'; $value = 1; if($stmt->execute()) //all went well commit! $db->connected()->commit(); } catch (Exception $e) { //Something went wrong rollback! $db->connected()->rollBack(); echo "Failed: " . $e->getMessage(); } What might I be doing wrong? Thanks
  15. I need to set up a threaded comments system in a PHP project and I got this script shown below from http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/#comment-436261 class Threaded_comments { public $parents = array(); public $children = array(); /** * @param array $comments */ function __construct($comments) { foreach ($comments as $comment) { if ($comment['parent_id'] === NULL) { $this->parents[$comment['id']][] = $comment; } else { $this->children[$comment['parent_id']][] = $comment; } } } /** * @param array $comment * @param int $depth */ private function format_comment($comment, $depth) { for ($depth; $depth > 0; $depth--) { echo "\t"; } echo $comment['text']; echo "\n"; } /** * @param array $comment * @param int $depth */ private function print_parent($comment, $depth = 0) { foreach ($comment as $c) { $this->format_comment($c, $depth); if (isset($this->children[$c['id']])) { $this->print_parent($this->children[$c['id']], $depth + 1); } } } public function print_comments() { foreach ($this->parents as $c) { $this->print_parent($c); } } } Here’s the example usage with the data provided as an array: $comments = array( array('id'=>1, 'parent_id'=>NULL, 'text'=>'Parent'), array('id'=>2, 'parent_id'=>1, 'text'=>'Child'), array('id'=>3, 'parent_id'=>2, 'text'=>'Child Third level'), array('id'=>4, 'parent_id'=>NULL, 'text'=>'Second Parent'), array('id'=>5, 'parent_id'=>4, 'text'=>'Second Child') ); $threaded_comments = new Threaded_comments($comments); $threaded_comments->print_comments(); I have a sample select query that pulls data from a database and stores the result in the $comments array as shown below. The $comments array is then passed as an argument to the $threaded_comments object: $sql = 'SELECT * FROM test_comments'; // submit the query and capture the result $result = $conn->query($sql); $comments = array(); while ($row = $result->fetch_assoc()) { $comments[] = $row; } The challenge is that nothing is printed to the screen when I run the script. Inspection of the comments array with the var_dump function is shown below: array (size=4) 0 => array (size=3) 'id' => string '1' (length=1) 'parent_id' => string '0' (length=1) 'text' => string 'comment' (length=7) 1 => array (size=3) 'id' => string '2' (length=1) 'parent_id' => string '0' (length=1) 'text' => string 'comment' (length=7) 2 => array (size=3) 'id' => string '3' (length=1) 'parent_id' => string '1' (length=1) 'text' => string 'comment ' (length= 3 => array (size=3) 'id' => string '4' (length=1) 'parent_id' => string '3' (length=1) 'text' => string 'comment ' (length= I was wondering if the array format from my select query is the issue? Could anyone provide a clue as to how to fix this? Thanks.
×
×
  • 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.