Jump to content

STR Replace with a function?


a1amattyj

Recommended Posts

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>

PHP:


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_get1 = str_replace('%{cat}%',Category(),$content);
echo $content;

 

It's getting the drop down box... just randomly dumping it above everything else in the html... any ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/107844-str-replace-with-a-function/
Share on other sites

remove the echo statemtns in your function. Use return instead maybe:

function Category()
{
    $html = '<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') {
            $html .= '
            <option value=' . $row['id'] . '>' . $row['name'] . '</option>
            ';
        }

    }

    $html .= '</select>';

    return $html;
}

$content = file_get_contents("multi_templates/signup.html");
$content = str_replace('%{cat}%', Category(), $content);

echo $content;

If the menu is still going above your table then there must be a bug in the generated HTML.

Archived

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

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