solved how do I choose a random quota_id from a set?

Ken Rich
Ken Rich
@ken-rich
11 years ago
926 posts
There is a lovely content featuring code that works well and is truly impressive (at least to me). First of all the code looks like this:

 <div class="col12 last">
      {if jrCore_is_mobile_device()}<div class="menu_tab">
             <div id="default" class="p_choice fartist" onclick="jrLoad('#sm','{$jamroom_url}/index_artists');jrSetActive('#default');">{jrCore_lang  skin=$_conf.jrCore_active_skin id="21" default="featured"} {jrCore_lang  skin=$_conf.jrCore_active_skin id="12" default="artists"}</div>
            <div id="s_song" class="p_choice fsong" onclick="jrLoad('#sm','{$jamroom_url}/index_songs');jrSetActive('#s_song');">Songs</div>           
            <div id="s_video" class="p_choice fvideo" onclick="jrLoad('#sm','{$jamroom_url}/index_videos');jrSetActive('#s_video');">Videos</div>                        <div class="clear"></div>        </div>

{else}

        <div class="menu_tab">
            <div id="default" class="p_choice fartist" onclick="jrLoad('#sm','{$jamroom_url}/index_artists');jrSetActive('#default');">{jrCore_lang  skin=$_conf.jrCore_active_skin id="21" default="featured"} {jrCore_lang  skin=$_conf.jrCore_active_skin id="12" default="artists"}</div>
            <div id="s_song" class="p_choice fsong" onclick="jrLoad('#sm','{$jamroom_url}/index_songs');jrSetActive('#s_song');">{jrCore_lang  skin=$_conf.jrCore_active_skin id="21" default="featured"} {jrCore_lang  skin=$_conf.jrCore_active_skin id="13" default="songs"}</div>
            <div id="s_soundcloud" class="p_choice fsoundcloud" onclick="jrLoad('#sm','{$jamroom_url}/index_soundclouds');jrSetActive('#s_soundcloud');">{jrCore_lang  skin=$_conf.jrCore_active_skin id="21" default="featured"} {jrCore_lang  skin=$_conf.jrCore_active_skin id="61" default="Soundcloud"}</div>
            <div id="s_video" class="p_choice fvideo" onclick="jrLoad('#sm','{$jamroom_url}/index_videos');jrSetActive('#s_video');">{jrCore_lang  skin=$_conf.jrCore_active_skin id="21" default="featured"} {jrCore_lang  skin=$_conf.jrCore_active_skin id="14" default="videos"}</div>
            <div id="s_youtube" class="p_choice fyoutube" onclick="jrLoad('#sm','{$jamroom_url}/index_youtubes');jrSetActive('#s_youtube');">{jrCore_lang  skin=$_conf.jrCore_active_skin id="21" default="featured"} {jrCore_lang  skin=$_conf.jrCore_active_skin id="62" default="youtube"}</div>
            <div id="s_vimeo" class="p_choice fvimeo" onclick="jrLoad('#sm','{$jamroom_url}/index_vimeos');jrSetActive('#s_vimeo');">{jrCore_lang  skin=$_conf.jrCore_active_skin id="21" default="featured"} {jrCore_lang  skin=$_conf.jrCore_active_skin id="63" default="vimeo"}</div>
            <div class="clear"></div>
        </div>{/if}
        <div class="inner mb8">
            <div id="sm"></div>
        </div>
    </div>
</div>

At the top of the index page, this code is used to call up the initialization protocol in Java for the above code...

<script type="text/javascript">
    $(document).ready(function(){
        jrSkinInit();
    });
</script>


In the java folder, the actual initialization protocol being called up looks like this:


function jrSkinInit() {
       jrLoad('#sm',core_system_url +'/index_artists');
 jrLoad('#top_members',core_system_url +'/top_members');
    jrLoad('#top_songs',core_system_url +'/top_songs');
    jrLoad('#top_groups',core_system_url +'/top_groups');
    jrLoad('#top_discussions',core_system_url +'/top_discussions');
    jrLoad('#top_notes',core_system_url +'/top_notes');
    jrLoad('#top_actions',core_system_url +'/top_actions');
    jrLoad('#top_blogs',core_system_url +'/top_blogs');
    jrLoad('#top_galleries',core_system_url +'/top_galleries');
    jrSetActive('#default');
}



At what point will it grab new content to display? It doesn't appear to be every page load like a simple rotor, as nothing changes over several page loads (at least in a short time frame).

If I reset the cache it immediately grabs new content. So what is actually controlling the initialization? How can it be altered to grab new random content from those assigned quotas every page load (like a rotor), or is that a bad idea in this system?




--

Ken Rich
indiegospel.net

updated by @ken-rich: 01/08/15 12:52:32PM
paul
@paul
11 years ago
4,335 posts
I think you've answered your own question ;-)

Caching - That's why its not changing. If you wait the cache time (300 seconds by default) it should change when refreshed.

To make things random you need to alter the order_by parameter the those top_members.tpl, top_songs.tpl etc. files -

{jrCore_list module="jrProfile" order_by="profile_name random" . . .}

If you really want these templates to refresh every time, try the following (though its not recommended on a busy site)

{jrCore_list module="jrProfile" order_by="profile_name random" no_cache=true . . .}


--
Paul Asher - JR Developer and System Import Specialist
SteveX
SteveX
@ultrajam
11 years ago
2,584 posts
Paul is right, cacheing is why the content isn’t changing between clicks and page loads.

It might help to understand what the impressive code does, let me give that a shot… Let me know which parts you get, and which you don’t understand - it isn’t complex, but there are a lot of things going on at once, so it is difficult to explain them all at once.

The important parts in the first piece of code you posted are
1. the menu links - when you click them you trigger an onclick event rather than going to a url. In the onclick handler is a call to a jrLoad javascript function (see below).
2. the div at the bottom with an id="sm" - this is where the content will load.

The javascript initialization is separate to the links, it gets things going when the page is downloaded and ready - in does the same job as clicking a link, but it does it for all of them without needing a click to happen.

jrLoad('#top_notes',core_system_url +'/top_notes')

What that does is call the jrLoad function with two parameters (they are in brackets and separated by a comma).

The first param is an id - the target div into which the content will be loaded. ("#" denotes an id, "." would denote a class). You can find something like this in your html:
<div id="top_notes" ... 

The second is a url: http://yoursite.com/top_notes

The jrLoad function calls that url and the (php) response from that url sends back some html content to load into the div ("#top_notes").

That example is triggered in the init function (along with all the others) - it happens when the page loads, and each of the jrLoad functions loads each response into the specified div.

It works in a similar fashion when you click a link in the menu (one with an onclick handler which calls a url via jrLoad, mentioned above). When you click the menu item, the onclick is triggered, it calls a url via the javascript jrLoad function, and places the returned html into the specified div - in the case of the menu links, that is #sm - I presume that is the main content of the page.

Please let me know how much of that you get - I'm trying to work out how to explain something similar to a bunch of librarians, so your feedback will be helpful to them, and to me.


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
SteveX
SteveX
@ultrajam
11 years ago
2,584 posts
Blimey, I do pity those librarians.

Hopefully I'll be able to explain better in time.


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)

updated by @ultrajam: 12/04/14 02:31:26PM
brian
@brian
11 years ago
10,148 posts
Just to add that caching is a good thing, and in Jamroom users that are logged in will get their own "version" of that page, and all logged out users will see the same version. Once your site is running the sections should change frequently enough that they look "fresh". What you want to avoid is disabling caching or setting it to a very short time period (say 10 seconds). This will results in a much higher load on your system than is really needed.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
Ken Rich
Ken Rich
@ken-rich
11 years ago
926 posts
SteveX:
Please let me know how much of that you get - I'm trying to work out how to explain something similar to a bunch of librarians, so your feedback will be helpful to them, and to me.

OK - most of what you said I understood intuitively on some level, by watching it work and surmising, but not knowing the techno-speak for it, or the actual "mechanics" behind it.

The part that's really frying my brain is this:

Quote: The first param is an id - the target div into which the content will be loaded. ("#" denotes an id, "." would denote a class). You can find something like this in your html...

The reason it's frying my brain is I had thought ID and class were related to the CSS not Javascript (I tend to play with CSS experimentally). I thought they were pointing back to a set of CSS styling values that would control what that division looked like visually, and were not related to how it operated functionally.

So now I don't know if they are dual function (CSS and Javascript), since it seems I was completely mistaken in my previous (CSS only) assumption.

Are they more like names that serve as reference points to WHATEVER is coming into play (CSS, Javascript, PHP, smarty).

I need some type of simple definition and perhaps a colorful metaphor (smile) to describe the function of ID and class. Otherwise, I'm missing part of the jigsaw puzzle.


--

Ken Rich
indiegospel.net

updated by @ken-rich: 12/04/14 07:50:16PM
Ken Rich
Ken Rich
@ken-rich
11 years ago
926 posts
paul:
Caching - That's why its not changing. If you wait the cache time (300 seconds by default) it should change when refreshed.

If you really want these templates to refresh every time, try the following (though its not recommended on a busy site)

{jrCore_list module="jrProfile" order_by="profile_name random" no_cache=true . . .}

That this is cached on a 5 minute timer makes perfect sense to me, I figured there had to be some sort of timer on it.

Quote: {jrCore_list module="jrProfile" order_by="profile_name random" . . .}

I actually worked that part worked out for myself this morning, and tied my two paid artist levels to it. What I would like to figure out now, is how to "weight" one quota more than another.

I am currently using
Quote: quota_id="7,6"

However, I want random picks from quota 7 to occur twice as often as from quota 6. So how could I accomplish this? Would it be as simple as placing the preferred quota twice?
Quote: quota_id="7,7,6"



--

Ken Rich
indiegospel.net

updated by @ken-rich: 12/04/14 08:32:29PM
michael
@michael
11 years ago
7,772 posts
The forums are kind of also used for documentation, since the question is "how do I choose a random number from a set?" would it be ok if I renamed this thread that?

Try this:
{$options = [ 'turkey', 'foo','belgum', 'france','wellington','tuesday','northern contennt']}
{$key = array_rand($options)}
{$wanted = $options[$key]}

{$wanted}

$wanted will output one of the items in that list. so you could then use $wanted wherever you needed it.

Change those items to the 7,7,6 then use that as the quota_id
{$options = ['7', '7','6']}
{$key = array_rand($options)}
{$wanted = $options[$key]}

{jrCore_list module="jrAudio" quota_id=$wanted}
SteveX
SteveX
@ultrajam
11 years ago
2,584 posts
Ken_Rich:...I had thought ID and class were related to the CSS not Javascript...

Are they more like names that serve as reference points to WHATEVER is coming into play (CSS, Javascript, PHP, smarty).

That's pretty much it, but php and smarty don't use them, they put them into the html for later use.

Both css and javascript use them to identify elements within the page. IDs and classes are a fast way of doing that, although you can use other attributes, or the structure of the html as well.

Identifying an element using selectors like id and class is key to both css and javascript, understand how to move around the document and everything else falls into place.


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
paul
@paul
11 years ago
4,335 posts
Quote: What I would like to figure out now, is how to "weight" one quota more than another.
The could be hard coded in the template something like this -
{jrCore_random_number min=6 max=8 assign="weighted_quota_id"}
{if $weighted_quota_id == 8}
    {assign var="weighted_quota_id" value=7}
{/if}
{jrCore_list module="jrProfile" quota_id=$weighted_quota_id  . . .}



--
Paul Asher - JR Developer and System Import Specialist
Ken Rich
Ken Rich
@ken-rich
11 years ago
926 posts
Thanks Michael - rename this to anything you like.

I just went cross-eyed looking at your (and Paul's) solution (thanks also). I'll obviously have to study this stuff for awhile, before I understand it.

Why couldn't it be simple like quota_id="7,7,6" lol


--

Ken Rich
indiegospel.net

updated by @ken-rich: 12/05/14 04:33:58AM
michael
@michael
11 years ago
7,772 posts
Ken_Rich:....Why couldn't it be simple like quota_id="7,7,6" lol....

Because that would make the question more complex. currently the question is "Which quota should I use?"

And changing it to that way would change the question to "Which quota or quotas should I use where multiple entries mean weighted options?"

Its a more specific question that not everyone needs. It would mean moving that logic into the main code to check for it every time rather than just this one instance your wanting to use it.

Slow it down everywhere else so you don't need to copy+paste that code into the location its actually used isn't such a great idea.

Its a little bit more complicated to look at, but not much. Either way shown above (Pauls or mine) works. You can choose :)
Ken Rich
Ken Rich
@ken-rich
11 years ago
926 posts
Hi Michael,

After looking at these a few times, your version actually makes sense to me a little. Like an algebra equation. The nomenclature is foreign to me, but I think I can understand the math.

Paul's version is a little more familiar looking with the if/then format I'm used to seeing in the templates. However, someone would have to step me through the math, because I am not "getting it" in that format.

In any case, am I correct in assuming these versions can be added right to the templates? So in the case of your code, would it look like this in practice for "Featured Artists? (index_artists.tpl).

{$options = ['7', '7','6']}
{$key = array_rand($options)}
{$wanted = $options[$key]}
{jrCore_list module="jrProfile" quota_id=$wanted}

{jrCore_list module="jrProfile" order_by="_profile_id random" limit="4" quota_id="$wanted" search1="profile_active = 1" template="index_artists_row.tpl"require_image="profile_image"}

Then for featured songs, I assume only the module specific information would change, so it would look like? (index_songs.tpl).

{$options = ['7', '7','6']}
{$key = array_rand($options)}
{$wanted = $options[$key]}
{jrCore_list module="jrAudio" quota_id=$wanted}

{jrCore_list module="jrAudio" order_by="audio_title random" quota_id="$wanted" limit="4" template="index_songs_row.tpl" require_image="audio_image"}

Do I have this straight now, or am I still lost?


--

Ken Rich
indiegospel.net

updated by @ken-rich: 12/06/14 06:18:29AM
michael
@michael
11 years ago
7,772 posts
I'll read them to you, first mine:
Quote:
Store these items ( '7' and '7' and '6' ) in a box called $options.
Randomly select the position in the $options box. use that position to determine the item. Store the value of the item in $wanted. (now $wanted is either '7' or '7' or '6' ).
Give $wanted to {jrCore_list.....}

The second one, pauls one, reads:
Quote:
Choose a random number not lower than 6 and not higher than 8. If the random number turned out to be 8 then change it to 7. Store it in $weighted_quota_id. (now $weighted_quota_id is either '7' or '7' or '6' ).
Give $weighted_quota_id to {jrCore_list.....}

So both are ways to pick a random number. {jrCore_list} doesn't care which way you chose to select that number. It only cares that it gets a number.

--edit--
Paul chose to name his variable $weighted_quota_id, I chose to use $wanted. That name makes no difference and you can use whatever you like as long as no other location around there is using it.
updated by @michael: 12/06/14 11:24:50PM
Ken Rich
Ken Rich
@ken-rich
11 years ago
926 posts
OK - I'm getting how they both function now, but just need confirmation on where to install.

Is it in the index_artists.tpl for artists, index_songs.tpl for songs (as I detailed in my last post above)?


--

Ken Rich
indiegospel.net