Jump to content

Archived

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

Birdman203

Help with a code.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
Share on other sites
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 - Daniel
1 - Steve
2 - Aaron
3 - 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']}

Share this post


Link to post
Share on other sites
Ok so this is what I did to test it:

[code]
<?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);
}
?>
[/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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Use:
[code]<?php
for($i=0;$i<count($array);$i++)
    if ($array[$i]['post_num'] == 1) echo $array[$i]['message'];
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.