Why won't database query repeat?

I’m trying to get the list of all of the ordered where the company name is similar to Ama and the order total (code2) was $29.99 but whenever I try it gives the same order over and over again, but there should be around 20 different ones.

This is what I’ve got at the moment:

<?php
  # includes
  require_once '../db.inc';

  $query="select * from int_orders where company LIKE '%Ama%' and cost2 = 29.99";
  $result=dbcurrent( $query,"IntDb" );
  if( is_array( $result ) ) {
    foreach( $result as $key=>$value ) {
      $int_orders['id']=$value['id'];
      $int_orders['date']=$value['date'];
      $int_orders['company']=$value['company'];
      $int_orders['customer_id']=$value['customer_id'];
      $int_orders['address_billing']=$value['address_billing'];
      $int_orders['shipping_address']=$value['shipping_address'];
      $int_orders['tracking']=$value['tracking'];
      $int_orders['shipping_id']=$value['shipping_id'];
      $int_orders['shipping_date']=$value['shipping_date'];
      $int_orders['cost']=$value['cost'];
      $total_shipping=$value['shipping_price'];
      $total_discount=$value['cost2'];
                $ctr++;
    } }

  $query="select company_address.id,name_company,name_first,name_last,street,address2,town,postcode,shipping_country.name as country,tel1,tel2,email from company_address,shipping_country where shipping_country.id=company_address.country and company_address.id=".$int_orders['shipping_address'];
  $shipping_address=dbcurrent( $query,"IntDb" );
            if( is_array( $result ) ) {
              foreach( $result as $key=>$value ) {
                $shipping_address['name_first']=$value['name_first'];
                $shipping_address['name_last']=$value['name_last'];
                $shipping_address['postcode']=$value['postcode'];
                $shipping_address['email']=$value['email'];
                $ctr++;
    } }
	
  $query="select * from shipping";
  $result=dbcurrent( $query,"IntDb" );
  if( is_array( $result ) ) {
    foreach( $result as $key=>$value ) {
      $shipping[$value['id']]=$value['name'];
      $tracker[$value['id']]=$value['tracker'];
                $ctr++;
    } }

?>
          <?php if( is_array( $int_orders ) ) { foreach( $int_orders as $key=>$value ) { ?>
Name: <?php echo $shipping_address[0]['name_first']." ".$shipping_address[0]['name_last'] ?><br>
Postcode: <?php echo $shipping_address[0]['postcode'] ?></td><br>
E-mail: <?php echo "<a href=\"mailto:".$shipping_address[0]['email']."\">".$shipping_address[0]['email']."</a>" ?><br>
Order Number: <?php echo date( $int_orders['id'] ) ?><br>
Order Date: <?php echo date( "d/m/y",$int_orders['date'] ) ?><br>
Shipping Date: <?php echo date( "d/m/y H:i:s",$int_orders['shipping_date'] ); ?><br>
Shipping Method: <?php echo $shipping[$int_orders['shipping_id']]; ?><br>
Tracking Reference: <?php echo $int_orders['tracking']; ?><br>
Voucher Used: <?php echo $int_orders['voucher_code'] ?>
<?php } } ?>

In your first foreach, you keep overwriting the lone $int_orders array. You should be assigning those values to a distinct array item then appending that item to your $int_orders array:

foreach( $result as $key => $value) {
    $order = array();
    $order['id'] = $value['id'];
    $order['date'] = $value['date'];
    ...
    $int_orders[] = $order;
}

Sorry I don’t understand what you mean

In the following code (the first set of instructions from your code above), you are performing your initial query. As you loop over the result set, instead of building a list of orders, you are just overwriting the same item named $int_orders:

$query="select * from int_orders where company LIKE '%Ama%' and cost2 = 29.99";
$result=dbcurrent( $query,"IntDb" );
if( is_array( $result ) ) {
  foreach( $result as $key=>$value ) {
  $int_orders['id']=$value['id'];
  $int_orders['date']=$value['date'];
  $int_orders['company']=$value['company'];
  $int_orders['customer_id']=$value['customer_id'];
  $int_orders['address_billing']=$value['address_billing'];
  $int_orders['shipping_address']=$value['shipping_address'];
  $int_orders['tracking']=$value['tracking'];
  $int_orders['shipping_id']=$value['shipping_id'];
  $int_orders['shipping_date']=$value['shipping_date'];
  $int_orders['cost']=$value['cost'];
  $total_shipping=$value['shipping_price'];
  $total_discount=$value['cost2'];
            $ctr++;
} }

To see this, add the following to end of this section of code:

print_r($int_orders);

Instead of seeing multiple orders as entries in your $int_orders array, $int_orders will contain the information for only the last entry in your result set.

To remedy this, as you loop through the result set, you can build each entry in the result set as it’s own array, then append that newly created array to your list of orders:

$int_orders = array();

$query="select * from int_orders where company LIKE '%Ama%' and cost2 = 29.99";
$result=dbcurrent( $query,"IntDb" );
if( is_array( $result ) ) {
  foreach( $result as $key=>$value ) {
  
  $order = array();
  
  $order['id'] = $value['id'];
  $order['date'] = $value['date'];
  $order['company'] = $value['company'];
  $order['customer_id'] = $value['customer_id'];
  $order['address_billing'] = $value['address_billing'];
  $order['shipping_address'] = $value['shipping_address'];
  $order['tracking'] = $value['tracking'];
  $order['shipping_id'] = $value['shipping_id'];
  $order['shipping_date'] = $value['shipping_date'];
  $order['cost'] = $value['cost'];
  
  // This will add the newly created array to the list of orders
  $int_orders[] = $order;
  
  // I'm not sure what you are hoping to do with these items...
  $total_shipping=$value['shipping_price'];
  $total_discount=$value['cost2'];
  $ctr++;
} }

If you make this change and perform the print_r again, you’ll see that you have a multi-dimensional array of orders that you can then loop over as you are trying to later in your script.

Edit: It looks like you’ll have the same situation later in your code when you are building the shipping information, but you should be able to remedy that process the same way.

It’s worth noting at this stage also that nothing is happening with these array elements other than being assigned with the same keys. Consequently, the loop could be shortened to:

if( is_array( $result ) ) {
    $int_orders = $result;
    $ctr  += count($result);

    // $total_shipping=$value['shipping_price'];  // I suspect these two lines are not
    // $total_discount=$value['cost2'];           // achieving their purpose currently.
}

This is based on the assumption that the $result array is a zero-indexed array of matched rows, which seems likely.

Ahh I see what you’re saying thank you

Hmm I think you’re right there :worried:

Are you using PDO or the mysqli_* extension to access the database (which would show in the dbcurrent() function? Both of them have got functions which will grab the entire result set in one hit, saving the overhead of the foreach loops.

btw @coding_noobie if you’re not already aware, it might not affect you but just so that you’re are aware the old mysql_* extension was deprecated in version 5.5 of PHP and is being removed from the next version of PHP (version 7)

It’s MySQL and I wasn’t aware of that but will make sure we’re ready when we do eventually upgrade to 7

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.