2009-12-07 63 views
1

有沒有一種方法可以讀取頂部的變量,它定義了底部的哪個位置。 下面是這種情況的例子: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; 
} 

我試圖用這個函數的返回值,但即使這樣似乎並沒有工作。

任何解決方案?

感謝

回答

4

不,在PHP中的所有指令順序執行,所以你無法讀取尚未定義的變量。您必須將代碼設置爲您調用logoPath()函數的地方以上的值$logo_path

或者,您可以將sql代碼放在logoPath()函數中,該函數允許您將函數返回到調用該函數的位置下方的徽標路徑。

1

沒有,但有一種方法使用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>"; 
} 

這是有點愚蠢,但它應該工作。

+0

是的,我已經這樣做使用js。謝謝大家 – Sarfraz 2009-12-07 10:47:58

-1

你可以做的是使用Div標籤,並在頁面頂部的位置。這樣代碼可以在按鈕上執行,但結果將顯示在頂部。