Forum Moderators: coopster

Message Too Old, No Replies

Splitting column data.

         

dkin

7:25 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



I am trying to seperate a columns input so I can have many links from the one column.

eg

in one cell I have widget widget widget.
Linking this the normal way I would get 1 big link.
I would like to split the input so I can have each "widget" as its own link.

Thank you for any help.

httpwebwitch

7:30 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$links = explode(" ","widget widget widget");
// split the string by a space
// creates an array ("widget","widget","widget")

foreach($links as $v){
print("<a href='$v'>$v</a>");
}
// walks through array and prints separate links

// cheers

dmorison

7:33 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the target is the same for each word; then you could do something like this:


$target = "http://www.example.com/";

$mystring="widget widget widget";

$widgets = explode(" ",$mystring);

foreach($widgets as $widget)
{
echo("<a href='$target'>$widget</a> ");
}

dkin

7:37 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



webwitch,

would I put that in the output script or does it have to go in the form processor before it is entered into the database?

dmorison

7:37 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, httpwebwitch beat me to it but there's a bug in his code ;)

dkin

7:38 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



where is the bug?

dmorison

7:40 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No space after each widget, but having said that, my code will also give you a redundant space at the end.

[edited by: dmorison at 7:42 pm (utc) on July 15, 2004]

dkin

7:41 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



and will these codes work when calling from a database?

dmorison

7:44 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It doesn't matter where "widget widget widget" has come from, these solutions are generic. You can get the string from a database, a form input, hard coded, wherever.

dkin

7:46 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



ok, I would like to dynamically link them to seperate pages,will this work with your code?

dmorison

7:49 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do you know what those separate pages are going to be?

httpwebwitch

8:16 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is your string something like
"h$tp://www.mydomain.com/index.htm h$tp://www.mydomain.com/happy.htm h$tp://www.mydomain.com/golucky.htm"

?

Don't store HTML in your database, just keep the data as-is and add the links when you display it.

Sorry about the bug. Here's a fixed version:

$text="URL URL URL";

$links=explode(" ",$text);
$new=array();
foreach($links as $v){
$new[]="<a href='$v'>$v</a>";
}
print(implode(" ",$new));

inside the foreach loop you can also apply functions that verify the link as http or mailto, add the "www" or "http" if they're missing, append a "/" to a URL path with no file extension, etc etc.

[edited by: httpwebwitch at 8:17 pm (utc) on July 15, 2004]

dkin

8:16 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



they are in a seperate table.

I have two tables in one there will be a entry like this "enc mag pal war"

In the other I have a row with all of these acronyms in it. if the text(after being split) matches to what is in one of those rows I would like it to link to it.

httpwebwitch

8:25 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Do the explode on " " to get your array.
foreach through the array {
SELECT that item from the database.
if (you find something) {
wrap your <a> around the word
}
push it into a new array
}
implode the new array and print it.

cheers

dkin

3:27 am on Jul 16, 2004 (gmt 0)

10+ Year Member



I really do not understand what you just wrote, I am not overly skilled when it comes to arrays and the explode function. I messed around with a couple of these codes but nothing produced what I was looking for, or I could not get them to work. Could you post some code for that last post and I will try to adapt that to my needs?