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

    I have been using modx for a couple of weeks now having mostly worked on Joomla and Wordpress in the past and I am amazed at how powerful it is. I am currently having a bit of an issue which is probably me doing something stupid so I will explain the whole end goal before going into my small issue in case you can think of an easier way of doing it.

    I am working on a site that is for a large amount of stories each story has template variables that say what characters and which volumes they belong to, Then I am storing the Id's of the stories a user has read in their extended fields.

    I can then get these extended field ID's and use it to display what the user has read to them and also how much of a character they have read, which is the bit I am currently working on.

    So.. On the Character 1 Page I want it to display all the stories that have this character in and then say how much of this character a user has read. (please excuse any sloppy coding as I will tidy this all up with more chunks later!)

    [[!getResources? &parents=`15` &includeTVs=`1` &processTVs=`1` &tpl=`Characters` &tvFilters=`Characters==%Character 1%`]]
    
    <br />
    total number of stories with this character in: [[+total]]
    
    [[!Profile]]
    <br />
    <p>Custom Field: [[+stories_read]]</p>
    <br />
    Amount of this character read: [[!user_char_read? &input1=[[+stories_read]] &input2=[[!getResources? &parents=`15` &includeTVs=`1` &processTVs=`1` &tpl=`characters_idonly` &tvFilters=`Characters==%Character 1%`]]]]
    
    ]

    The first bit that shows how many stories contain character 1 is working fine but the second bit is not working properly, I am passing the two inputs off to a snippet which is below (currently displaying results with a print that will be removed as well, also only showing inputs and outputs for debugging)


    Snippet: stories_read
    $id = $modx->resource->get('id');
    $profile = $modx->user->getOne('Profile');
    $extended = $profile->get('extended');
    if (substr_count($extended['stories_read'],$id) > 0)
    {
    print $extended['stories_read'];
    }
    else
    {
    
    print $extended['stories_read'] . ", " . $id;
    }



    Snippet: user_char_read
    <?php
    $output1 = explode(", ", $input1);
    $output3 = substr($input2, 0, -1);
    $output2 = explode(", ", $output3);
    
    $result = array_intersect($output1, $output2);
    
    echo '<br /> in1 '; print_r ($input1);
    echo '<br /> out1 '; print_r ($output1);
    echo '<br /> in2 '; print_r ($input2);
    echo '<br /> out2 '; print_r ($output2);
    echo '<br /> out3 '; print_r ($output3);
    echo '<br /> result '; print_r ($result);
    
    And for further info here is the template chunk
    
    [b]chunk: characters_idonly[/b]
    [[+id]], 
    So the issue is that I have two arrays that look completely identical when I look at the output of each one but all this is producing is an intersected array with one value in it where it should have 3, I have attached the result as an image If anyone could help me it would be greatly appreciated or if I am going about this completely the wrong way then a nudge in the right direction. Many Thanks, David
      • 4172
      • 5,888 Posts
      Now, maybe, it gets a bit advanced for a MODX - beginner.

      If I could follow your scenario so far, you would need a db-design like that:

      characters - MODX-Resources (or maybe better, records in a custom-table)
      stories - MODX-Sub-Resources of characters (or maybe better, records in a custom-table)
      users - MODX-users

      user_stories_read - I wouldn't use a user-extended-field to store them, but a custom-table with story_id and user_id

      now, you could list all stories and show, which of them the user has read with a simple

      migxLoopCollection - snippet - call with a join to the user_stories-read - table, something like that:

      [[migxLoopCollection?
      &packageName=`yourCustomPackageName`
      &classname=`modResource`
      &joins=`[{"alias":"StoriesRead","classname":"UserStoriesRead","on":"story_id=modResource.id"}]`
      &where=`{"StoriesRead.user_id":"[[+modx.user.id]]","parent":"[[*id]]"}`
      &tpl=`tplStoriesRead`
      ]]
      


      You will need to create a xpdo - package, first, which isnt't that difficult with help of MIGX.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 50903
        • 6 Posts
        HI Bruno17,

        Thanks for the reply it sounds like that might be a little over my head but I will try and get cracking on it today, I will let you know how I get on

        Thanks,
        Dave
          • 4172
          • 5,888 Posts
          If you have any questions, let us know.
          Its really not that hard, as it might sound, at first.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 50903
            • 6 Posts
            Hi Bruno,

            I just wanted to say thank you very much I am much closer to where I need to be now using MigxDB and creating new tables for the user_stories_read. I don't really want to post up my code for others as it's still pretty sloppy but I just thought I would add this link to a video tutorial which helped me hugely getting my head around MigxDB

            https://www.gelstudios.co.uk/blog/2013/10/modx-migx-db-tutorial.html

            Thanks Again,
            David