We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27330
    • 884 Posts
    I’m trying to import a calendar script into modx and I have such form which mix the php values into the form and I’m not understanding exactly how to go on about it.

    I’m reading this http://rtfm.modx.com/display/revolution20/Snippets#Snippets-Don%27ttrytomixPHPandHTMLinaSnippet. and not sure if I’m suppose to write a different snippet for each php call on the form?

    thanks in advance for any help

     <form action="[[++calendar_path]]php/datafeed.php?method=adddetails
    
    <?php echo isset($event)?"&id=".$event->Id:""; ?>
               
              " class="fform" id="fmEdit" method="post">                 
              <label>                    
                <span>                        *Subject:              
                </span>                    
                <div id="calendarcolor">
                </div>
                <input MaxLength="200" class="required safe" id="Subject" name="Subject" style="width:85%;" type="text" value="
    
    <?php echo isset($event)?$event->Subject:"" ?
    
    >" />                     
                <input id="colorvalue" name="colorvalue" type="hidden" value="
    <?php echo isset($event)?$event->Color:"" ?>" />                
              </label>                 
              <label>                    
                <span>*Time:
                </span>                    
                <div>  
                  <?php if(isset($event)){
                      $sarr = explode(" ", php2JsTime(mySql2PhpTime($event->StartTime)));
                      $earr = explode(" ", php2JsTime(mySql2PhpTime($event->EndTime)));
                  }?>                    
                  <input MaxLength="10" class="required date" id="stpartdate" name="stpartdate" style="padding-left:2px;width:90px;" type="text" value="<?php echo isset($event)?$sarr[0]:""; ?>" />                       
                  <input MaxLength="5" class="required time" id="stparttime" name="stparttime" style="width:40px;" type="text" value="<?php echo isset($event)?$sarr[1]:""; ?>" />To                       
                  <input MaxLength="10" class="required date" id="etpartdate" name="etpartdate" style="padding-left:2px;width:90px;" type="text" value="<?php echo isset($event)?$earr[0]:""; ?>" />                       
                  <input MaxLength="50" class="required time" id="etparttime" name="etparttime" style="width:40px;" type="text" value="<?php echo isset($event)?$earr[1]:""; ?>" />                                            
                  <label class="checkp"> 
                    <input id="IsAllDayEvent" name="IsAllDayEvent" type="checkbox" value="1" <?php if(isset($event)&&$event->IsAllDayEvent!=0) {echo "checked";} ?>/>          All Day Event                      
                  </label>                    
                </div>                
              </label>                 
              <label>                    
                <span>                        Location:
                </span>                    
                <input MaxLength="200" id="Location" name="Location" style="width:95%;" type="text" value="<?php echo isset($event)?$event->Location:""; ?>" />                 
              </label>                 
              <label>                    
                <span>                        Remark:
                </span>                    
    <textarea cols="20" id="Description" name="Description" rows="2" style="width:95%; height:70px">
    <?php echo isset($event)?$event->Description:""; ?>
    </textarea>                
              </label>                
              <input id="timezone" name="timezone" type="hidden" value="" />           
            </form>   
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      You could write a separate snippet for each PHP call on that, but that’s not a good idea, generally. It’s usually much cleaner if you were to put all that HTML into a Chunk and sprinkle in [[+placeholders]] where-ever you need to insert a dynamic value. Then in your PHP, calculate what all those values are, then substitute them into the Chunk. So your first line of your new improved Chunk might look like this:

      <form action="[[++calendar_path]]php/datafeed.php?method=adddetails&id=[[+event_id]]" class="fform" id="fmEdit" method="post"> 


      Any time you’re doing any of that if/then/else concatenation stuff inside of your HTML, that’s where you’d put a placeholder. Make sense? It’s MUCH cleaner that way.

      In architectural terms, the Snippet is acting as a controller (as in MVC = Model, View, Controller), and the Chunk is acting as a view. MODx follows this architectural principle *really* well, and it makes the code soooo much easier to debug and maintain. The goal here is to keep all your logic and calculations separate from the HTML that displays the results of those calculations.

      Sorry if this is being overly pedantic wink
        • 27330
        • 884 Posts
        thanks for the explanation, so If I strip the php from the html this is what I’m left with:
        return $event?"&id=".$event->Id:""; 
        return $event?$event->Subject:"" ;
        return $event?$event->Color:"" ;
        if($event){
                          $sarr = explode(" ", php2JsTime(mySql2PhpTime($event->StartTime)));
                          $earr = explode(" ", php2JsTime(mySql2PhpTime($event->EndTime)));
                      }                    
                     
        return $event?$sarr[0]:""; 
        return $event?$sarr[1]:""; 
        return $event?$earr[0]:""; 
        return $event?$earr[1]:""; 
        if($event&&$event->IsAllDayEvent!=0) {return "checked";} 
        return $event?$event->Location:""; 
        return $event?$event->Description:""; 
        


        was hoping for easy life so tried this which didn’t work of course smiley
        <?php
        $output = $modx->getChunk('calEditorTPL',array(
           'event_id' => '$event?"&id=".$event->Id:""',
           'subject' => '$event?$event->Subject:""',
           'start_date' => '$event?$sarr[0]:""',
           'start_time' => '$event?$sarr[1]:""',
           'end_date' => '$event?$earr[0]:""',
           'end_time' => '$event?$earr[1]:""',
           'location' => '$event?$event->Location:""',
           'description' => '$event?$event->Description:""',
        ));
        return $output;


        the result now is that the page loads the chunk but populates the placeholders as they are inside the quotes.

        Any chance I’m getting further example or am I asking too much?
        it *feels* so close smiley
          • 3749
          • 24,544 Posts
          You can take the single quotes off the ends of the array members on the right side, but you’ll have to set the $event variable somewhere.

          You may be able to replace it with $_POST[’event’] or $_GET[’event’] depending on where it’s coming from.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 4172
            • 5,888 Posts
            try this:

            <?php
            
            
            $event_ph=array();
            if (isset($event)){
            	$event_ph['event_id']=$event->Id;
            	$event_ph['subject']=$event->Subject;
            	$event_ph['start_date']=$sarr[0];
            	$event_ph['start_time']=$sarr[1];
            	$event_ph['end_date']=$earr[0];
            	$event_ph['end_time']=$earr[1];
            	$event_ph['location']=$event->Location;
            	$event_ph['description']=$event->Description;
            }
            
            $output = $modx->getChunk('calEditorTPL',$event_ph);
            return $output;		 
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 27330
              • 884 Posts
              thx guys for the replies smiley

              I tried as Bob suggested and removed the single quotes, also added the function with the event variable (the data comes from mysql db) and sweet hurray it’s working sweet. with my lame code I got a warning
              Warning: Unexpected character in input: ''' (ASCII=39) state=1
              but with Bruno’s code the error disappears but I get
              1 1

              on begning of page, I guess from my copy/paste of the database connection function.
              I’ll look at the correct way to connect to the db.

              This is a try of using this jquery calendar and this is getting really sweet.
              http://www.web-delicious.com/jquery-plugins-demo/wdCalendar/docs/index.htm

              thanks alot for the tips and code. u guys rock big time.