在PHP中运行循环时检查是否最后一行

时间:2014-03-06 20:44:39

标签: php

我正在使用此PHP代码来运行MySQL数据库的查询:

$i=0;
$display='[';
$stmt = $pdo_conn->prepare("SELECT * from tickets where status = :status and deleted = :deleted ");
$stmt->execute(array(':status' => 'Open', ':deleted' => ''));
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
$counter = count($records);
foreach($records as $result) {
    $i++;
    $stmt = $pdo_conn->prepare("SELECT * from contacts where sequence = :sequence ");
    $stmt->execute(array(':sequence' => $result["contact"]));
    $contact = $stmt->fetch();

    $display.='{';
    $display.='"customer":"'.$contact["forename"].' '.$contact["surname"].'",';
    $display.='"subject":"'.$result["subject"].'"';
    if($counter == $i) {
        $display.='},';
    } else {
        $display.='}';
    }
}
$display.=']';

我希望最终结果如下所示:

[{"customer":"Carol","subject":"Fax not working"}{"customer":"Clive","subject":"VoIP Issues"}{"customer":"Leigh ","subject":"company Antaeus"}{"customer":"Debbie","subject":"emails"}{"customer":"Kim","subject":"Printer setup"}{"customer":"Sue ","subject":"Phone"}{"customer":"Sandra","subject":"Debbie's computer "}{"customer":"Daniel","subject":"Email Attachments"}{"customer":"Lara","subject":"Internet Issues"}]

然而,目前它看起来像:

[{"customer":"Carol","subject":"Fax not working"}{"customer":"Clive","subject":"VoIP Issues"}{"customer":"Leigh ","subject":"company Antaeus"}{"customer":"Debbie","subject":"emails"}{"customer":"Kim","subject":"Printer setup"}{"customer":"Sue ","subject":"Phone"}{"customer":"Sandra","subject":"Debbie's computer "}{"customer":"Daniel","subject":"Email Attachments"}{"customer":"Lara","subject":"Internet Issues"},]

请注意]

之前的逗号

我如何确保逗号最后没有显示,我尝试在我的循环中使用if语句与计数器($counter$i),但这不起作用

1 个答案:

答案 0 :(得分:3)

您可以在没有任何循环和单个内部联接的情况下执行此操作。建议您使用json_encode()为您完成,而不是手动构建JSON字符串。运行单个连接查询,并将行收集到一个编码为JSON的数组中。

这消除了对所有其他机器的需要 - 循环和计数器。

// A single join query will return everything you need from both tables.
// Customer names can be concatenated here into a single Customer field
// You only appear to need tickets.subject.
// And reading your queries, the table relation appears to be tickets.contact = contacts.sequence
$sql = '
SELECT
  t.subject AS Subject,
  CONCAT(c.forename, ' ', c.surname) AS Customer
FROM 
  tickets t 
  INNER JOIN contacts c ON t.contact = c.sequence
WHERE status = :status AND deleted = :deleted;
';
$stmt = $pdo_conn->prepare($sql);
if ($stmt) {
  $stmt->execute(array(':status' => 'Open', ':deleted' => ''));
  // Fetch all rows...
  $rows = $stmt->fetchAll();

  // Now $rows looks like the 2D array needed, you can directly JSON encode it
  $output = json_encode($rows);
}

// Inspect it...
echo $output;

我会指出尽管你习惯于绑定参数是完全正确的,但实际上并不需要它们,因为字符串'Open'''是静态的,众所周知。您可以在此处调用普通query()而不是prepare()/execute()

// No user input, static strings only, means no need for params
$sql = "
SELECT
  t.subject AS Subject,
  CONCAT(c.forename, ' ', c.surname) AS Customer
FROM 
  tickets t 
  INNER JOIN contacts c ON t.contact = c.sequence
WHERE status = 'Open' AND deleted = '';
";
// Just a simple query() call
$result = $pdo_conn->query($sql);
if ($result) {
  $rows = $result->fetchAll();
  // etc...
}