PHP:读取底部顶部的变量
问题描述:
有没有一种方法可以读取顶部的变量,它定义了底部的哪个位置。 下面是这种情况的例子:PHP:读取底部顶部的变量
<!--this image is somewhere on top of the page-->
<img src="/<?php print logoPath(); ?>" border="0" />
// this code is somewhere on bottom of the page
$logo_query = "select logo_path from table where id = $row->user_id";
$res_logo = mysql_query($logo_query) or die(mysql_error());
$row_logo = mysql_fetch_object($res_logo);
$logo_path = $row_logo->logo_path;
function logoPath()
{
global $logo_path;
return $logo_path;
}
我试图用这个函数的返回值,但即使这样似乎并没有工作。
任何解决方案?
感谢
答
不,在PHP中的所有指令顺序执行,所以你无法读取尚未定义的变量。您必须将代码设置为您调用logoPath()函数的地方以上的值$logo_path
。
或者,您可以将sql代码放在logoPath()函数中,该函数允许您将函数返回到调用该函数的位置下方的徽标路径。
答
没有,但有一种方法使用jQuery做到这一点:
<img id="image" src="" border="0" />
// this code is somewhere on bottom of the page
$logo_query = "select logo_path from table where id = $row->user_id";
$res_logo = mysql_query($logo_query) or die(mysql_error());
$row_logo = mysql_fetch_object($res_logo);
$logo_path = $row_logo->logo_path;
function logoPath()
{
global $logo_path;
echo "<script type='text/javascript'> $('#image').src('".$logo_path."');</script>";
}
这是有点愚蠢,但它应该工作。
答
你可以做的是使用Div标签,并在页面顶部的位置。这样代码可以在按钮上执行,但结果将显示在顶部。
是的,我已经这样做使用js。谢谢大家 – Sarfraz 2009-12-07 10:47:58