John Hesch

Echoing my thoughts and interests

Update:

Never mind, I finally figured it out. I was referencing the function Get() in the GetList() function and Get() was not searching by ItemID.


I have posted this problem on several PHP topic forums and have been working on it for several days and I cannot get it to work properly. You're my last hope.

I have the following function written in PHP

PHP:
  1.  
  2. function GetList($Value)
  3. {
  4.     $order_items_List = Array();
  5.     $Database = new DatabaseConnection();
  6.     $query = "select * from order_items where (IOrderID = $Value)";
  7. //        echo $query;
  8.     $Database->Query($query);
  9.     for ($i=0; $i < $Database->Rows(); $i++)
  10.         {
  11.             $order_items = new order_items();
  12.             $order_items->Get($Database->Result($i,"IOrderID"));
  13.             $order_items_List[] = $order_items;
  14.         }
  15.         return $order_items_List;
  16.     }

There are two records in the database. They each have a unique ItemID but the same IOrderID. When I try to display the results of the query in the above function I get two instances of the first record and not one instance of both records.

If I echo the query in MySQL I return both records as expected.

SQL:
  1. SELECT * FROM order_items WHERE (IOrderID = '10')

Here is what I'm using to display the results

PHP:
  1.  
  2.  $items = new order_items();
  3.  $itemslist = $items->GetList(10);
  4.     foreach ($itemslist as $items)
  5.     {
  6. echo "<tr>";
  7. echo "<td class=\"list\" height=\"25\">".$items->ItemID."</td>";
  8. echo "<td class=\"list\" height=\"25\">".$items->ISKU."</td>";
  9. echo "<td class=\"list\" height=\"25\">".$items->IDescription."<br /></td>";
  10. echo "<td class=\"list\" height=\"25\">&nbsp;</td>";
  11. echo "<td class=\"list\" align=\"right\" height=\"25\">".$items->IPrice."</td>";
  12. echo "<td class=\"list\" align=\"right\" height=\"25\">".$items->IPriceExtended."</td>";
  13. echo "</tr>";
  14.     }

If I add the following to display the database results within the function

PHP:
  1.  
  2. echo $Database->Result($i,"IOrderID")."<p>";
  3. echo $Database->Result($i,"ItemID")."<p>";
  4.  

The results of the echo are the two records as expected.

I can't figure out why my code above won't return both records. Any ideas?

  1. Derek Scruggs Said,

    For this:


    echo $Database->Result($i,"IOrderID")."";

    If you use constants 0 & 1, do you get the same result, i.e.


    echo $Database->Result(0,"IOrderID")."";
    echo $Database->Result(1,"IOrderID")."";

    Since your primary code only uses the version with “IOrderID”, not the one with “itemID”, your underlying dataset may not be returning the appropriate record. It’s hard to analyze without looking at how Database::Result works.

    I would also print_r($order_items_List) right before the return and see what that looks like, but you’ve probably already tried that.

  2. John Said,

    Thanks Derek for the suggestions. I finally got it working. Amazing what happens when you take code and break it down step by step. Most of the time you get it figured out.

Graytone | Design: Tenant Report