Jump to content

Help with a code.


Birdman203

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]
Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
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']}
Link to comment
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.
Link to comment
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!
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.