2013-12-22 74 views
0

我在客戶端的網站上使用for循環將購買的票證信息插入到數據庫中。循環工作正常,但客戶端請求在每次運行for循環時都將唯一標識符附加到第一個條目。此標識符將用於在打印票據時突出顯示主票證所有者。我已經將下面的循環包括在內以供參考。任何想法如何實現這一目標?謝謝。在PHP for循環中爲第一次迭代添加唯一標識符

$threepack = ''; 
$i = ''; 
for($i = 0; $i < $tickets; $i++) 
{ 
    $firstname = 'firstname'.$threepack; 
    $lastname = 'lastname'.$threepack; 
    $address = 'address'.$threepack; 
    $city = 'city'.$threepack; 
    $postal = 'postal'.$threepack; 
    $phone = 'phone'.$threepack; 
    $altphone = 'altphone'.$threepack; 
    $sec_firstname = 'sec_firstname'.$threepack; 
    $sec_lastname = 'sec_lastname'.$threepack; 
    $email = 'email'.$threepack; 

    $table->firstname = $data->$firstname; 
    $table->lastname = $data->$lastname; 
    $table->address = $data->$address; 
    $table->city = $data->$city; 
    $table->postal = $data->$postal; 
    $table->phone = $data->$phone; 
    $table->altphone = $data->$altphone; 
    $table->sec_firstname = $data->$sec_firstname; 
    $table->sec_lastname = $data->$sec_lastname; 
    $table->email = $data->$email; 
    $table->id = 0; 
    $table->order_total = $data->total; 
    $table->store(); 

    if($data->tickets == '-1') 
    { 
     if($threepack == 2) 
     { 
      $threepack = 3; 
     } else { 
      $threepack = 2; 
     } 
    } 

    // 8 Fields 
    if($data->tickets == '5') 
    { 
     if ($threepack == '') { 
      $threepack = 2; 
     } else { 
      $threepack += 1; 
     } 
    } 
} 
+0

如果$ i = 0,那麼你知道這是第一次運行 – Zarathuztra

+0

我不是100%肯定你的意思在這裏。你不能這樣做:if($ i == 0)$ table-> uniqueId = 4327849? –

+0

如果這一切都到數據庫你可以添加int id字段到數據庫讓它自動增量並使用mysql_insert_id()來獲取最後插入的ID,這樣你將有一個唯一的標識符 – talsibony

回答

0
for ($i = 0; $i < $tickets; $i++) { 
    // ... 

    if ($i == 0) 
    $table->highlightme = 1; 
    // ... 

    $table->store(); 

    // ... 
} 
+0

我曾嘗試將變量設置爲1並將其應用於名字字段,但它應用於數據庫中的多個條目。我認爲threepack代碼可能會導致問題。思考? –

相關問題