...

View Full Version : pulling info from db and outputting separate lines



Meltdown
02-23-2008, 03:09 AM
Input is from textbox and there is ONE URL per line in the textbox (sometimes it's just one url and other times it could be 50 urls). Input goes into mysql db (links)

So my problem:
Example: If I input three lines
one.com
two.com
three.com
The output of the code below would be:
Link: one.com
two.com
three.com
And they are NOT linked. Can anyone explain this? I'm probably not supposed to be using foreach, no?


$result = mysql_query("SELECT * FROM links WHERE callsign='$callsign1'");
$row = mysql_fetch_array($result);
$linksfoo1 = nl2br($row['links']);
$linksfoo = array($linksfoo1);
foreach ($linksfoo as $linksfoo) {
echo "Link: <a href=\"$linksfoo?\">$linksfoo</a>\n";
}

the game hog
02-23-2008, 06:54 AM
I would try to attempt using string replace and adding a simple HTML break tag at the end in an array. Try this:



<?php
$result = mysql_query("SELECT * FROM links WHERE callsign='$callsign1'");
while($rows=mysql_fetch_array($result)) {

//add a closing to the anchor tag and a break tag after any findings of .com
$break_link = str_replace('.com', '.com</a><br />', $rows['links']);

//output your results
echo '<a href="' .$rows['links']. '">' .$break_link. '';

}

?>


In the above example, for each link in the main mysql array, if it has .com in the end, the string replace will add </a><br /> at the end of it, and then I print all those results. I set the variable $break_line equal to the string replace because it allows you to still use the $row['links'] for when using the anchor tag link, <a href="'. $rows['link']. '">

Of course, this would force you to have to have .com at the end of each URL, but in that case, if you were using other domains, you could add those into an array and parse it through it like so:


<?php
$result = mysql_query("SELECT * FROM links WHERE callsign='$callsign1'");
while($rows=mysql_fetch_array($result)) {


//in this one, we extend it to domains beyond ones with .com
//not 100&#37; sure this will work either but you can try it if you are using
//more than just .com names
$links = array('.com', '.net', '.biz', '.org', '.gov');
$break_link = str_replace('$links', '$links</a><br />', $rows['links']);

//output your results
echo '<a href="' .$rows['links']. '">' .$break_link. '';

}

?>


Let me know if I found a solution to your problem! :)

WAIT, I just noticed I forgot to add the name of the link you are echoing, I'm sure you can add that (it's only going to print .com<br /> .com<br />) I'm sure you can fix that! Just make another string replace to take out the .com to print the plain text, then put the break_link string replace. I can't imagine it being much harder, let me know if it works or not though. I have found alot of good uses from str_replace function. Hmm, I'm not sure if you will need to edit it or not.
Well, try it out. If only prints .com<br /> then it might be goofed up, but I think it's good, as the whole link like, three.com<br /> is being set equal to the break_link variable. Sorry, I'm kinda tired to get it right or not.

Meltdown
02-23-2008, 03:00 PM
That doesn't seem to work because it's only recognizing the last "domain.com".

If it is in the db as
"link1.com
link2.com
link3.com"
I need to know how to handle each of those lines individually (really I just need to make them link accordingly)

Meltdown
02-23-2008, 07:39 PM
Developed my own solution after reading up for a few hours.



$result = mysql_query("SELECT * FROM links WHERE callsign='$callsign1'");
$row = mysql_fetch_array($result);
$links = $row['links'];
$chunks = spliti ("
", $links, 10);
//print_r($chunks);
foreach ($chunks as $value) {
echo "Link: <a target=\"_blank\" href=\"$value\">$value</a><br>";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum