Jump to content

Displaying a clickable playername


Gubbins

Recommended Posts

I am going wrong somewhere with the mysql_query, can someone please put me straight?

 

$playername = mysql_query("SELECT * FROM `players` WHERE `playername` LIMIT 1");
while($f = mysql_fetch_array($playername)){

$txt = str_replace("[player]", "<a href='profile.php?player={$f['playername']}'>{$f['playername']}>", $txt);
$txt = str_replace("[/player]", "</a>", $txt);
}

Link to comment
https://forums.phpfreaks.com/topic/190167-displaying-a-clickable-playername/
Share on other sites

I am going wrong somewhere with the mysql_query, can someone please put me straight?

 

$playername = mysql_query("SELECT * FROM `players` WHERE `playername` LIMIT 1");
while($f = mysql_fetch_array($playername)){

$txt = str_replace("[player]", "<a href='profile.php?player={$f['playername']}'>{$f['playername']}>", $txt);
$txt = str_replace("[/player]", "</a>", $txt);
}

 

$playername = mysql_query("SELECT * FROM `players` WHERE `playername` LIMIT 1");

This is your error.

 

SELECT * FROM `players` WHERE `playername` LIMIT 1

 

you're not specifying anything. It should be WHERE playername = 'x' or just remove the WHERE statement so it would be.

 

SELECT * FROM `players` LIMIT 1

 

SELECT * FROM `players` LIMIT 1

 

I did this but it only pulls the first name in the database no matter what name i want it to display...

 

$get_name = 'jeff';

$playername = mysql_query("SELECT * FROM `players` WHERE playername = '$get_name' LIMIT 1");

 

Set $get_name equal to the name you want to get from the database

What's happening is that on the first iteration you're replacing every instance of [player] and [/player] with the name of the first player pulled out of the database.

 

By the time the loop hits the second record, there's nothing left to replace.

 

I'm not entirely sure if there's a way to solve it without changing the contents of your $txt variable or writing another function to only replace one instance at a time.

 

Does your variable contain something like this?

 

[player][/player]
[player][/player]
[player][/player]

 

If you could post an example of the content in the variable I might be able to be of more use. :)

I know it's possible to only replace one line at a time with str_replace with extra code, and that preg_replace has a limit argument.

 

Would it be possible to build your $txt variable so it could contain the player names that you wanted to replace?

 

Something along the lines of:

$query= mysql_query("SELECT `playername` FROM `players`");
while ($array= mysql_fetch_array($query)) {
$txt .= "[player]".$array['playername']."[/player]\n";
}

 

Then something like:

 

$playername = mysql_query("SELECT * FROM `players`");
while($f = mysql_fetch_array($playername)){
$txt = str_replace("[player]$playername", "<a href='profile.php?player={$f['playername']}'>{$f['playername']}>", $txt);
$txt = str_replace("[/player]", "</a>", $txt);
}

 

There are better ways of doing it though! ;)

 

$str = "[player]Andy[/player]";

$pattern = '~\[player\](.*)\[\/player\]~i';

$replacement = '<a href="profile.php?player=$1">$1</a>';

$link = preg_replace($pattern, $replacement, $str);

 

 

There you go Trev ;)

 

Thanks Andy but that didnt work at all :S i am going to call it a night now but thanks for trying...

I know it's possible to only replace one line at a time with str_replace with extra code, and that preg_replace has a limit argument.

 

Would it be possible to build your $txt variable so it could contain the player names that you wanted to replace?

 

Something along the lines of:

$query= mysql_query("SELECT `playername` FROM `players`");
while ($array= mysql_fetch_array($query)) {
$txt .= "[player]".$array['playername']."[/player]\n";
}

 

Then something like:

 

$playername = mysql_query("SELECT * FROM `players`");
while($f = mysql_fetch_array($playername)){
$txt = str_replace("[player]$playername", "<a href='profile.php?player={$f['playername']}'>{$f['playername']}>", $txt);
$txt = str_replace("[/player]", "</a>", $txt);
}

 

There are better ways of doing it though! ;)

 

 

Thanks Yucky but that didnt work at all :S i am going to call it a night now but thanks for trying...

Worked fine here. You trying to make a dynamic bbcode or something, right?

 

 

<?php

$str = "[player]Andy[/player]";

$pattern = '~\[player\](.*)\[\/player\]~i';

$replacement = '<a href="profile.php?player=$1">$1</a>';

$link = preg_replace($pattern, $replacement, $str);

var_dump(htmlentities($link));

?>

 

 

Output:

string(64) "<a href="profile.php?player=Andy">Andy</a>"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.