Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with loop 1

Status
Not open for further replies.

florens

IS-IT--Management
Nov 13, 2002
50
NL
Hi, I've got a bit of a problem and I can't see how I should fix this.
I've got two tables in my database, pages and pictures.
In the pages table I store the content of my webpages, and in pictures I store the filename of my pictures and the page_id.

Now through some simple CMS page users can alter the webpages. They can choose several templates for a site, and type some text, but nothing more. In the templates, I've set some places for images to appear (I've just coded: <img src="#1" />).

This all works fine, my problem appears when I want to show the specific pages. I just get the page content from the database, but then I need to replace the #1, #2 etc. by the filename from my picture.
I figured I could do this with a for each loop or something, but I just can't get it to work (this is my first encounter with a for loop, so I don't really know how to set it up).

This is what I got so far:
I get the pictures from my database with a datareader, and then I tried to put the pictures in an array:
Do While reader.Read()
pictureArray.Add(reader("pic_file_name").ToString)
Loop

But then I have no idea how to replace the #1, #2 etc. with the contents of the arraylist.

Does anyone have an idea how to do this, I'm not asking for the exact code, just a push in the right direction would be great.
Thanks
 
Something like this should give you an idea of how it could be done:
Code:
            Dim myArray As Array
            Dim myHTML As String
            For i As Integer = 0 To myArray.Length
                myHTML = myHTML.Replace("<img src=""#" & i & """ />", "<img src=""#" & myArray.GetValue(i)) & """ />"
            Next
In the above example, I simply loop through an array and replace "i" with the contents of the relevant array item.



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for the quick response, I've used your code and altered it a bit to fit in with the rest of my code. I was actually using an arraylist and not an array (I'm not sure what the difference is, but I can't seem to get data from my datareader into an array, but I can get it in an arraylist (at least I think, I'm not getting any errors)).

So I changed your code to:
Dim i As Integer
Dim content As Label = FindControl("lblContent")
For i = 0 To pictureArray.Count
content.Text = content.Text.Replace("#" & i, "admin/files" & pictureArray.Item(i))
Next

If I chage the pictureArray.item(i) part to some hardcoded image reference it works fine, but like this I get the following error (I've translated it, so it's probably not literally as it would be in english):

System.ArgumentOutOfRangeException: Index is out of reach. The index can't be negative and should be smaller then the size of the collection.

I don't know how to fix this, I think it has something to do with the content of i, but I can't figure out what it is exactly.
 
It's probably to do with the amount of items in your ArrayList. For example, the count may be 3 but as the array starts with zero, the items are actually numbered 0, 1 and 2. So, if you change:
Code:
For i = 0 To pictureArray.Count
to:
Code:
For i = 0 To (pictureArray.Count - 1)
then you should be OK.

Another (and better) way to loop through the ArrayList, would be to use an IEnumerator e.g.
Code:
        ' Declare an arraylist
        Dim myArrayList As New ArrayList

        ' populate the arraytlist with some sample data
        myArrayList.Add(1)
        myArrayList.Add(2)
        myArrayList.Add(3)

        ' Declare an IEnumerator which is used to loop through the arraylist
        Dim myEnum As IEnumerator = myArrayList.GetEnumerator

        ' Loop through the enumerator and write out each value
        While myEnum.MoveNext
            Response.Write(myEnum.Current)
        End While


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thank you very much, it works like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top