/    Sign up×
Community /Pin to ProfileBookmark

making links from an array built from a sql statement

I gather all the names using this function and put them in an array

[code=php]
//////////////////
// band links ///
////////////////
function bandmatch($connection)
{
global $band_array;
$table_name = “band_list”;
$sql = “SELECT `band` FROM `band_list`”;
$resultband = mysql_query($sql) or die (‘Error line 470:’ . mysql_error());
$band_array = “”;
while($row = mysql_fetch_array($resultband))
{
$band = ($row[‘band’]);
$band_array .= “$band, ”;
}

}
[/code]

I am confused on this part here: I need to be able to (when there is a match on a name in the array) provide a link with the name in the $_GET I suspect something like this..

[code=php]
//$details hold text I am looking for the match in.
$details = preg_replace($band_array)b/i’,
“<a href=”folder/get_info.php?name=match_from_array” title=”>$1</a>”,
[/code]

I can do everything form that point I just don’t know how to get the match out of the array and place it in to the link ?name=The Match

Example of what I am trying to accomplish:

Array:
$band_array = (thisband, that band, other name);

Paragraph:
If I want [B]this band[/B] and [B]that band[/B] to be links then I need to get this figured out.

Result:

[code=html]

If I want <a href=”folder/get_info.php?name=this band”>this band</a> and <a href=”folder/get_info.php?name=that band”>that band</a> to be links then I need to get this figured out.

[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Markbad311authorApr 14.2006 — ..::BuMp::..
Copy linkTweet thisAlerts:
@NogDogApr 14.2006 — How about something like this (untested)?
[code=php]
function bandmatch($connection)
{
$band_array = array();
$sql = "SELECT band FROM band_list";
$resultband = mysql_query($sql) or die ('Error line 470:' . mysql_error());
while($row = mysql_fetch_array($resultband))
{
$band_array[] = $row['band'];
}
return($band_array);
}
$bandList = bandmatch($connection);
// turn into search and replace arrays:
$search = array();
$replace = array();
foreach($bandList as $val)
{
$search[] = '/b'.$val.'b/i';
$replace[] = sprintf("<a href='folder/get_info.php?name=%s'>%s</a>",
urlencode($val), $val);
}
$finalResult = preg_replace($search, $replace, $inputText);
[/code]
Copy linkTweet thisAlerts:
@Markbad311authorApr 18.2006 — Modified Code:
[code=php]
function bandmatch($connection)
{
$band_array = array();
$sql = "SELECT band FROM band_list";
$resultband = mysql_query($sql) or die ('Error line 24:' . mysql_error());
while($row = mysql_fetch_array($resultband))
{
$band_array[] = $row['band'];
}
return($band_array);
}
$bandList = bandmatch($connection);
// turn into search and replace arrays:
$search = array();
$replace = array();
foreach($bandList as $val)
{
$search[] = '/b'.$val.'b/i';
$replace[] = sprintf("<a href='folder/get_info.php?name=%s'>%s</a>",
urlencode($val), $val);
}
echo "$band_array";
//nothing echos back
$details = preg_replace($search, $replace, $details);//line42
//$details is the variable containing the text I want to
//search through and replace stuff in.
[/code]



Well hardly modified at all. I am recieving two preg_replace warnings

Warning: preg_replace(): Unknown modifier 'G' in /home/markbad/public_html/index2.php on line 42

Warning: preg_replace(): Unknown modifier 'Z' in /home/markbad/public_html/index2.php on line 42[/quote]


and when I try to echo back the array as noted above I recieve no output. the connection to the db is set in another function imported to this file. (hence $connection). I am not to familiar with preg_replaces as I have researched them a lot and still very clumsy with the method.
Copy linkTweet thisAlerts:
@NogDogApr 18.2006 — Is it possible any of the items in your band list has a slash ("/") in it? If so, you'll need to escape it with a back-slash.
Copy linkTweet thisAlerts:
@Markbad311authorApr 19.2006 — Ahh thanks. Definetely works and man is that a useful little function. Thanks a ton for giving me a hand NogDog. I am sure my users will appreciate it too.


until next time

-Mark
×

Success!

Help @Markbad311 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...