View Full Version : Splitting arrays with sentences
saljb
06-01-2005, 11:03 PM
I have an array I want to split. This is the code I am currently using:
<%
dim aSelected
aSelected = split(request.form("option"),",")
for i = 0 to ubound(aSelected)
response.write (aSelected(i) & ("<br>"))
next
%>
This splits the array no problem. The problem I have is that the different variables (if thats the word to use?) the array is holding is sentences that contain commas in themselves. So the split is splitting my sentences. How can I split the variables at the end of the sentences. (some sentences have periods some don't so I can't use the ".")
I read somewhere about the 'mid' function and 'mid' statement. If that is the solution can some one show me an example of how I would do it.
saljb
06-01-2005, 11:04 PM
It didn't work. I don't add double spaces after a sentence. A little background these sentences were taken from a database put in a form then put in an array in which I am calling now.
This is an eg. of a sentence I have w/out a comma:
Additional standard basement sash with window well
With Comma:
G.E. #DJXR433 electric clothes dryer – includes electric hook-up, gas hook-up deleted (white-on-white or bisque-on-bisque)
The way the above examples look when inserted without a split:
Additional standard basement sash with window well, G.E. #DJXR433 electric clothes dryer – includes electric hook-up, gas hook-up deleted (white-on-white or bisque-on-bisque),
SalJB
p.s.
I also just noticed there are sentences like this:
Recessed medicine cabinet in vanity end wall, 16" x 24" with mirrored door, wood frame to match vanity and two shelves (installed)
That are stop showing the rest of the sentence after the 16. I am guessing its because of the quote. But that is part of the senetnce since it describes measurement. So how can I solve this issue and most importantly the first one mentioned in the first thread.
glenngv
06-02-2005, 03:55 AM
You should have just PM'ed the mod to move the original thread (http://www.codingforums.com/showthread.php?t=60230) instead of starting this new thread. Cross-posting is against forum rules. But since you already posted this, you should at least link this thread to the original thread so that others can quickly follow the discussions here.
Anyway, to answer your question...
Are the sentences stored in different textareas with the same name "option"?
If they are, then this is the simple solution:
for each item in request.form("option")
response.write Server.HTMLEncode(item) & "<br />"
next
HTMLEncode function encodes the strings so that characters & " < > are encoded to its HTML entity equivalent & " < >
saljb
06-02-2005, 04:05 PM
I apologize and I will make sure to follow protocal the next time. :thumbsup:
Thanks GlennGV for the code it worked. I do have two more followup questions.
1. The first is concerning the second issue I stated that parts of the sentences have quotes and when I call up the form parts of the sentences after the first quote " is cut off.
e.g.:
Actual Sentence:
Recessed medicine cabinet in vanity end wall, 16" x 24" with mirrored door, wood frame to match vanity and two shelves (installed)
How sentence appears after its called up:
Recessed medicine cabinet in vanity end wall, 16
2. My second question is concenring the layout.
This is how my current setup is:
<tr>
<td>
<%
for each item in request.form("option")
response.write Server.HTMLEncode(item) & "<br /><hr size=1>"
next
%>
</td>
<td>
<%
for each item in request.form("price2")
response.write (FormatCurrency(Server.HTMLEncode(item),0)) & "<br /><hr size=1>"
next
%>
</td>
</tr>
The problem I have is that "option" and "Price2" don't line up by each other.
Example being
item1:item1
item2:item2
item3:item3
instead it comes out like
item1:
item2:
item3:item1
item4:item2
item5:item3
item6:
The is one "price2" for every "option"
Would something similiar to this work:
<tr>
<td>
<%
for each item in request.form("option" & "price2")
response.write Server.HTMLEncode(item) & "<br /><hr size=1>"
next
%>
</td>
</tr>
I do need to add the formatcurrency to the "price2"
glenngv
06-02-2005, 04:13 PM
1. Server.HTMLEncode should have encoded the double quotes to ". Can you post the generated code for the sentences?
2. Since you put the content in a <td> and the default vertical alignment for <td> is "middle", the content is vertically-centered. You just set the vertical alignment to "top" to make the content aligned to the top.
<tr>
<td style="vertical-align:top">
<%
for each item in request.form("option")
response.write Server.HTMLEncode(item) & "<br /><hr size=1>"
next
%>
</td>
<td style="vertical-align:top">
<%
for each item in request.form("price2")
response.write (FormatCurrency(Server.HTMLEncode(item),0)) & "<br /><hr size=1>"
next
%>
</td>
</tr>
saljb
06-02-2005, 04:49 PM
Thanks GlennGV
1. I should have mentioned I did try the valign="top" but it still doesn't align correctly. The all are placed vertically at the top but Reason being that some sentences ("option") take mulitple lines (up to 3) while all the "price2" just take a single line.
2.
The way I have setup is that I have Page 1 which is a form and I have multiple categories with this code in each the difference in each is the Where type:
<%sSQL="SELECT * FROM options Where type='foundation'"%>
They are checkboxes for each option that comes up. The highlighted code is the info that gets passed to page2 in an array in hitting submit. They are a parent checkbox and two hidden child checkboxes:
<!--#include virtual="aspCode1.inc"-->
<%sSQL="SELECT * FROM options Where type='foundation'"%>
<!--#include virtual="aspCode2.inc"-->
<tr>
<td><%Response.Write Recordset("id")%>. </td>
<td><%Response.Write Recordset("option")%></td>
<td><%Response.Write FormatCurrency((Recordset("price")),0)%></td>
<td><input type="checkbox" name="price" value="<%Response.Write Recordset("price")%>" onclick="toggleGroup(this)" /><input type="checkbox" name="option" value="<%Response.Write Recordset("option")%>" style="display:none "/><input type="checkbox" name="price2" value="<%Response.Write Recordset("price")%>" style="display:none "/></td>
</tr>
<!--#include virtual="aspCode3.inc"-->
On page 2 I have this code:
<%
dim cSelected
dim total
cSelected = split(request.form("price"),",")
total = 0
for i = 0 to ubound(cSelected)
if IsNumeric(cSelected(i)) then
total = total + CDbl(cSelected(i))
end if
next
%>
<table>
<tr>
<td valign="top">
<%
for each item in request.form("option")
response.write Server.HTMLEncode(item) & "<br /><hr size=1>"
next
%>
</td>
<td valign="top">
<%
for each item in request.form("price2")
response.write (FormatCurrency(Server.HTMLEncode(item),0)) & "<br /><hr size=1>"
next
%>
</td>
</tr>
<tr><td>Sub-Total Options: <%response.write FormatCurrency((total),0)%></td></tr>
<tr><td>Sub-Total Home: <%Response.Write FormatCurrency((Recordset("community")),0)%></td></tr>
<tr><td>Total:</td></tr>
</table>
The highlighted blue is code I still haven't figured out that will allow me to add:
<%response.write FormatCurrency((total),0)%> and <%Response.Write FormatCurrency((Recordset("community")),0)%> for a new total. (if you have a solution for this one too it will be great. ;)
glenngv
06-02-2005, 05:13 PM
You should also Server.HTMLEncode the value when you put it in the checkbox.<input type="checkbox" name="option" value="<%=Server.HTMLEncode(Recordset("option"))%>" style="display:none "/>
For the total:<%
dim subtotalOpt, subtotalHome, total
subtotalOpt = FormatCurrency((total),0)
subtotalHome = FormatCurrency((Recordset("community")),0)
total = FormatCurrency(subtotalOpt + subtotalHome, 0)
%>
<tr><td>Sub-Total Options: <%=subtotalOpt%></td></tr>
<tr><td>Sub-Total Home: <%=subtotalHome%></td></tr>
<tr><td>Total: <%=total%></td></tr>
</table>
For the alignment problem, I'm still thinking of the best solution.
saljb
06-02-2005, 05:40 PM
Thanks, it mostly worked with an adjustment. What do you think?
For the Total:
<%
dim subtotalOpt, subtotalHome, total
subtotalOpt = FormatCurrency((total),0)
subtotalHome = FormatCurrency((Recordset("community")),0)
total = FormatCurrency(subtotalOpt + subtotalHome, 0)
%>
<tr><td>Sub-Total Options: <%=subtotalOpt%></td></tr>
<tr><td>Sub-Total Home: <%=subtotalHome%></td></tr>
<tr><td>Total: <%=total%></td></tr>
</table>
I changed to:
<%
dim subtotalOpt, subtotalHome, total2
subtotalOpt = FormatCurrency((total),0)
subtotalHome = FormatCurrency((Recordset("community")),0)
total2 = (subtotalOpt + subtotalHome)
%>
<tr><td>Sub-Total Options: <%=subtotalOpt%></td></tr>
<tr><td>Sub-Total Home: <%=subtotalHome%></td></tr>
<tr><td>Total: <%=total%></td></tr>
</table>
reasons:
1. I was getting this error:
Microsoft VBScript compilation error '800a0411'
Name redefined
/checkboxtrial2.asp, line 93
dim subtotalOpt, subtotalHome, total
-------------------------------^
2. After I changed total to total2, I got this error:
Price this Home with addtional Options
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'FormatCurrency'
/checkboxtrial2.asp, line 96
So I changed:
total2 = FormatCurrency(subtotalOpt + subtotalHome, 0)
to
total2 = (subtotalOpt + subtotalHome)
3. No my result appears as:
$80,268$91,400 instead of total of those two numbers.
saljb
06-02-2005, 07:04 PM
I firgured out the
3. Now my result appears as:
$80,268$91,400 instead of total of those two numbers.
with
<%
dim subtotalOpt, subtotalHome, total2
subtotalOpt = FormatCurrency((total),0)
subtotalHome = FormatCurrency((Recordset("community")),0)
total2 = int(subtotalOpt) + int(subtotalHome)
total2 = formatcurrency((total2),0)
%>
still have the align issue
glenngv
06-03-2005, 09:06 AM
still have the align issue
You should put the options and prices in one cell.
The final output should be something like this:
<style type="text/css">
.record {
border:1px solid black;
margin-bottom:2px;
padding:2px;
width:500px;
clear:both;
float:left;
}
.spanleft {
width:370px;
float:left;
margin:2px;
}
.spanright {
width:110px;
float:right;
padding-left:10px;
}
</style>
...
<tr>
<td>
<div class="record">
<span class="spanleft">Recessed medicine cabinet in vanity end wall, 16" x 24" with mirrored door, wood frame to match vanity and two shelves (installed). Recessed medicine cabinet in vanity end wall, 16" x 24" with mirrored door, wood frame to match vanity and two shelves (installed)</span>
<span class="spanright">$1,000,000.00</span>
</div>
<div class="record">
<span class="spanleft">Recessed medicine cabinet in vanity end wall, 16" x 24" with mirrored door, wood frame to match vanity and two shelves (installed)</span>
<span class="spanright">$1,000.00</span>
</div>
<div class="record">
<span class="spanleft">Recessed medicine cabinet in vanity end wall, 16" x 24" with mirrored door, wood frame to match vanity and two shelves (installed). Recessed medicine cabinet in vanity end wall, 16" x 24" with mirrored door, wood frame to match vanity and two shelves (installed)</span>
<span class="spanright">$1,000,000.00</span>
</div>
</td>
</tr>
To have that output, the asp code should be like this:
<%
function createTag(what)
select case what
case "spanleft"
case "spanright"
createTag = "<span class=""" & what & """>"
case "closespan"
createTag = "</span>"
case "opendiv"
createTag = "<div class="""record""">"
case "closediv"
createTag = "</div>"
end select
end function
dim index, strData, allOptions, allPrices
allOptions = request.form("option")
allPrices = request.form("price2")
for index = 0 to allOptions.count - 1
strData = strData & createTag("opendiv") & VbCrLf & createTag("spanleft") & Server.HTMLEncode(allOptions(index)) & createTag("closespan") & VbCrLf 'options
strData = strData & createTag("spanright") & Server.HTMLEncode(allPrices(index)) & createTag("closespan") & VbCrLf & createTag("closediv") & VbCrLf 'prices
next
%>
<td>
<%=strData%>
</td>
saljb
06-03-2005, 03:07 PM
Thanks GlennGV I appreciate your help!! I was given an alternative solution from ShadowWizard at ASP Development. I used this one because it had a bit less code:
Shadow Wizard:
if you know for sure there will be the same amount of items you can use such loop:
for x=1 To request.form("option").Count
response.write Server.HTMLEncode(request.form("option").Item(x)) &":" & FormatCurrency(Server.HTMLEncode(request.form("price2").Item(x)), 0)
next
I used that code and customized it to my needs:
<%
for x=1 To request.form("option").Count
response.write "<tr><td>" & Server.HTMLEncode(request.form("option").Item(x)) & "</td>" & "<td align='right'>" & FormatCurrency(Server.HTMLEncode(request.form("price2").Item(x)), 0) & "</td></tr>"
next
%>
If you notice anything that I could do to be more efficient or can cause any issues with this code, let me know
Thanks everyone. You have been helpful!!
SalJB
glenngv
06-06-2005, 05:00 AM
Your solution is essentially the same with mine. I just used divs and spans (and not table) to group items in every row and align them left and right.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.