We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30889
    • 29 Posts
    Hello,

    I have just started working with MODx and I am considering it for a current project. I am trying to bring over an existing site with a custom shopping cart into MODx. There is a function that returns the contents of the cart to the top of the page/template. It is not working and so far all testing is suggesting to me it is a session issue. Below are the separate snippets. Placed here sequentially for convenience.

    <?php
    //snippet for code shop class instance
    
    //Session variable
    session_start();
    
    //online db info ---------------
    include("common.php");  // db constants
    include_once("functions.php");  //generic functions
    include_once("instance.shop.php");  //-connects to database creates array (used in next snippet) and disconnects
    ?>
    
    
    <?php
    //snippet for display in template
    echo '<strong>View Your Order</strong>';
    
    echo "<strong>(Value : ";
    echo "\$".number_format($ordercontents['finaltotalprice'], 2); 
    echo "</strong>";
    
    echo "<strong>";
    echo '/ Items : '.$orderitemcount .' )';
    echo "</strong>";
    ?>
    


    I attempted to make use of mysql_select_db() to change databases, do the query and then change back to modx database. That also did not work.

    Eventually I am hoping to integrate the shop completely into MODx as modules and snippets.

    For now I just want to test the possibilities.

    Is what I am attempting possible?
    Could someone make some suggestions?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You don’t want to have session_start() in a snippet. The document containing the snippet already takes care of that. All you need to do is add what you want to the $_SESSION array. I would suggest putting all of your stuff in its own array ($_SESSION[’myshop’]) so you can easily find it and clear the whole thing if you want to.

        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 30889
        • 29 Posts
        Thank you for the timely reply.

        I removed session_start(). I was only using it to generate a unique ID to track the cart for the relevent user. I still get no values returned from the query.

        I have also replaced all database connectivity with the modxapi/ DBAPI and it still doesn’t work.

        I know the cart is filled with items because I can check the database as well as the normal site outside of MODx.

        I have run out of ideas to try.
          • 30889
          • 29 Posts
          I think I have posted this under the wrong title and assumption.

          I assumed because of an experience while I was testing the fifty or so CMS in that last month and half. One of them(Expression Engine) did not work with the same code I am trying here until I removed the session_start(), then it worked fine.

          Sessions work fine. I think it is the way the code is processed.

          There is no error. I just get no values returned from shop class instance. The result suggests the cart is empty when it actually contains something.

          Thank you for the testing suggestion none the less.
            • 7231
            • 4,205 Posts
            Do a direct echo of the session var you are looking for and see if it is getting a value. I have used sessions in MODx with no problem so they do work grin
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 30889
              • 29 Posts
              One of the reasons the situation makes me thinks it is a session issue the following is print_r of $_COOKIE

              [QE_linksShown] => 1
                  [QE_position] => 0/0
                  [SN4786bb48e0f79] => e980bca706fdac0437c1013a38e7f788
                  [cartID] => e980bca706fdac0437c1013a38e7f788
                  [dbx-postmeta] => grabit:0 |1-|2-|3-|4-|5-&advancedstuff:0-|1-|2-
                  [PHPSESSID] => 52ef22e3c14fc1d76e892ffd12ceec4f
                  [exp_perpage] => 50
                  [etomite_etomite_] => %5D%EC%03%F6%E4
                  [exp_save_info] => no
                  [exp_last_activity] => 1200573392
                  [exp_last_visit] => 1200526063
                  [bar] => shown
                  [SESS63542523f88c858e362651fd3aea05e9] => qvpgfv8bhv1jfjgugi1mmb0eh2
                  [exp_notify_me] => yes
                  [style] => Trend
                  [exp_mode] => normal
                  [toggle_articles_detail] => 1
                  [webfxtab_resourcesPane] => 3
              


              cartID is what I need to check the DB. It has the same data as [SN4786bb48e0f79] when it should have [PHPSESSID].

              When I change the cookie code from using session_ID() to the following it still returns the above values for cartID.

              function getCartID(){
              			
              	if(isset($_COOKIE["cartID"])){
              		//unset($_COOKIE["cartID"]);
              		return $_COOKIE["cartID"];
              	}
              	else{
              		// There is no cookie set. We will set the cookie
              		// and return the value of the users session ID for use with the cart
              				
              		setcookie("cartID", $_COOKIE["PHPSESSID"], time() + 14400); //set how long cookie should last
              		return $_COOKIE["PHPSESSID"]; 
              	}
              }
              


              I am curious as to how MODx works.
                • 30889
                • 29 Posts
                I’ve worked out what the problem was. Maybe this will be helpful to others.

                I incorrectly understood the way snippets work.

                I thought that if snippetA includes() another php file that contains $varA
                then snippetB would be able to access $varA if snippetB comes after snippetA. Apparently wrong.

                If snippetA includes another php file, the variable in that file must be set with $_SESSION([varA]) = $varA and then accessed as a session variable in snippetB.


                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  I believe you could make the variable part of the $GLOBALS array as well. Since snippets are parsed via eval() calls, any variables declared within them (or files they include) will be local to that snippet. This will change in 0.9.7, but I haven’t gotten that far in my study of the new core to be able to address this question.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 30889
                    • 29 Posts
                    Thank you for that clarification.

                    My understanding of MODx thus far is through trial, error, forums and wiki. And I am making errors.

                    0.9.7 sound like it will be a break away Etomite.

                    Trialing the same code using Etomite is how I started to come up with the solution. I knew nothing about eval(), but now I know more.

                    The interesting thing is that cartID started referencing PHPSESSID instead of [SN4786bb48e0f79] as in the example of the code above. I don’t know what [SN4786bb48e0f79]is though but the values changed because of eval().

                    Next on my list of things to do is to write a simple module into the control panel and to work out how to get friendly urls to work since even after following the instructions they don’t work. They never worked in Etomite either. But I think this forum topic is resolved.
                      • 29606
                      • 63 Posts
                      Quote from: sottwell at Jan 23, 2008, 03:05 AM

                      I believe you could make the variable part of the $GLOBALS array as well. Since snippets are parsed via eval() calls, any variables declared within them (or files they include) will be local to that snippet. This will change in 0.9.7, but I haven’t gotten that far in my study of the new core to be able to address this question.

                      I know it’s an old topic, but... In Revolution, now, is there any way to have a global variable ?
                      I want to include an external application which needs a $somevar defined as global throughout the site to work fine. Of course, I don’t want to change this application code, for it would be a pain to install updates afterwards.
                      Does anybody have an idea on how to do this ?