locked
Hyperlinking Comma Seperated Data RRS feed

  • Question

  • User-2051275383 posted

    Hey guys,

    I am hoping this is easy and I cant seem to find an easy way of doing it.

    I have a query that returns comma seperated data, to which I wanted hyperlinked.

    ie. text1, text2, text3 (original)

    Coverted to

    ie. text1, text2, text3

    Is there an easy way to achieve this?

    Cheers!

    Wednesday, September 17, 2014 2:14 AM

Answers

  • User-821857111 posted
    var data = db.QueryValue(sql); // returns just the comma separated value
    @foreach(var item in data.ToString().Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)){
        <a href="@item">@item</a> 
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 17, 2014 2:27 AM
  • User-821857111 posted
    var sql = "SELECT * From Test";
    var data = db.Query(sql);
    
    @foreach(var row in data){
    <div>
        foreach(var item in row.Content.ToString().Split(new[]{','},StringSplitOptions.RemoveEmptyEntries)){
            @item<br />
        }
        @row.Column2<br />
        @row.Column3
    </div>
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 17, 2014 6:23 AM
  • User-821857111 posted

    Any C# book will do. There's a free one here: http://www.robmiles.com/c-yellow-book/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 17, 2014 7:04 AM

All replies

  • User-821857111 posted
    var data = db.QueryValue(sql); // returns just the comma separated value
    @foreach(var item in data.ToString().Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)){
        <a href="@item">@item</a> 
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 17, 2014 2:27 AM
  • User-2051275383 posted

    var data = db.QueryValue(sql); // returns just the comma separated value
    @foreach(var item in data.ToString().Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)){
        <a href="@item">@item</a> 
    }



    Thanks for the quick reply Mike, I am always finding new ways of displaying data - Yourself (and the community) provide excellent help and I am very appreciative.

    On second glance, I don't know if I made my OP clear enough. Will this above code work if my data that in in the database already has the comma? 

    ie. "data1, daae 2" would be saved in the database exactly like that, not two separate pieces (ie. "data1","data2").

    Thanks again Mike!

    Wednesday, September 17, 2014 4:01 AM
  • User-821857111 posted

    Will this above code work if my data that in in the database already has the comma? 

    Yes - that's what I assumed you meant - that you were storing comma-separated strings as single values. The code I posted will take the single value and split it on the comma, creating an array of values. 

    Wednesday, September 17, 2014 4:18 AM
  • User-2051275383 posted

    Thanks for yet the very prompt reply. Looks like I got it working however (to make things more complicated), I have one column that I wish to apply the comma + hyperlink code to and return the rest.

    I managed to get it working by doing this. My 'test' table has three columns: Content, Column2 and Column3 - looks cumbersome but it works:

    @{
           var db= Database.Open("database");
           var sqlQ = "SELECT * FROM Test";
          
    //Hyperlink and seperate
           var data = db.QueryValue(sqlQ); 
    
    //Return all other data in table
           var data1 = db.Query(sqlQ);
         }      
    <div>
         @foreach(var item in data.ToString().Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)){
        <p><a href="@item">@item</a></p> 
    }@foreach(var row in data1){
         <p>@row.column2</p>
         <p>@row.column3</p>
    }
         </div>
        

    Would there be a more efficient way of just applying it to a particular column (in this instance Content) and returning the other column results as per normal?

    Very grateful for your help!

    Wednesday, September 17, 2014 4:23 AM
  • User-2051275383 posted

    Actually I lie.. It seems to just return the first set of data in the table in column called Content and that's it..

    Wednesday, September 17, 2014 4:37 AM
  • User-821857111 posted

    So is that what you want?

    Wednesday, September 17, 2014 4:45 AM
  • User-2051275383 posted

    Nope, I would like to pull out all of the data in that Content column in the format as per your code, not just the first result.

    I am guessing that is far more difficult? 

    Wednesday, September 17, 2014 4:59 AM
  • User-821857111 posted
    var sql = "SELECT * From Test";
    var data = db.Query(sql);
    
    @foreach(var row in data){
    <div>
        foreach(var item in row.Content.ToString().Split(new[]{','},StringSplitOptions.RemoveEmptyEntries)){
            @item<br />
        }
        @row.Column2<br />
        @row.Column3
    </div>
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 17, 2014 6:23 AM
  • User-2051275383 posted

    This is absolutely perfect, I made some slight tweaks.. Final result is exactly what I was after..

    @{
           var db= Database.Open("Movies");
    
           var sql = "SELECT * From Test";
           var data = db.Query(sql);
           }
    <div>
    @foreach(var row in data){
    
        foreach(var item in row.Content.ToString().Split(new[]{','},StringSplitOptions.RemoveEmptyEntries)){
           <a href="~/@item">@item</a><br />
        }
        @row.Column2<br />
        @row.Column3<br />
    }
    </div>

    Thank you so much Mike, very much appreciated. I wish there were more 'Razor' materials available but looks like things will change with vNext anyway..

    Cheers mate!

    Wednesday, September 17, 2014 6:35 AM
  • User-821857111 posted

    I wish there were more 'Razor' materials available

    Actually, the issue you faced is a C# question. The solution is in C#. The only Razor bit is the @ signs to make the values render. 

    Wednesday, September 17, 2014 6:41 AM
  • User-2051275383 posted

    a-rad

    I wish there were more 'Razor' materials available

    Actually, the issue you faced is a C# question. The solution is in C#. The only Razor bit is the @ signs to make the values render. 



    Forgive my ignorance - Can you recommend any resources/tutorials for C#?

    Wednesday, September 17, 2014 6:56 AM
  • User-821857111 posted

    Any C# book will do. There's a free one here: http://www.robmiles.com/c-yellow-book/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 17, 2014 7:04 AM