Editor. dependent ('team. content ','../php/countries. php ') for cascading tables; Unable to execut

Editor. dependent ('team. content ','../php/countries. php ') for cascading tables; Unable to execut

lancwplancwp Posts: 90Questions: 20Answers: 1

Link to test case:
https://editor.datatables.net/examples/advanced/cascadingLists.html

Debugger code (debug.datatables.net):

<?php

// DataTables PHP library
include( "../lib/DataTables.php" );

$values = $_REQUEST['values']['team.continent'];
if (! $values) {
echo json_encode( [] );
return;
}

$countries = $db
->select( 'country', ['id as value', 'name as label'], ['continent' => $_REQUEST['values']['team.continent']] )
->fetchAll();

echo json_encode( [
'options' => [
'team.country' => $countries
]
] );

Error messages shown:

Parse error: syntax error, unexpected '[', expecting ')' in D:\web\Editorphp\controllers\countries.php on line 8

Description of problem:

https://editor.datatables.net/examples/advanced/cascadingLists.html
Editor. dependent ('team. content ','../../controllers/countries. php ') in; This countries.php cannot be executed in a lower version of PHP (PHP 5.3). How can I modify this code so that it can be executed in PHP 5.3?

Thank you so much

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    When you link to my example as a test case, are you saying the error can be seen on that page? It appears to work okay for me. Or do you mean you are using that same code on your PHP 5.3 server and it is causing issues?

    PHP 5.3 is ancient and stop receiving security updates in 2014!

    However, the update is fairly straightforward. 5.4 gained the ability to use Javascript like array syntax, so array() could be written as [].

    For example, the echo could be written as the following in PHP 5.3 code:

    echo json_encode(array(
      'options' => array(
        'team.country' => $countries
      )
    ));
    

    You need to also update the other array constructors, such as on the select line to make it PHP 5.3 compatible.

    But honestly - upgrade. It's worth it! ;)

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1
    edited April 2023

    Thank you. I have rewritten it without reporting any errors,But I have another question I want to continue asking

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    But I have another question I want to continue asking

  • lancwplancwp Posts: 90Questions: 20Answers: 1
    edited April 2023

    I have the type2 and type3 fields in the myorder table.

      id     ddbh       type2       type3  .........
      438   el1902    Outdoor   Fashio
    

    These two fields are linked and their association is based on the myservice table. The myservice table structure is as follows

    id    name   addime              removed_date   name2
    108  Outdoor    2022-03-28                           Mountaineering, Fashion, Leisure, Sports
    109  Skateboarding     2022-03-28                     eisure,  Pulleys 
    

    JS:(Omitted parts)

            ~~   {
                    label: "鞋种:",
                    name: "myorder.type2",
                    type:  "select"             
                },
                
                
                
                {
                    label: "分类:",
                    name: "myorder.type3",
                    type:  "select"             
                },
    
     editor.dependent( 'myorder.type2', '../../controllers/countries2.php' );
    
    ajax: "controllers/staff.php",~~
    

    countries2.php code

    // DataTables PHP library
    include( "../lib/DataTables.php" );
    $values = $_REQUEST['values']['myorder.type2'];
    if ( $values) {
        echo json_encode(array() );
        
        return;
    }
    
    $countries = $db
        ->select( 'myservice', array('id as value', 'name as label'),array('name' => $_REQUEST['values']['myorder.type2']))
        ->fetchAll();
    
    echo json_encode( array(
        'options' => array(
            'myorder.type3' => $countries
        )
    ) );
    

    staff.php code

    include( "../lib/DataTables.php" );
    
    
    // Web and thumbs paths to file
      $webPath = '/uploads/233.jpg';
       
    // System paths
        $sysPath = $_SERVER['DOCUMENT_ROOT'] . $webPath;
         
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    // Build our Editor instance and process the data coming from _POST
    
    
    Editor::inst( $db, 'ocorder' )
    
        ->fields(
        
            Field::inst( 'myorder.id' )->set(false)  //ID是自新增的,加上set(false才能新增后自动刷新)
            
         
            Field::inst( 'myorder.ddbh' )
            ->validator( Validate::notEmpty( ValidateOptions::inst()
                    ->message( '大底编号不能为空' ) 
                ) )
            ,               
            
                                
        Field::inst( 'myorder.type2' )
                 ->setFormatter( Format::ifEmpty( null ) ),
    
    
        Field::inst( 'myorder.type3' )
                ->options( Options::inst()
                    ->table( 'myservice' )
                    ->value( 'name' )
                    ->label( 'name2' )
                ),           
                 
    
    
            Field::inst( 'myorder.removed_date' )
                ->setFormatter( Format::ifEmpty( null ) )
            
        )
    
    ->on( 'preCreate', function ( $editor, &$values ) {       
                
        } )
        
         ->on( 'preEdit', function ( $editor, $id, &$values ) {
                    
    
        } )
    
        ->leftJoin( 'myservice',   'myorder.type2',   '=', 'myservice.name' )
    
        ->where( 'myorder.removed_date', null )     
    ->on( 'preRemove', function () {
            return false;
        } )
        
    
        
    
        ->debug(true)
        ->process( $_POST )
        ->json();
    

    When changing TYPE2

    Countries2.php output is null :

    {options: {myorder.type3: []}}
    options
    : 
    {myorder.type3: []}
    myorder.type3
    : 
    []
    

    How can I modify my code?

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • lancwplancwp Posts: 90Questions: 20Answers: 1
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    If you could link to a page showing the issue that would help be debug it and offer you some help.

    I need to know what is being submitted to the server when you change the type2 field.

    My guess is that the query condition:

    array('name' => $_REQUEST['values']['myorder.type2'])
    

    is what is causing the issue.

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    The latest key JS code is as follows(Part of the code)

                     {
                                    label: "鞋种:",
                                    name: "ocorder.type2",
                                    type:  "select"             
                     },
    
    
    
                    {
                                    label: "分类:",
                                    name: "ocorder.type3",
                                    type:  "select"             
                    },
    
        editor.dependent( 'ocorder.type2', '../../controllers/countries2.php' );
            **********
    
            { data: "ocorder.type2" },  
    
                        { data: "serviceshow4.name2" },
    

    countries2.php*****************

        <?php
        // DataTables PHP library
        include( "../lib/DataTables.php" );
        $values = $_REQUEST['values']['ocorder.type2'];
        if ( $values) {
            echo json_encode(array() );
    
            return;
        }
        $countries = $db
            ->select( 'serviceshow4', array('id as value', 'name as label'),array('name' => $_REQUEST['values']['ocorder.type2']))
          //  ->select( 'country', ['id as value', 'name as label'], ['continent' => $_REQUEST['values']['team.continent']] )
    
    
            ->fetchAll();
    
        echo json_encode( array(
            'options' => array(
                'ocorder.type3' => $countries
            )
        ) );
        ?>
    

    staff.php ************************************(Part of the code)

          <?php
    
                Field::inst( 'ocorder.type2' )
            ->options( Options::inst()
              ->table( 'serviceshow4' )
                ->value( 'name' )
             ->label( 'name' )
                   ),   
           ......                                                                            
               Field::inst( 'serviceshow4.name2' ),       
    
              ->leftJoin( 'serviceshow4',   'ocorder.type2',   '=', 'serviceshow4.name' )
              ->where ( 'serviceshow4.removed_date', null )     
               ->where( 'ocorder.removed_date', null )
    
    
        ...?>__
    

    The TYPE3 output in the table now represents the entire value of the NAME2 field in the SERVICESHOW4 table corresponding to TYPE2, such as Mountaineering, Fashion, Leisure, and Sport. However, what I want is one of the values, such as Sport. I know to use explore and array to separate it, but I don't know where to separate it

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    I'm finding this really hard to picture I'm afraid. Can you give me a link to the page please?

    I'm not clear if the issue is still that contries2.php is still returning an empty data array when you select a value, or something else?

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1
    edited April 2023

    是返回空的数组,链接地址是:

    http://wh-nxhac0j6fcs69he31w2.my3w.com/temp/gborder.php

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    That page is giving errors I'm afraid - it looks like jQuery isn't being loaded:

    Uncaught ReferenceError: $ is not defined
        at gborder.php:109:1
    

    Could you take a look at that, please.

    Colin

  • lancwplancwp Posts: 90Questions: 20Answers: 1
  • lancwplancwp Posts: 90Questions: 20Answers: 1
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Thanks for the updated link. You don't need to bump your thread just a few hours after posting :).

    Assuming that countries2.php is up to date, then, because the response from the server is simply [], then it must be falling into this part:

    $values = $_REQUEST['values']['ocorder.type2'];
    if ( $values) {
        echo json_encode(array() );
     
        return;
    }
    

    I can see in the request Editor is sending:

    values[ocorder.type2]:  "运动"
    

    So something somewhere is going wrong with the $_REQUEST is my guess. Can you add: print_r($_REQUEST); to your script, just before the echo json_encode(array()); perhaps?

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1
    edited April 2023

    ok

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    countries2.php

                  <?php
                    // DataTables PHP library
                    include( "../lib/DataTables.php" );
                    $values = $_REQUEST['values']['ocorder.type2'];
    
                    print_r($_REQUEST);
                    echo json_encode(array());
                    if ( $values) {
                        echo json_encode(array() );
    
                        return;
                    }
                    $countries = $db
                        ->select( 'serviceshow4', array('id as value', 'name as label'),array('name' => $_REQUEST['values']['ocorder.type2']))
                      //  ->select( 'country', ['id as value', 'name as label'], ['continent' => $_REQUEST['values']['team.continent']] )
    
    
                        ->fetchAll();
    
                    echo json_encode( array(
                        'options' => array(
                            'ocorder.type3' => $countries
                        )
                    ) );
                    ?>
    

    The above is the latest code, and the output print has been added

    print_r($_REQUEST);
    echo json_encode(array());

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    Array ( [rows] => Array ( [0] => Array ( [DT_RowId] => row_9684 [ocorder] => Array ( [id] => 9684 [factory] => 鏌忛槼 [ddbh] => 6002 [type1] => TPR [size] => 37 [sizediff] => [type2] => 鏃惰 [keyword] => [istax] => [wear] => [bjbz] => [bjr] => [bjdate] => [jgdate] => [mjdate] => [bjstau] => 姝e父 [hppic] => 19230209 [content1] => [removed_date] => ) [serviceshow4] => Array ( [name2] => 骞冲簳,钖勫簳,浣庤窡,涓窡,楂樿窡,鍧¤窡,璐磋窡,鎴峰,宸ヤ綔,椹竵闈�,鏃跺皻,浼戦棽,杩愬姩,浠€浣�,鍘氬簳 ) ) ) [row] => Array ( [DT_RowId] => row_9684 [ocorder] => Array ( [id] => 9684 [factory] => 鏌忛槼 [ddbh] => 6002 [type1] => TPR [size] => 37 [sizediff] => [type2] => 鏃惰 [keyword] => [istax] => [wear] => [bjbz] => [bjr] => [bjdate] => [jgdate] => [mjdate] => [bjstau] => 姝e父 [hppic] => 19230209 [content1] => [removed_date] => ) [serviceshow4] => Array ( [name2] => 骞冲簳,钖勫簳,浣庤窡,涓窡,楂樿窡,鍧¤窡,璐磋窡,鎴峰,宸ヤ綔,椹竵闈�,鏃跺皻,浼戦棽,杩愬姩,浠€浣�,鍘氬簳 ) ) [values] => Array ( [ocorder.factory] => 鏌忛槼 [ocorder.ddbh] => 6002 [ocorder.type1] => TPR [ocorder.size] => 37 [ocorder.sizediff] => [ocorder.type2] => 鏃惰 [ocorder.type3] => [ocorder.keyword] => [ocorder.bjbz] => [ocorder.istax] => [ocorder.bjstau] => 姝e父 [ocorder.content1] => [ocorder.hppic] => 19230209 [ocorder.removed_date] => ) ) [][]

    Now, the returned NAME2 is a set of values for "Flat Bottom, Thin Bottom, Low Heel, Mid Heel, High Heel, Slope Heel, Fit Heel, Outdoor, Work, Martin Boots, Fashion, Leisure, Sports, Shijia, Thick Sole", but what I want to return is one of the values, such as "Flat Bottom". How do I split this NAME2

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    edited April 2023

    That's impressively hard to read and parse :). Here it is rewritten:

    Array (
        [rows] => Array (
            [0] => Array (
                [DT_RowId] => row_9684
                [ocorder] => Array (
                    [id] => 9684
                    [factory] => 鏌忛槼
                    [ddbh] => 6002
                    [type1] => TPR
                    [size] => 37
                    [sizediff] =>
                    [type2] => 鏃惰
                    [keyword] =>
                    [istax] =>
                    [wear] =>
                    [bjbz] =>
                    [bjr] =>
                    [bjdate] =>
                    [jgdate] =>
                    [mjdate] =>
                    [bjstau] => 姝e父
                    [hppic] => 19230209
                    [content1] =>
                    [removed_date] => 
                )
                [serviceshow4] => Array (
                    [name2] => 骞冲簳,钖勫簳,浣庤窡,涓窡,楂樿窡,鍧¤窡,璐磋窡,鎴峰,宸ヤ綔,椹竵闈�,鏃跺皻,浼戦棽,杩愬姩,浠€浣�,鍘氬簳
                )
            )
        )
        [row] => Array (
            [DT_RowId] => row_9684
            [ocorder] => Array (
                [id] => 9684
                [factory] => 鏌忛槼
                [ddbh] => 6002
                [type1] => TPR
                [size] => 37
                [sizediff] =>
                [type2] => 鏃惰
                [keyword] =>
                [istax] =>
                [wear] =>
                [bjbz] =>
                [bjr] =>
                [bjdate] =>
                [jgdate] =>
                [mjdate] =>
                [bjstau] => 姝e父
                [hppic] => 19230209
                [content1] =>
                [removed_date] =>
            )
            [serviceshow4] => Array (
                [name2] => 骞冲簳,钖勫簳,浣庤窡,涓窡,楂樿窡,鍧¤窡,璐磋窡,鎴峰,宸ヤ綔,椹竵闈�,鏃跺皻,浼戦棽,杩愬姩,浠€浣�,鍘氬簳
            )
        )
        [values] => Array (
            [ocorder.factory] => 鏌忛槼
            [ocorder.ddbh] => 6002
            [ocorder.type1] => TPR
            [ocorder.size] => 37
            [ocorder.sizediff] =>
            [ocorder.type2] => 鏃惰
            [ocorder.type3] =>
            [ocorder.keyword] =>
            [ocorder.bjbz] =>
            [ocorder.istax] =>
            [ocorder.bjstau] => 姝e父
            [ocorder.content1] =>
            [ocorder.hppic] => 19230209
            [ocorder.removed_date] =>
        )
    ) 
    

    Based on that, $values should be 鏃惰. Which has just let me to the error:

    if ( $values) {
          echo json_encode(array() );
          return;
    }
    

    The if condition should be if (! $values) { !!

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    Thank you. It is already possible to link, but now the output name2 is a set of numbers. What I want is that name2 is also a selection box, and the options in the selection box are separated by ",". At which position should we use the split function to separate this

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    It looks like countries2.php is now returning data.

    What I want is that name2 is also a selection box, and the options in the selection box are separated by ","

    I don't understand I'm afraid. Where is name2 on your page? And do you mean a select element with multiple=true which combines the selected values to be comma separated?

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    Sorry, there was a problem with my expression. Now I have output. NAME2 is a dataset separated by, such as Mountainengineering, Fashion, Leisure, Sports. However, what I want to output is that NAME2 is a multi option SELECT with options such as Mountainengineering, Fashion, etc. I don't know where to separate this name2

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    I feel like it needs to be processed in the staff.php

    staff.php ************************************(Part of the code)

    <?php
    
            Field::inst( 'ocorder.type2' )
        ->options( Options::inst()
          ->table( 'serviceshow4' )
            ->value( 'name' )
         ->label( 'name' )
               ),  
       ......                                                                           
           Field::inst( 'serviceshow4.name2' ),      
    
          ->leftJoin( 'serviceshow4',   'ocorder.type2',   '=', 'serviceshow4.name' )
          ->where ( 'serviceshow4.removed_date', null )    
           ->where( 'ocorder.removed_date', null )
    
    
    ...?>
    

    The problem seems to be in this position
    Field::inst( 'serviceshow4.name2' ),

    Datasets with ',' signs are not separated here

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Right - so the value stored in ocorder.type2 is a string with a comma separated list of values? If that is the case use the separator and multiple option of select - e.g.:

    multiple: true,
    separator: ',',
    

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    yes, the value stored in is a string with a comma separated list of values ,It is for this field TYPE3 (serviceshow4.name2),The value of TYPE2 is correct and does not need to be separated

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Okay - thanks. Is this now resolved, or do you have a further question on it?

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    Allan
    Thank you for your continuous response. What I want to achieve is:

    When the first red arrow corresponds to a dropdown selection of a column, the second red arrow should also be able to drop down. The dropdown items of this column are the values separated by "," here,

    I don't know how to modify Field:: inst ('serviceshow4. name2 ') in order to achieve such a dropdown

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Should the options of the second select change based on the value selected in the first? If so, see this blog post.

    If you have two select elements dependent on each other, I would also strongly suggestion you show them both as editable at the same time. See this example for how to trigger inline editing on multiple cells at the same time.

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    Thank you for your continuous response
    Please check the latest URL
    http://www.oceanshoes.com/temp/gborder.php

    The latest countries2.php code is as follows

    <?php
    // DataTables PHP library
    include( "../lib/DataTables.php" );
    $values = $_REQUEST['values']['ocorder.type2'];
    
    //print_r($_REQUEST);
    //echo json_encode(array());
    
    if (! $values) {    
        echo json_encode(array() );
    
        return;
    }
    $countries = $db
        ->select( 'serviceshow4', array('name2 as value', 'name2 as label'),array('name' => $_REQUEST['values']['ocorder.type2']))
    
    
    
        ->fetchAll();
    
    echo json_encode( array(
        'options' => array(
            'ocorder.type3' => $countries
        )
    ) );
    ?>
    

    The secondary linkage is ready, they are working very well, and now there is only one problem left. The value of the secondary linkage is a string separated by "," ,such as "Baotou, Fashion, Fashion, Leisure, Sports", but I want these values to be separated by "," and "options. How can I modify them to achieve this?

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    For your column 分类 there is only ever one option shown in the select.

    Even if we were to split the value by comma, I don't understand where the options for that list should come from? How can there only be a single option for it, and that option is always selected. It seems like you need to resolve the list of options as the first priority. Unless I'm totally misunderstanding.

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    The separated list option is itself, which means separating its own fields into different list options through ","

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    It's already done, all the issues have been resolved. Thank you all

Sign In or Register to comment.