Jump to content

Get only first element of array from SQL fetch array


imprint

Recommended Posts

I have the following code which prints results from a sqlsrv_fetch_array() using a while loop. For one segment, however, I only need the first $value of the $key=>$value pair, and obviously with the code written this way it returns all rows.

 

I have an idea of an approach, and I'm wondering if I'm headed in the right direction or if anyone has experience with this that they can share. So, during the while loop, I am pushing the elements of the sqlsrv_fetch_array() into a separate array.

 

So I see two potential options: 

 

1. Print from the $actcontacts[] array rather than from the sqlsrv fetch array.

 

2. During the while loop, rather than pushing anything into a new array, build a conditional that says to only print the record if $row['ENDTIME'] is in position $row[0].

 

I'm leaning toward #2, but I'm not quite sure how to get there...and if there is a better way under door #3, I would like to try that.

 

while ($row = sqlsrv_fetch_array( $stmt_act, SQLSRV_FETCH_ASSOC) ) {
    $actcontacts[] = "N/A" . "," . $row['COMPANYNAME'] . "," . $row['CITY'] . "," . $row['STATE'] . "," . $row['CUST_Salesman_124705300'] . "," . $row['ENDTIME'] . "," . "ACT";
    // I have done a print_r() on $actcontacts and the data is correct.
    $endtime[] = $row['ENDTIME'];
    echo "<tr>";
    echo "<td>" . "<i>N/A</i>" . "</td>";
    echo "<td>" . $row['COMPANYNAME'] . "</td>";
    echo "<td>" . $row['CITY'] . "</td>";
    echo "<td>" . $row['STATE'] . "</td>";
    echo "<td>" . $row['CUST_Salesman_124705300'] . "</td>";
    echo "<td>" . $row['ENDTIME'] . "</td>"; // This is the bugger that is printing eleventy records. It may be one customer, but there are always multiple contact/history entries.
    echo "<td>" . "ACT" . "</td>";
    echo "</tr>";
}
 
Link to comment
Share on other sites

So you want the value of $row['ENDTIME']  to be output only once for the first row of the returned result set and not for the 2nd, 3rd, 4th etc...rows? Then what you need is an if at the start of the loop

while ($row = sqlsrv_fetch_array( $stmt_act, SQLSRV_FETCH_ASSOC) ) {

    $endtime = !isset($endtime) ? $row['ENDTIME'] : '';

    $actcontacts[] = "N/A" . "," . $row['COMPANYNAME'] . "," . $row['CITY'] . "," . $row['STATE'] . "," . $row['CUST_Salesman_124705300'] . "," . $endtime . "," . "ACT";

    echo "<tr>";
    echo "<td>" . "<i>N/A</i>" . "</td>";
    echo "<td>" . $row['COMPANYNAME'] . "</td>";
    echo "<td>" . $row['CITY'] . "</td>";
    echo "<td>" . $row['STATE'] . "</td>";
    echo "<td>" . $row['CUST_Salesman_124705300'] . "</td>";
    echo "<td>" . $endtime . "</td>"; // should only be output for the first row
    echo "<td>" . "ACT" . "</td>";
    echo "</tr>";
}
Link to comment
Share on other sites

Ch0cu3r, that is what I was thinking and was trying to see if I was on the right path, thanks!

 

cyberRobot, DOH! I can't believe I didn't think of that. So simple and will speed things up as well.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.