在PHP for循环中为第一次迭代添加唯一标识符

时间:2013-12-22 02:03:15

标签: php mysql

我在客户的网站上使用for循环将购买的票证信息插入数据库。循环正常工作,但客户端已请求选项在每次运行for循环时将唯一标识符附加到第一个条目。打印票证时,此标识符将用于突出显示主票证所有者。我在下面列出了当前的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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

  $table->store();

  // ...
}