Jump to content


Photo

Help with a code.


  • Please log in to reply
14 replies to this topic

#1 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 03 July 2006 - 03:20 PM

I don't know why, but this doesn't work. the $i always stays at 0.

$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);
}


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 July 2006 - 03:33 PM

Becuase you havnt told the for loop how many times you want it to loop. $prepareCount doesnt return an integer but an array 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:
for ($i = 0; $i < count($prepareCount)-1; $i++) {
Notice -1 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.

#3 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 03 July 2006 - 04:05 PM

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.

#4 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 03 July 2006 - 08:05 PM

Does anyone know what can be wrong with it?

#5 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 04 July 2006 - 06:09 PM

Does any one know?>

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 04 July 2006 - 06:15 PM

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 doesn't work.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 04 July 2006 - 07:46 PM

$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.

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 July 2006 - 08:39 PM

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:
$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";
}
I assumed you was getting a memebers name of some kind. The result I get is the following:
0 - Daniel
1 - Steve
2 - Aaron
3 - Maxwell
Which backups up the for loop is working.

What appears to be the problem is the following:
$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);

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']}

#9 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 04 July 2006 - 09:50 PM

Ok so this is what I did to test it:

<?php

require('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);
}
?>

And this is what I got:
<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>

But when you do print_r($prepare); everything is in the array the right way and stuff so I don't know whats wrong.

#10 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 05 July 2006 - 10:33 AM

Just bumping the topic.

#11 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 05 July 2006 - 10:51 AM

Change this:
$message = preg_replace("/\[\[\member:(.*?)\]\]/si"
to the following:
$message = preg_replace("/\[\[\member:{$alias}\]\]/si"
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!

#12 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 05 July 2006 - 12:44 PM

Nice, thank you it works now.

#13 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 05 July 2006 - 01:03 PM

OK, now I have another question. Didn't know if I should make a new topic or not.

Lets say I have this array:

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"

And now I need to get the "message" from it where the post_num is 1.


#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 05 July 2006 - 02:58 PM

Use:
<?php
for($i=0;$i<count($array);$i++)
     if ($array[$i]['post_num'] == 1) echo $array[$i]['message'];
?>

Ken

#15 Birdman203

Birdman203
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 05 July 2006 - 10:43 PM

Can you tell me whats wrong with this?

<?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);
	}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users