2010-03-28 146 views
-1

我承認我是一名新手程序員,而且我唯一的經驗就是傳統的ASP。我正在尋找一種方法將此asp代碼轉換爲PHP。對於只能訪問linux系統的客戶,也可以作爲學習工具。將此代碼從ASP轉換爲PHP

在此先感謝您的幫助:

記錄和功能:

Function pd(n, totalDigits) 
     if totalDigits > len(n) then 
      pd = String(totalDigits-len(n),"0") & n 
     else 
      pd = n 
     end if 
End Function 


'declare the variables 
Dim Connection 
Dim Recordset 
Dim SQL 
Dim SQLDate 

SQLDate = Year(Date)& "-" & pd(Month(Date()),2)& "-" & pd(Day(Date()),2) 

'declare the SQL statement that will query the database 
SQL = "SELECT * FROM tblXYZ WHERE element_8 = 2 AND element_9 > '" & SQLDate &"'" 

'create an instance of the ADO connection and recordset objects 
Set Connection = Server.CreateObject("ADODB.Connection") 
Set Recordset = Server.CreateObject("ADODB.Recordset") 

'open the connection to the database 
Connection.Open "PROVIDER=MSDASQL;DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;UID=xxxxx;PWD=xxxxx;database=xxxxx;Option=3;" 

'Open the recordset object executing the SQL statement and return records 
Recordset.Open SQL,Connection 

顯示頁/循環:

Dim counter 
counter = 0 

    While Not Recordset.EOF 
     counter = counter + 1 

     response.write("<div><td width='200' valign='top' align='center'><a href='" & Recordset("element_6") & "' style='text-decoration: none;'><div id='ad_header'>" & Recordset("element_3") & "</div><div id='store_name' valign='bottom'>" & Recordset("element_5") & "</div><img id='photo-small-img' src='http://xyz.com/files/" & Recordset("element_7") & "' /><br /><div id='ad_details'>"& Recordset("element_4") & "</div></a></td></div>") 
     Recordset.MoveNext 
     If counter = 3 Then 
      Response.Write("</tr><tr>") 
      counter = 0 
     End If 
    Wend 
+2

到目前爲止,你有什麼,和它怎麼不工作? – 2010-03-28 15:43:53

回答

1

這將是這個樣子......

$sqldate = strftime('Y-m-d',time()); // time defaults to now - change unix time to string 
$sql = "SELECT * FROM tblXYZ WHERE element_8 = 2 AND element_9 '$date'"; 

$pdoCon = new PDO($connString, $username, $password); 

$outputTpl = '<div><td width="200" valign="top" align="center"><a href="%s" style="text-decoration: none;"><div id="ad_header">%s</div><div id="store_name" valign="bottom">" %s</div><img id="photo-small-img" src="http://xyz.com/files/%s" /><br /><div id="ad_details">%s</div></a></td></div>'; 

$count = 0; 
foreach($pdoCon->query($sql) as $row) { 
    $counter++; 
    echo sprintf(
     $outputTpl, 
     $row['element_6'], 
     $row['element_3'], 
     $row['element_5'], 
     'http://xyz.com/files/' . $row['element_7'], 
     $row['element_4'] 
); 

    if($counter == 3) { 
    echo '<tr></tr>'; 
    $counter = 0; 
    } 
} 
+0

謝謝,這正是我正在尋找的。不幸的是,它給了我一個500內部服務器錯誤。 – jethomas 2010-03-29 22:11:28

+0

我會猜測你沒有MSSQL的PDO dirver(我認爲它的實驗)或者PDO。在你自己的年紀,因爲我不使用MSSQl,不知道。這只是一個簡單的例子,主要讓你比較文本的循環和輸出 - 你也需要研究特定的數據庫方面。我會建議在PHP中發佈一個關於MSSQL連接的新問題。然後從那裏出發。 – prodigitalson 2010-03-30 01:50:41

+0

謝謝!我會仔細看看的! – jethomas 2010-03-30 11:21:22