Page 1 of 1
refresh using variable from mysql for content not working
Posted: Thu Feb 05, 2004 6:48 pm
by CCCMTech
Like is stated I'm having problems using a variable I called from MySQL in a meta refresh statement. I have verified the variable is pulling using an echo statement, but the refresh won't accept it, if I hard code the content in the refresh it will work fine.
Code: Select all
<?php
$sql = "SELECT * FROM phpbb_forums
WHERE forum_id = $forum_id";
$result = $db->sql_query($sql);
while ( $row = $db->sql_fetchrow($result) )
{
$refresh = $row['refresh_forum'];
$refreshsec = $row['refresh_forum_sec'];
if ( $refresh == 1 ){
?>
<meta http-equiv="refresh" content="<? $refreshsec ?>"> <?php}
else
{
echo "not in loop"
}
}
?>
Posted: Thu Feb 05, 2004 6:49 pm
by Straterra
Try this
Code: Select all
<?php
$sql = "SELECT * FROM phpbb_forums
WHERE forum_id = $forum_id";
$result = $db->sql_query($sql);
while ( $row = $db->sql_fetchrow($result) )
{
$refresh = $row['refresh_forum'];
$refreshsec = $row['refresh_forum_sec'];
if ( $refresh == 1 ){
?>
<meta http-equiv="refresh" content="<? echo $refreshsec; ?>"> <?php}
else
{
echo "not in loop";
}
}
?>
Edit : It is better practice to stay in PHP and merely echo HTML. For an example, if you wanted to do this, you would use this code.
Code: Select all
<?php
$sql = "SELECT * FROM phpbb_forums
WHERE forum_id = $forum_id";
$result = $db->sql_query($sql);
while ( $row = $db->sql_fetchrow($result) )
{
$refresh = $row['refresh_forum'];
$refreshsec = $row['refresh_forum_sec'];
if ( $refresh == 1 ){
echo "<meta http-equiv="refresh" content="".$refreshsec."">";
}
else
{
echo "not in loop";
}
}
?>
Just remember, when you echo HTML, you much escape the quotation marks with \ slashes.
Posted: Fri Feb 06, 2004 3:01 am
by twigletmac
Sorry, Straterra but I've got to disagree with your advice.
Straterra wrote:Edit : It is better practice to stay in PHP and merely echo HTML. For an example, if you wanted to do this, you would use this code.
Not necessarily, it really depends on personal choice and need. In many respects the further your HTML is kept from your PHP the better.
Straterra wrote:Just remember, when you echo HTML, you much escape the quotation marks with \ slashes.
Well no, you don't have to it depends how you're echoing the code, personally I wouldn't bother with the hassle of doing:
Code: Select all
echo "<meta http-equiv="refresh" content="".$refreshsec."">";
IMHO, if you put it into single quotes it's easier to maintain and clearer to read:
Code: Select all
echo '<meta http-equiv="refresh" content="'.$refreshsec.'">';
but in heredoc (or HTML outside a PHP block like the original code) it's even easier. In heredoc format you would have something like this:
Code: Select all
echo <<<END
<meta http-equiv="refresh" content="$refreshsec">
END;
no quotes and no concenation, nice.
Mac
Posted: Fri Feb 06, 2004 3:03 am
by twigletmac
BTW, for the original problem I fully agree with Straterra

, you need to echo the variable to make it appear in the HTML.
instead of
Mac
Posted: Fri Feb 06, 2004 5:35 am
by Straterra
I myself have had trouble adding linebreaks when I echo with single quotes..maybe it's just me?
Posted: Fri Feb 06, 2004 5:41 am
by twigletmac
Straterra wrote:I myself have had trouble adding linebreaks when I echo with single quotes..maybe it's just me?
Use heredoc then - much better solution, makes looking after your HTML code a lot easier and saves a lot of variable escaping.
Mac
Posted: Fri Feb 06, 2004 5:51 am
by Straterra
Only reason I don't use heredoc is because I am often just echoing a single line, and it seems easier to just...echo it.
Posted: Fri Feb 06, 2004 9:29 am
by CCCMTech
Thanks for the excellent advice guys. I think I may have found a new Dev home
Now if only I can get more to help with complex MySQL statements (ie. using 3 tables a LEFT JOIN, ON, WHERE using multiple rows to output a formatted table).
Anyways, thanks again. I really like the heredoc format....