PHP table display for multidimensional array error

Viewed 49

I am trying to display the output of this query:

  $resultReturn = $con->prepare( "SELECT `returns`.`return_id`, `returns`.`return_ZDTicket`,  status.status_description, orders.customer_email\n"

    . "FROM `returns` \n"

    . " LEFT JOIN `status` ON `returns`.`return_status` = `status`.`status_id`\n"

    . "    LEFT JOIN `agents` ON `returns`.`agent_id` = `agents`.`id`\n"

    . "    LEFT JOIN `orders` ON `returns`.`return_orderID` = `orders`.`order_id`\n"

    . "WHERE `agents`.`id` = ? ");


$resultReturn->bind_param('i', $id);
$resultReturn->execute();
$resultReturn->store_result();
$resultReturn->bind_result($returnID, $returnZD, $returnStatus,$returnCustomerEmail);


$arrayReturnID[] = array();
$arrayreturnZD[] = array();
$arrayreturnStatus[] = array();
$arrayreturnCustomerEmail[] = array();

while ($resultReturn->fetch()){
    array_push($arrayReturnID,$returnID);
    array_push($arrayreturnZD,$returnZD);
    array_push($arrayreturnStatus,$returnStatus);
    array_push($arrayreturnCustomerEmail,$returnCustomerEmail);

}

array_splice($arrayReturnID, 0, 1);
array_splice($arrayreturnZD, 0, 1);
array_splice($arrayreturnStatus, 0, 1);
array_splice($arrayreturnCustomerEmail, 0, 1);

$returnsTable = array($arrayReturnID,$arrayreturnZD,$arrayreturnStatus,$arrayreturnCustomerEmail);

if we print the $returnsTable this is the result:

Array ( [0] => Array ( [0] => 2 ) [1] => Array ( [0] => 268903 ) [2] => Array ( [0] => OPEN ) [3] => Array ( [0] => mariorossi@testnetatmo.com ) )

This is my table:

          <tbody>
          <tr>
              <th>Return ID</th>
              <th>ZD Ticket</th>
              <th>Return Status</th>
              <th>Customer Email</th>
          </tr>
              <?php  ?>
          <tr>
          </tr>
              <?php ?>
          </tbody>
          </table>
      </div>

Can you please help me display the table? It is driving me crazy, php is not my thing! :( Thankyou!

2 Answers

I would ditch the 4 array_splice calls and instead take just the first element of each array:

$returnsTable = array($arrayReturnID[0],$arrayreturnZD[0],$arrayreturnStatus[0],$arrayreturnCustomerEmail[0]);

This will make $returnsTable a simple array that you can loop through with a simple foreach:

          <tbody>
          <tr>
              <th>Return ID</th>
              <th>ZD Ticket</th>
              <th>Return Status</th>
              <th>Customer Email</th>
          </tr>
          <tr>
              <?php 
              foreach($returnsTable as $item){
                  echo "<td>".$item."</td>";
              }
              ?>
          </tr>
          </tbody>
          </table>
      </div>

Assuming that, as per your question: "I am trying to display the output of this query".

...and that you want the full content of the query returned (otherwise you should narrow down your query). You don't need to keep moving data with array functions just output it.

Currently your code doesn't add any HTML tags to show output...

<?

 $resultReturn = $con->prepare("
    SELECT 
        `returns`.`return_id`, `returns`.`return_ZDTicket`,
        `status`.`status_description`, `orders`.`customer_email`
    FROM `returns`
        LEFT JOIN `status` ON `returns`.`return_status` = `status`.`status_id`
        LEFT JOIN `agents` ON `returns`.`agent_id` = `agents`.`id`
        LEFT JOIN `orders` ON `returns`.`return_orderID` = `orders`.`order_id`
    WHERE `agents`.`id` = ? ");


$resultReturn->bind_param('i', $id);
$resultReturn->execute();
$resultReturn->store_result();
$resultReturn->bind_result($returnID, $returnZD, $returnStatus,$returnCustomerEmail);

$returnTable = "";                                     // Initialise output variable
while ($resultReturn->fetch()){                        // Loop through results
    $returnTable .= "<tr>";                            // Start a new table row
    $returnTable .= "<td>{$returnID}</td>";            // Return table data in a cell
    $returnTable .= "<td>{$returnZD}</td>";            // ^^
    $returnTable .= "<td>{$returnStatus}</td>";        // ^^
    $returnTable .= "<td>{$returnCustomerEmail}</td>"; // ^^
    $returnTable .= "</tr>";                           // End table row
}

?>

          <tbody>
          <tr>
              <th>Return ID</th>
              <th>ZD Ticket</th>
              <th>Return Status</th>
              <th>Customer Email</th>
          </tr>
              <?php  echo $returnTable; ?>              // Print table content to page
          </tbody>
          </table>
      </div>
Related