Jump to content

method not executing on second cylce of foreach?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
I have a foreach loop that runs for every row returned from a database. Inside of this foreach loop i call a method that turns bb code and ASCII for smilies into HTML. It works on the first cycle throu the loop but stops working after that. I've been trouble shooting this for around an hour but i really need a fresh pair of eyes. I don't know if the problem is in syntex or if (as the case most likely is) I don't understand somthing about OOP and i am doing it wrong.

any way here is the loop:
<?
foreach($db->getBySpecId("news_comments", $_REQUEST[n_id], "article_id", "Desc") as $comment)
 {
  //user asset data (sig, avatar, display name, & so on)
  $user = $db->getBySpecId2("users_assets", $comment[1], "user_id", "Desc");
?>
        <table class="responce">
            <tr>
                <td colspan="2">
                <br /><b> Re:<?php echo $news[3]; // Title of news article ?></b>
                </td>
            </tr>
            <tr>
                <td rowspan="3" class="responceUserBar">
                    <center>
                        <br />
                        <b><?php echo $user[4];  //Name of person leaving a coment ?></b>
                        <br />
                        <img src="<?php echo $user[3]; //user avatar ?>" border="0" />
                        <br /><br />
                        thumbs
                        <br />
                    </center>
                </td>
                <td class="responceBody">
                    <?php echo $string->bbToHtml($comment[4]); // users comment ?>
                </td>
            </tr>
            <tr>
                <td class="responceEditBar">
                    Edit | Delete
                </td>
            </tr>
            <tr>
                <td class="responceBody">
     <?php echo $user[2];//user signiture ?>
                </td>
            </tr>
        </table> 
<?php
 }

this is the line in the above code that is giving me trouble:
 <?php echo $string->bbToHtml($comment[4]); ?>

Here is the method being called:
 private function setBbToHtml($var)
 {
  include_once("config/bb_codeConfig.inc.php");
  //replace smileys
  $var2 = str_replace($smileyAscii,$smileyHtml, $var);
  //replace bb code
  $var3 = str_replace($bbCode,$htmlCode, $var2);
 
  return $var3;
 }
 public function bbToHtml($var)
 {
  $var2 = $this->setBbToHtml($var);
  return $var2;
 }

Here is a screen of what is happening (note that i am trying to use the same smiley on them so its not that i did the smiley wrong, i copy pasted it from the working post)
[ATTACH]2988[/ATTACH]


I would like to go to bed and have a go at it when i'm awake, but unfortunitly that is not an option so i am posting on here.

Attached Files


Posted Image

#2
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
Sorry for the double post but i have another question so i wanted this to show up as unread.

I fixed the original problem. The method was executing as it turns out. The problem was inside the method. Instead of using include_once() in that bottom code block I had to change it to include(). My qestion is why dosent include_once work in this situation?
Posted Image

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Well, my guess would be, that you create one instance of the object $string. The first time setBbToHtml is called, the file is loaded. This loaded file becomes part of the $string object. When setBbToHtml is called the second time, PHP checks to make sure the file is not already included, it sees that it is, and then does nothing (because it can only be included once). But that is just a guess, I will look into it more Wednesday after my exam. Also, during development, I'd recommend turning error reporting to the max (this should have thrown a Notice Undefined variable blah blah)

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);


#4
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
Thanks for the reply John,

I thought that sence the value of the strings that gets included (thats all thats in the include file is a set of string arrays) never changed that i would only need to include it one time. IE: if i had $var="dog"; I figured no matter how many times i use $var it would print out dog and not need redefined. I was obviously wrong.

The error handaling you put at the end of your post. Does that just go in my index page (which calls everything on my site) or is there a special file it has to go in? (like the php.ini that i don't know much about)

Good luck on your exam (asumming its the type of exam that is taken) And thanks for the help.
Posted Image