Birdman203 Posted July 3, 2006 Share Posted July 3, 2006 I don't know why, but this doesn't work. the $i always stays at 0.[code]$prepareCount = preg_match_all("#\[\[member:(.*?)\]\]#si", $message, $prepare);for ($i = 0; $i < $prepareCount; $i++) { $alias = $prepare[1][$i]; $result = mysql_db_query("typekill_global", "select * from cp_members where alias = '$alias'"); $row = mysql_fetch_array($result); $message = preg_replace("/\[\[member:(.*?)\]\]/si", "<img src=http://global.typekill.net/flags/$row[country].gif> <a href=members.php?mode=profile&id=$row[member_id]>$row[alias]</a>", $message);}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/ Share on other sites More sharing options...
wildteen88 Posted July 3, 2006 Share Posted July 3, 2006 Becuase you havnt told the for loop how many times you want it to loop. $prepareCount doesnt return an [b]integer[/b] but an [b]array[/b] so the for loop doesnt know how many times it needs to loop. So if you use the count function. This will count how many items there are in the array. So the start of your for loop will now look likes this:[code]for ($i = 0; $i < count($prepareCount)-1; $i++) {[/code]Notice [b]-1[/b] after count. This is stops the for loop from going on too many times, for example you have five items in array. Count will say you have 6 as its included the first item in the array which is zero. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-52479 Share on other sites More sharing options...
Birdman203 Posted July 3, 2006 Author Share Posted July 3, 2006 When I did that it like doesn't execute, because it doesn't even replace the [[member:Birdman]] or [[member:ZoID]]EDIT: It's fine how it was before, but the $i++ doesn't execute. Even if I put it at the end below $message. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-52497 Share on other sites More sharing options...
Birdman203 Posted July 3, 2006 Author Share Posted July 3, 2006 Does anyone know what can be wrong with it? Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-52627 Share on other sites More sharing options...
Birdman203 Posted July 4, 2006 Author Share Posted July 4, 2006 Does any one know?> Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-52974 Share on other sites More sharing options...
.josh Posted July 4, 2006 Share Posted July 4, 2006 wildteen told you what was wrong with it. when you do a for loop, you count from a to b in numbers, just like real life. an array is not a number. your pregmatch turns $preparecount into an array. trying to make your for loop count from 0 to an array is as silly as trying to count from 0 to book. Book isn't a number. It's a word. now, wildteen's solution may not be what you were looking for, but i can see why he would have told you that. what you have implies that's what you want it to do. But that's why it [i]doesn't[/i] work. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-52977 Share on other sites More sharing options...
Birdman203 Posted July 4, 2006 Author Share Posted July 4, 2006 $prepareCount returns the number just like if you would do it with count. I did print_r($prepareCount); and it returned 2 not an array. $prepare is an array not $prepareCount. The problem is that $i++ isn't executing or somethin, because it always stays at 0. I inserted $i before <img and both were 0, but the 2nd 1 was supposed to be 1. That is why this is not workin and I do not know how to fix it that's what I'm asking for. If any of you know how to fix this. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53015 Share on other sites More sharing options...
wildteen88 Posted July 4, 2006 Share Posted July 4, 2006 Well the for loop is working, as I have tested it, although I striped out your code and used little bit code to echo the mataches preg_match_all has found, This is the code i used:[code=php:0]$message = "[[member:Daniel]] ta [[member:Steve]] tra [[member:Aaron]] la [[member:Maxwell]]";$prepareCount = preg_match_all("#\[\[member:(.*?)\]\]#si", $message, $prepare);for ($i = 0; $i < $prepareCount; $i++){ $alias = $prepare[1][$i]; echo $i . ' - ' . $alias . "<br />\n";}[/code]I assumed you was getting a memebers name of some kind. The result I get is the following:[code]0 - Daniel1 - Steve2 - Aaron3 - Maxwell[/code]Which backups up the for loop is working.What appears to be the problem is the following:[code]$message = preg_replace("/\[\[member:(.*?)\]\]/si", "<img src=http://global.typekill.net/flags/$row[country].gif> <a href=members.php?mode=profile&id=$row[member_id]>$row[alias]</a>", $message);[/code]All the other code is you have is fine. The only thing I can recommend is to wrap your variables in curly braces ({}), for example: {$row['country']} Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53029 Share on other sites More sharing options...
Birdman203 Posted July 4, 2006 Author Share Posted July 4, 2006 Ok so this is what I did to test it:[code]<?phprequire('required.inc.php');$message = "[[member:Birdman]]<br>sada<br>[[member:ZOiD]]";echo member($message);function member($message) { $prepareCount = preg_match_all("#\[\[member:(.*?)\]\]#si", $message, $prepare); for ($i = 0; $i < $prepareCount; $i++) { $alias = $prepare[1][$i]; $result = mysql_db_query("typekill_global", "select * from cp_members where alias = '$alias'"); $row = mysql_fetch_array($result); $message = preg_replace("/\[\[\member:(.*?)\]\]/si", "<img src=http://global.typekill.net/flags/{$row[country]}.gif> <a href=members.php?mode=profile&id={$row[member_id]}>{$row[alias]}</a>", $message); } return($message);}?>[/code]And this is what I got:[code]<img src=http://global.typekill.net/flags/55.gif> <a href=members.php?mode=profile&id=1>Birdman</a><br>sada<br><img src=http://global.typekill.net/flags/55.gif> <a href=members.php?mode=profile&id=1>Birdman</a>[/code]But when you do print_r($prepare); everything is in the array the right way and stuff so I don't know whats wrong. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53045 Share on other sites More sharing options...
Birdman203 Posted July 5, 2006 Author Share Posted July 5, 2006 Just bumping the topic. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53254 Share on other sites More sharing options...
wildteen88 Posted July 5, 2006 Share Posted July 5, 2006 Change this:[code]$message = preg_replace("/\[\[\member:(.*?)\]\]/si"[/code]to the following:[code]$message = preg_replace("/\[\[\member:{$alias}\]\]/si"[/code]The problem was with the (.*?) as its was always getting the first member from the $message variable, Now that you chnage (.*?) with the $alias variable it gets all the correct data! Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53259 Share on other sites More sharing options...
Birdman203 Posted July 5, 2006 Author Share Posted July 5, 2006 Nice, thank you it works now. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53297 Share on other sites More sharing options...
Birdman203 Posted July 5, 2006 Author Share Posted July 5, 2006 OK, now I have another question. Didn't know if I should make a new topic or not.Lets say I have this array:[code]Array (2)0 => Array (11) id => "18", post_num => "1", message => "news post #7<br />\r\n<img src=http://g..."1 => Array (11) id => "19", post_num => "2", message => "gagaga"[/code]And now I need to get the "message" from it where the post_num is 1. Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53314 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2006 Share Posted July 5, 2006 Use:[code]<?phpfor($i=0;$i<count($array);$i++) if ($array[$i]['post_num'] == 1) echo $array[$i]['message'];?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53364 Share on other sites More sharing options...
Birdman203 Posted July 5, 2006 Author Share Posted July 5, 2006 Can you tell me whats wrong with this?[code]<?php function quote($message) { $quoteCount = preg_match_all("#\#(.*?)#si", $message, $quote2); for ($i = 0; $i < $quoteCount; $i++) { if (is_numeric($quote2[1][$i])) { $quote = $quote2[1][$i]; $result = mysql_db_query("typekill_$config[short]", "select * from cp_fposts where topic_id = '$_GET[id]' order by id asc"); $n = 1; $b = 0; while ($row = mysql_fetch_array($result)) { $array[$b] = array( 'id' => $qry[id], 'post_num' => $n, 'author' => $row[author], 'message' => $row[message] ); $n++; $b++; } for ($d = 0; $d < count($array); $d++) { if ($array[$d]['post_num'] == $quote) { $author = mysql_db_query("typekill_global", "select * from cp_members where member_id = '$array[$d][author]'"); $qry = mysql_fetch_array($author); $message = preg_replace("/\#{$quote}/si", '<table cellspacing=\"1\" cellpadding=\"2\" bgcolor=\"#A9A8AE\" width=\"85%\" style=\"margin: 4px auto 8px auto;\"><tr style=\"background-image: url(images/oddrowbg.gif);\"><th style=\"padding-left: 5px; text-align: left;\"><b>#{$quote} by <a href=members.php?mode=profile&id={$qry[member_id]}>{$qry[alias]}</a></b></th></tr><tr bgcolor=\"{$config.color}\"><td style=\"padding-left: 6px;\" valign=\"top\">{$array[$d][message]}</td></tr></table>', $message); } } } } return($message); }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13554-help-with-a-code/#findComment-53619 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.