STR Replace with a function?

Discussion in 'PHP' started by Agent_Smith, May 29, 2008.

  1. #1
    Hello,

    Im trying to replace a random statement in my html file with a function.

    HTML:

    					<tr>
    						<td align="right">Category: </td>
    						<td>%{cat}%</td>
    					</tr>
    HTML:
    
    
    function Category()
    {
        echo '<select name="category">';
        $query = "SELECT * FROM {$db_prfix}multi_cat";
        $result = mysql_query($query) or die(mysql_error());
    
        while ($row = mysql_fetch_array($result)) {
            if ($row['status'] == '1') {
                echo '
                <option value=' . $row['id'] . '>' . $row['name'] . '</option>
                ';
            }
    
        }
        echo '</select>';
    }
    
    $content_get = file_get_contents("multi_templates/signup.html");
    $content = str_replace('%{cat}%',Category(),$content_get);
    echo $content;
    PHP:
    It's getting the drop down box... just randomly dumping it above everything else in the html... any ideas?

    Thanks
     
    Agent_Smith, May 29, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    This should work:
    <?php
    function Category()
    {
        $return = '<select name="category">';
        $query = "SELECT * FROM {$db_prfix}multi_cat";
        $result = mysql_query($query) or die(mysql_error());
    
        while ($row = mysql_fetch_array($result)) {
            if ($row['status'] == '1') {
                $return .= '
                <option value=' . $row['id'] . '>' . $row['name'] . '</option>
                ';
            }
    
        }
        return $return . '</select>';
    }
    
    $content_get = file_get_contents("multi_templates/signup.html");
    $content = str_replace('%{cat}%',Category(),$content_get);
    echo $content;
    ?>
    
    PHP:
    Rep appreciated :D (If it works for you, that is :))

    Jay
     
    jayshah, May 29, 2008 IP
  3. Agent_Smith

    Agent_Smith Well-Known Member

    Messages:
    890
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    145
    #3
    I fixed it. Don't know why but i just used the <td> in the function, not html.

    Strange but yeah.

    Thanks ^

    BTW, You must spread some Reputation around before giving it to jayshah again. ;)
     
    Agent_Smith, May 30, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Thanks anyway :)

    So, spread some rep and come back :D
     
    jayshah, May 30, 2008 IP