How should I specify an item in a checkbox list, when using the dom? - wordpress

I am currently automating use of the wordpress editor using VBA in access 2003, but would like to extend the automation to include the selection of category, taxonomy items etc.
That is with Checkbox lists.
An example with the same structure is here: http://devblog.xing.com/frontend/the-checkbox-list/
My list is based on a hierarchy of geographic entities: On the local database I may have data associated with the locality: Algeria for instance.
I want to be able to use (ie As SHDocVw.InternetExplorer) ie.document.what?
I am a bit lost as to an elegant approach. I haven't tried it yet but I guess I can get the innerhtml for each selectit class, check to see if it contains my keyword an if so extract the input id with a bit of string manipulation, and then use ie.document.getelementbyid("whatever").Click Check or Toggle
But is there a better approach?
(Ultimately I will have to look at how to connect to the remote database and drag the tag_id from the tables there - but I thought this would be quicker especially in that the automation functionality in a larger sense already exists)
Any pointers appreciated!
<ul id="localitieschecklist" class="categorychecklist form-no-clear"
data-wp-lists="list:localities">
<li id="localities-8" class="popular-category">
<label class="selectit">
<input id="in-localities-8" type="checkbox" name="tax_input[localities][]" value="8"> </input>
Africa
</label>
<ul class="children"><li id="localities-96">
<label class="selectit"><input id="in-localities-96" type="checkbox" name="tax_input[localities][]" value="96"></input>
Algeria
</label>

Thanks for your help #Tim Williams. I am not sure that worked on the elegance front but it has enabled me to move forward, I run into a bit of headache with the hierarchical nature of the list, and time as dictated a compromise of only supporting the first two levels of the hierarchy. It will do for now, but any further comments are certainly welcome!
Localities
If artClassLocalities <> "" And Not IsNull(artClassLocalities) Then
artClasses = Split(artClassLocalities, ",")
Set Element = .Document.getElementByID("localitieschecklist")
For i = 0 To Element.childNodes.Length - 1 'the element collection represents the globe
'popular category items (6) one for each landmass
Set Landmass = Element.childNodes(i)
'landmass has 2 nodes
'child 1 is the selectitnode node 0 (item 1)
If InStr(1, artClassLocalities, Right(Landmass.childNodes(0).innerText, Len(Landmass.childNodes(0).innerText) - 1)) Then
Call Landmass.childNodes(0).childNodes(0).setAttribute("checked", True)
Else
Call Landmass.childNodes(0).childNodes(0).setAttribute("checked", False)
End If
For j = 0 To Landmass.childNodes(1).childNodes.Length - 1 'the children are the countries
Set Country = Landmass.childNodes(1).childNodes(j) ' a given child is a country
If InStr(1, artClassLocalities, Right(Country.childNodes(0).innerText, Len(Country.childNodes(0).innerText) - 1)) Then
Call Country.childNodes(0).childNodes(0).setAttribute("checked", True)
Else
Call Country.childNodes(0).childNodes(0).setAttribute("checked", False)
End If
'Support for Subregions not yet functional
'For k = 0 To Country.childNodes(1).childNodes.Length - 1
'Set PAndADiv = Country.childNodes(j)
Next
Next
End If

Related

ASP, Forms and passing variables between frames

I am pretty new to ASP, I know VBScript reasonably well though. What I am trying to do is create a website with 2 frames. In the top frame, it asks for a year (from a selection box) and a week number (from a selection box). It should then display the dates relating to the selection and a button to process the request. When the button is clicked the bottom form then processes a SQL query based on the selection in the top frame and displays the info.
Now, my problem is when it comes to understanding ASP. With ASP, all the code is processed then the output is sent to the browser. How do you update variables or even pass them to other frames when the code has already processed?
I just need some pointers on the way forward to accomplishing the above.
Thanks
First off, don't use frames: they're annoying, ugly, and outmoded.
You can do something like this in asp, but it's going to require a round trip (or two) to the server.
The basic outline of the page (let's call it thispage.asp) would be something like
<html><head>[head stuff]
<%
dim yr, wk, i
yr = request.form("Year")
wk = request.form("Week")
'- if you use form method='get', then use request.querystring("Year")
if not isnumeric(yr) then
yr = Year(date) 'or whatever else you want to use as a default
else
yr = CInt(yr)
end if
'similar validation for wk
%>
</head>
<body>
<form method="post" action="thispage.asp">
<select name="Year" size="1">
<%
for i = Year(Date) - 2 to Year(Date) + 2
response.write "<option value='" & i & "'"
if i = yr then response.write " selected"
response.write ">" & i & "</option>"
next
%>
</select> [similar code for week or date or whatever]
<input type="submit">
</form>
<%
If yr <> "" and wk <> "" Then
'- look up stuff in database and output the desired data
'- (this part will be much longer than this)
Else
Response.Write "<p>Please make your selections above.</p>"
End If
%>
</body></html>
If you need to output form fields that are dependent on the user's initial year & week selections, then you're going to need more than one trip to the server, but it's still the same idea: set up the variables you're going to need, see if they have values, write out the form, and then if all the necessary variables have all the necessary values, then you can do your output stuff.

Parsing images via nokogiri and xpath

I currently have a piece of code which will grab a product title, description, and price and for that it works great. However, I also need it to get the image URL which is where my dilemma is. I tried using a xpath inside the loop I have at the bottom and it lists out ALL the images that are equal to 220 on EVERY product which I dont want at all. So basically I get something like this....
product 1 Title here
product 1 Description here
product 1 price here
http://www.test.com/product1.jpg
http://www.test.com/product2.jpg
http://www.test.com/product3.jpg
http://www.test.com/product4.jpg
product 2 Title here
product 2 Description here
product 2 price here
http://www.test.com/product1.jpg
http://www.test.com/product2.jpg
http://www.test.com/product3.jpg
http://www.test.com/product4.jpg
Where as I obviously want product 1 to just have http://www.test.com/product1.jpg and product 2 to have http://www.test.com/product2.jpg etc, etc. The images are just in a div tag with no class or ID hence why I didnt just easily put them into a css selector. Im really new to ruby/nokogiri so any help would be great.
require 'nokogiri'
require 'open-uri'
url = "http://thewebsitehere"
data = Nokogiri::HTML(open(url))
products = data.css('.item')
products.each do |product|
puts product.at_css('.vproduct_list_title').text.strip
puts product.at_css('.vproduct_list_descr').text.strip
puts product.at_css('.price-value').text.strip
puts product.xpath('//img[#width = 220]/#src').map {|a| a.value }
end
Try changing:
puts product.xpath('//img[#width = 220]/#src').map {|a| a.value }
to:
puts product.xpath('.//img[#width = 220]/#src').map {|a| a.value }
The point of the '.' there is to say you want all images that are children of the current node (e.g. so you're not peeking at product 2's images).
File#basename will return only the filename:
File.basename('http://www.test.com/product4.jpg')
#=> "product4.jpg"
So you probably want something like this:
puts product.xpath('//img[#width = 220]/#src').map {|a| File.basename(a.value) }

Catalog Result By Positional Order in Contents

I want to have catalog result displayed using the following codes from my Archetypes package. The issue is the displayed listing not according to the positional order in Contents tab. What am I missing?
class BookView(BrowserView):
implements(IBookView)
def get_chapters(self):
context = aq_inner(self.context)
catalog = getToolByName(context, 'portal_catalog')
folder_path = '/'.join(context.getPhysicalPath())
results = catalog(
path={'query': folder_path, 'depth': 1},
portal_type=('File', 'Chapter')
)
return results
<div class="books-pdf"
tal:define="chapters view/get_chapters"
tal:condition="chapters">
<span class="listname"
i18n:translate="">Chapter Name</span>
<span class="iconname"
i18n:translate="">File Type</span>
<span class="sizename"
i18n:translate="">File Size</span>
<tal:chapters repeat="chapter chapters">
...
The catalog returns items in internal order unless you explicitly request an order. You can do so with the sort_on parameter, which should be the name of an index to use for the sorting.
Most indexes can be used for sorting, with the notable exception being full text indexes (which is why there is a sortable_title index in the Plone catalog).
You probably want to sort on the getObjPositionInParent index, which holds the index of each object in it's container:
results = catalog(
path=dict(query=folder_path, depth=1),
portal_type=('File', 'Chapter'),
sort_on='getObjPositionInParent',
)
You need to sort results by this index:
getObjPositionInParent

How can I model a scalable set of definition/term pairs?

Right now my flashcard game is using a prepvocab() method where I
define the terms and translations for a week's worth of terms as a dictionary
add a description of that week's terms
lump them into a list of dictionaries, where a user selects their "weeks" to study
Every time I add a new week's worth of terms and translations, I'm stuck adding another element to the list of available dictionaries. I can definitely see this as not being a Good Thing.
class Vocab(object):
def __init__(self):
vocab = {}
self.new_vocab = vocab
self.prepvocab()
def prepvocab(self):
week01 = {"term":"translation"} #and many more...
week01d = "Simple Latvian words"
week02 = {"term":"translation"}
week02d = "Simple Latvian colors"
week03 = {"I need to add this":"to self.selvocab below"}
week03d = "Body parts"
self.selvocab = [week01, week02] #, week03, weekn]
self.descs = [week01d, week02d] #, week03, weekn]
Vocab.selvocab(self)
def selvocab(self):
"""I like this because as long as I maintain self.selvocab,
the for loop cycles through the options just fine"""
for x in range(self.selvocab):
YN = input("Would you like to add week " \
+ repr(x + 1) + " vocab? (y or n) \n" \
"Description: " + self.descs[x] + " ").lower()
if YN in "yes":
self.new_vocab.update(self.selvocab[x])
self.makevocab()
I can definitely see that this is going to be a pain with 20+ yes no questions. I'm reading up on curses at the moment, and was thinking of printing all the descriptions at once, and letting the user pick all that they'd like to study for the round.
How do I keep this part of my code better maintained? Anybody got a radical overhaul that isn't so....procedural?
You should store your term:translation pairs and descriptions in a text file in some manner. Your program should then parse the text file and discover all available lessons. This will allow you to extend the set of lessons available without having to edit any code.
As for your selection of lessons, write a print_lesson_choices function that displays the available lessons and descriptions to the user, and then ask for their input in selecting them. Instead of asking a question of them for every lesson, why not make your prompt something like:
self.selected_weeks = []
def selvocab(self):
self.print_lesson_choices()
selection = input("Select a lesson number or leave blank if done selecting: ")
if selection == "": #Done selecting
self.makevocab()
elif selection in self.available_lessons:
if selection not in self.selected_weeks:
self.selected_weeks.append(selection)
print "Added lesson %s"%selection
self.selvocab() #Display the list of options so the user can select again
else:
print "Bad selection, try again."
self.selvocab()
Pickling objects into a database means it'll take some effort to create an interface to modify the weekly lessons from the front end, but is well worth the time.

Obtain data from dynamically incremented IDs in JQuery

I have a quick question about JQuery. I have dynamically generated paragraphs with id's that are incremented. I would like to take information from that page and bring it to my main page. Unfortunately I am unable to read the dynamically generated paragraph IDs to get the values. I am trying this:
var Name = ((data).find("#Name" + id).text());
The ASP.NET code goes like this:
Dim intI As Integer = 0
For Each Item As cItem in alProducts1
Dim pName As New System.Web.UI.HtmlControls.HtmlGenericControl("p")
pName.id = "Name" & intI.toString() pName.InnerText = Item.Name controls.Add(pName) intI += 1
Next
Those name values are the values I want...Name1, name2, name3 and I want to get them individually to put in their own textbox... I'm taking the values from the ASP.NET webpage and putting them into an AJAX page.
Your question is not clear about your exact requirement but you can get the IDs of elements with attr method of jQuery, here is an example:
alert($('selector').attr('id'));
You want to select all the elements with the incrementing ids, right?
// this will select all the elements
// which id starts with 'Name'
(data).find("[id^=Name]")
Thanks for the help everyone. I found the solution today however:
var Name = ($(data).find('#Name' + id.toString()).text());
I forgot the .toString() part and that seems to have made the difference.

Resources