What is it exactly you're trying to achieve and what does your full amended code look like? And is it giving you a new error message or is it the same as from the original question?
Im not getting an error, but now im not comparing '1121' to '3423 1231 1121' but rather longer strings to longer strings, so im getting a result of none basically.
Ah ok, I misunderstood your previous follow up question.
In that case where you have multiple id's stores that you need to compare I would say that you need to loop over all of those selected nodes and then in the loop do the .contains part - Does this make sense? :)
Hey Jan, ive taken some time and still not figured this out unfortunately, would you be able to provide a hint, even if its just quick pseudo code, on how to execute your suggestion. :-)
Sorry for my late reply - I have been meaning to get back to you all week but there has been too much on my plate to sit down and write you a proper reply, which can help you. The thing is that I need some time to setup a mini test for myself to play around with too before I guide you further.
I hope to have some time available for this by tomorrow unless someone else beats me to it of course - Hope that's ok with you :)
Hey Jan, apologies if your busy its no trouble :-)
I got somewhere with someone else's help elsewhere, building on your answers, but it's not quite meeting the needs I have, if I put the new code in below, you'll see it's using an array and a loop to kinda achieve the goal.
But I think something is wrong because, and I don't know if your able to glean what I'm building but its an Advert system, so I'm looking at the Advert location given (adPlace) then comparing it to the Current Page ID, and all its parents, (pagesID) because I want an advert placed on "Events" to hit Events and all the children events for example. But I think I need to exclude the tippy top ID because I'm getting an advert everywhere.
Ok I'm afraid that I have lost track of what it is you're trying to do - But I can't see the .Contains method in your above code now?
But looking at it makes me thing you perhaps you should consider placing some of the logic in a controller since there is quite a lot going on in the view now :) - But easy for me to say since I usually don't write controllers myself (My wonderful backend colleagues handle that part) :-)
Ehm, but my thinking was that you should be able to loop over each of the possible add places and inside that loop do the comparison against the PagesId.
But now I can't even remember if you MNTP is saving a comma separated string of id's or if you're getting the UDI's instead? But let's say you have the page id's available like "1023, 1233, 3002, 6789" then i would loop over all of the adPlace id's and run pagesID.Contains(adPlace.ToString()) to see if there is a match. But please note that this might be possible to do in a smarter fashion using LINQ perhaps.
Does this make any sense and am I even on the right track in regards to what you're trying to do?
Hey Jan, apologies for the delay, I have it set up like this:
So you'd set up an Advert, then if you see in that example the advert location is set to news, id need it on News, and all of News Children, that's why I'm doing the array of current page ID + all parents, to check if the current page or any of its parents, has an advert associated with it, and thus display it.
So I have refined everything and now I'm at this stage, albeit still not working:
I have 2 variables:
pagesID & adPlace and these might exist like
pagesID: 1351 1348adPlace: 1348 1067
Then the below code runs, essentially as said above, im looking to see if any of the numbers in pagesID are in adPlace and this works if the number in pagesID is only one number, or it is first, however see my example above, adplace has 1348 and 1067, so technically there is a match as 1348 exists within pagesID but because it is not first, the code does not find a 'match'.
var targetsToMatch = pagesID.Split(' ');
foreach (var pagesUrl in targetsToMatch)
{
if (adPlace.Contains(pagesUrl) && hasValue == false)
{
Do X
}
hasValue = true;
}
Where String Contains String
Hi guys, got this:
@pagesID could be something like this
And @adPlace would be like this
Can't seem to get the above to work.
I get this;
Thinking about it, strings is probably not the best way to do it, im only comparing Page ID's afterall!
Hi Kieron
From the top of ny head I would say that you will need to write it as
Does this work?
/Jan
I think this works, its showing the page atleast!
I don't think im getting matches though, could it be the way I build my long string up?
string pagesID = "";
I added spaces so that the ID's are seperate.
Sorry Jan, would you change this at all if rather than comparing 1232 1212 3273 to 9383, but if it was multiple to multiple, like
Contains
Ive changed to be a multinode picker now, and when I pick one item it works, like the original question, but if i pick multiples it doesnt.
Hi Kieron
What is it exactly you're trying to achieve and what does your full amended code look like? And is it giving you a new error message or is it the same as from the original question?
/Jan
Im not getting an error, but now im not comparing '1121' to '3423 1231 1121' but rather longer strings to longer strings, so im getting a result of none basically.
Here is my code
But the important bit is this:
Which works if i give it one value, like 3221, but if i pick multiple values like "3221 7372" then it wont match anything.
You can see here, https://gyazo.com/5ac3d0923a5fe48af1a9029d26e12f69 the 2 items have a matching ID in both, but nothing as a result of the match, which makes me think its not seeing a match.
Also, you may wish to notify whomever, my script tags in my code are actually executed on submit, even on this forum lol.
Hi Kieron
Ah ok, I misunderstood your previous follow up question.
In that case where you have multiple id's stores that you need to compare I would say that you need to loop over all of those selected nodes and then in the loop do the .contains part - Does this make sense? :)
/Jan
It does make sense, but me typing it out successfully is another issue :P
Thank you, I will try and have a go but not really sure where to begin.
Hey Jan, ive taken some time and still not figured this out unfortunately, would you be able to provide a hint, even if its just quick pseudo code, on how to execute your suggestion. :-)
Thanks again
Hi Kieron
Sorry for my late reply - I have been meaning to get back to you all week but there has been too much on my plate to sit down and write you a proper reply, which can help you. The thing is that I need some time to setup a mini test for myself to play around with too before I guide you further.
I hope to have some time available for this by tomorrow unless someone else beats me to it of course - Hope that's ok with you :)
Cheers, Jan
Hey Jan, apologies if your busy its no trouble :-)
I got somewhere with someone else's help elsewhere, building on your answers, but it's not quite meeting the needs I have, if I put the new code in below, you'll see it's using an array and a loop to kinda achieve the goal.
But I think something is wrong because, and I don't know if your able to glean what I'm building but its an Advert system, so I'm looking at the Advert location given (adPlace) then comparing it to the Current Page ID, and all its parents, (pagesID) because I want an advert placed on "Events" to hit Events and all the children events for example. But I think I need to exclude the tippy top ID because I'm getting an advert everywhere.
Hi Kieron
Ok I'm afraid that I have lost track of what it is you're trying to do - But I can't see the .Contains method in your above code now?
But looking at it makes me thing you perhaps you should consider placing some of the logic in a controller since there is quite a lot going on in the view now :) - But easy for me to say since I usually don't write controllers myself (My wonderful backend colleagues handle that part) :-)
Ehm, but my thinking was that you should be able to loop over each of the possible add places and inside that loop do the comparison against the PagesId.
But now I can't even remember if you MNTP is saving a comma separated string of id's or if you're getting the UDI's instead? But let's say you have the page id's available like "1023, 1233, 3002, 6789" then i would loop over all of the adPlace id's and run pagesID.Contains(adPlace.ToString()) to see if there is a match. But please note that this might be possible to do in a smarter fashion using LINQ perhaps.
Does this make any sense and am I even on the right track in regards to what you're trying to do?
/Jan
Yeah thats basically it! though ive not yet got too using Controllers, or heard of LINQ, lol.
But yeah, user picks 'adPlace' as like a top level node, and then i want to display an advert on all the pages that match adPlace AND its children.
ps the Contain is now roughly half way through that code.
Hi Kieron
What does your backoffice setup for this look like?
Then I'll see if I can setup an instance like you have done in order to play around with some code for you to try out.
/Jan
Hey Jan, apologies for the delay, I have it set up like this:
So you'd set up an Advert, then if you see in that example the advert location is set to news, id need it on News, and all of News Children, that's why I'm doing the array of current page ID + all parents, to check if the current page or any of its parents, has an advert associated with it, and thus display it.
Thanks :)
Scratch that it works, I hadn't set the ad to show where I was refreshing the page, lol.
Thanks!
So I have refined everything and now I'm at this stage, albeit still not working:
I have 2 variables:
pagesID
&adPlace
and these might exist likepagesID: 1351 1348
adPlace: 1348 1067
Then the below code runs, essentially as said above, im looking to see if any of the numbers in
pagesID
are inadPlace
and this works if the number inpagesID
is only one number, or it is first, however see my example above,adplace
has1348
and1067
, so technically there is a match as1348
exists withinpagesID
but because it is not first, the code does not find a 'match'.Thank you
Hi Kieron ,
looking at this bit of code specifically (not all the code above that) - Umbraco has a number of extension functions to help you with this.
first there is
.ToDelimitedList()
which will split the string in to its items (remove blanks) and give you lists .so pageIds.ToDelimitedList(" ") will give you the list of ids.
Then
.ContainsAny(list)
will tell you if one list contains any values from another list -So if you get two lists you can see if one contains any items from the other.
So in its simplest form this can be written as:
This is looking good. I will admit I have got it working a different way, but it is a complete mess because I basically gave up on the idea.
I'm going to be able to replace alot of code with that little bit lol.
Thanks very much!
is working on a reply...