Gubbins Posted January 28, 2010 Share Posted January 28, 2010 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); } Quote Link to comment Share on other sites More sharing options...
wee493 Posted January 28, 2010 Share Posted January 28, 2010 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 Quote Link to comment Share on other sites More sharing options...
Gubbins Posted January 28, 2010 Author Share Posted January 28, 2010 The mysql_query must be pulled from the field playername but what statement do i put in place of 'x' ? Quote Link to comment Share on other sites More sharing options...
Gubbins Posted January 28, 2010 Author Share Posted January 28, 2010 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... Quote Link to comment Share on other sites More sharing options...
wee493 Posted January 28, 2010 Share Posted January 28, 2010 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 Quote Link to comment Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 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. Quote Link to comment Share on other sites More sharing options...
Gubbins Posted January 28, 2010 Author Share Posted January 28, 2010 Yes Yucky Thats it, so i may want to display [player]Gubbins[/player] one one page and maybe your name next to it [player]Yucky[/player] both being so when you click the name it takes you to that players profile... Quote Link to comment Share on other sites More sharing options...
Andy-H Posted January 28, 2010 Share Posted January 28, 2010 $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 Quote Link to comment Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 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! Quote Link to comment Share on other sites More sharing options...
Gubbins Posted January 28, 2010 Author Share Posted January 28, 2010 $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... Quote Link to comment Share on other sites More sharing options...
Gubbins Posted January 28, 2010 Author Share Posted January 28, 2010 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... Quote Link to comment Share on other sites More sharing options...
Andy-H Posted January 28, 2010 Share Posted January 28, 2010 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>" Quote Link to comment Share on other sites More sharing options...
dbradbury Posted January 28, 2010 Share Posted January 28, 2010 are you trying to do this? http://stacksteadsband.host56.com/meettheplayers.php and when you click the name, info come up or something? Quote Link to comment Share on other sites More sharing options...
Gubbins Posted January 28, 2010 Author Share Posted January 28, 2010 thats link doesnt work. But dont worry Andy-H solved it cheers mate.. Quote Link to comment Share on other sites More sharing options...
dbradbury Posted January 29, 2010 Share Posted January 29, 2010 lol its alright, that literally just happened!! it was fine five mins ago lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.