refresh using variable from mysql for content not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CCCMTech
Forum Newbie
Posts: 4
Joined: Wed Feb 04, 2004 12:39 pm

refresh using variable from mysql for content not working

Post 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"
}
}
?>
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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.

Code: Select all

<?php echo $refreshsec; ?>
instead of

Code: Select all

<? $refreshsec; ?>
Mac
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

I myself have had trouble adding linebreaks when I echo with single quotes..maybe it's just me?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post 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.
CCCMTech
Forum Newbie
Posts: 4
Joined: Wed Feb 04, 2004 12:39 pm

Post 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....
Post Reply