Jump to content

Help With $csvLine = array() Data


dinoslikeroar

Recommended Posts

I'm still new to this coding and I am trying to figure out what I'm supposed to use in the area for $csvLine = array(). Example below:

 

What I'm not sure is what information do I put in the $csvLine = array(
'SAMPLE1 ITEMS RECEIVED' => $orderInfo['NOT SURE WHAT TO PUT HERE'],
) to pull the data

 

Below is some of the code:

 

-----

$test_sql = "SELECT DISTINCT order_or_item_number as item_number, location, search_type ".
            "FROM order_history ".
            "LEFT JOIN research_queue on order_or_item_number = item_number ".
            "WHERE (order_history.modified_date BETWEEN ??? AND ???) ".
            "AND action LIKE '%SENT TO REVIEW'";
            
$start_date = '2016-07-20 00:00:00';
$end_date = '2016-07-20 23:59:59';
                    
$test_sth = DBHelper::execute($test_sql, 'test_twin', $start_date, $end_date);
$items_submitted = array();
 
$test_out_sql = "SELECT DISTINCT order_or_item_number as item_number, location, search_type ".
             "FROM order_history ".
             "LEFT JOIN research_queue on order_or_item_number = item_number ".
             "WHERE (order_history.modified_date BETWEEN ??? AND ???) ".
             "AND action LIKE '%COMPLETED FROM REVIEW'";
             
                    
$test_out_sth = DBHelper::execute($test_out_sql, 'test_twin', $start_date, $end_date);
$items_completed = array();
 
while($result=mysql_fetch_assoc($test_sth)) {
    $items_submitted[] = $result;
   
}
   
while($result=mysql_fetch_assoc($test_out_sth)) {
    $items_completed[] = $result;
    
}
 
// Begin main while loop
$sample1_received = array();
$sample2_received = array();
 
$sample1_completed = array();
$sample2_completed = array();
 
foreach($items_submitted as $item){
    $psn = substr($item['item_number'], 2, 4);
    
    switch ($psn) {
        case '1234':
        case '0123':
            $sample1_received[] = $item['item_number'];
            break;
        
        case '1232':
        case '5659':
        $sample2_received[] = $item['item_number'];
            break;
          
        }
    }
 
foreach($items_completed as $item){
    $psn = substr($item['item_number'], 2, 4);
    
    switch($psn) {
        case '1234':
        case '0123':
            $sample1_completed[] = $item['item_number'];
            break;
        
        case '1232':
        case '5659':
            $sample2_completed[] = $item['item_number'];
            break;
       }   
}
 
 
$csvLine = array(
'DATE' => $orderInfo[''],
'SAMPLE1 ITEMS RECEIVED' => $orderInfo[''],
'SAMPLE1 ITEMS COMPLETED' => $orderInfo[''],
'SAMPLE2 ITEMS RECEIVED' => $orderInfo[''],
'SAMPLE2 ITEMS COMPLETED' => $orderInfo[''],
  );
 
    $csvLine = array_map('clean', $csvLine);
    $report->writeLine($csvLine);
    $rowsWritten++;
// End main while loop

 

Edited by cyberRobot
Please surround code with [code][/code] tags
Link to comment
Share on other sites

Based on the code shown, $orderInfo isn't defined. It looks like all the data is stored in the following arrays:

$sample1_received
$sample2_received
$sample1_completed
$sample2_completed

What would you like to happen in the following line?

$csvLine = array(
    'DATE' => $orderInfo[''],
    'SAMPLE1 ITEMS RECEIVED' => $orderInfo[''],
    'SAMPLE1 ITEMS COMPLETED' => $orderInfo[''],
    'SAMPLE2 ITEMS RECEIVED' => $orderInfo[''],
    'SAMPLE2 ITEMS COMPLETED' => $orderInfo[''],
);
 
 
Are the values, where you currently have $orderInfo[''], supposed to be a string...or an array?
 
 
Side note: the mysql_ functions were removed in the latest release of PHP. You will want to use a different API soon. More information can be found here:
Link to comment
Share on other sites

 

Based on the code shown, $orderInfo isn't defined. It looks like all the data is stored in the following arrays:

$sample1_received
$sample2_received
$sample1_completed
$sample2_completed

What would you like to happen in the following line?

$csvLine = array(
    'DATE' => $orderInfo[''],
    'SAMPLE1 ITEMS RECEIVED' => $orderInfo[''],
    'SAMPLE1 ITEMS COMPLETED' => $orderInfo[''],
    'SAMPLE2 ITEMS RECEIVED' => $orderInfo[''],
    'SAMPLE2 ITEMS COMPLETED' => $orderInfo[''],
);
 
 
Are the values, where you currently have $orderInfo[''], supposed to be a string...or an array?
 
 
Side note: the mysql_ functions were removed in the latest release of PHP. You will want to use a different API soon. More information can be found here:

 

 

The below is the lines above the $csvLine = array() information
 
while ($orderInfo = mysql_fetch_assoc($test_sth)) {
    if ($options->debug)
        $progressLength = 
          print_progress(++$count, $numRows, $scriptStartTime, $progressLength);
          
while ($orderInfo = mysql_fetch_assoc($testout_sth)) {
    if ($options->debug)
        $progressLength = 
          print_progress(++$count, $numRows, $scriptStartTime, $progressLength);       
 
}
}
 
I'm needing the information to display the data into those columns in my csv file.
Link to comment
Share on other sites

Sorry, but your code is a mess.

 

You need to stop copying and pasting, and you should understand what you want before you write anything. You've produced an impressive amount of code, but none of this really fits together. For example, where do you separate the different orders? Right now, you're collecting all items across all orders.

 

As far as I understand, the task is this:

  • You want to generate a CSV file with one line per order.
  • The items of each order should be divided into two samples according to their item number. Within each sample, the items are further divided into received and completed items.

This requires one query to select the order items and one loop to fetch and separate the items. Do not duplicate code when you need to perform similar actions. Whenever you feel the urge to hit ctrl+c/ctrl+v, stop and think. There's a smarter solution.

 

You also need to sort the items by the order ID so that you can switch to a new CSV line whenever all items of one order have been processed.

<?php

// use constants instead of cluttering your code with hard-coded values coming out of nowhere
const ORDER_DATE = '2016-07-20';    // MySQL knows that a day has 24 hours; just give it the date



// when multiple tables are involved, always use *qualified* column names (i. e. table.column) so that there are no
// name collisions and the reader doesn't have to guess which table the column belongs to
$order_items_sql = "
    SELECT DISTINCT   -- are you sure you need DISTINCT?
        research_queue.item_number,    -- I'm guessing the table here
        order_history.location,        -- I'm guessing the table here; is this column even needed?
        order_history.search_type      -- I'm guessing the table here; is this column even needed?
        research_queue.action LIKE '%COMPLETED FROM REVIEW' AS completed    -- Why is there no proper status field?
    FROM
        order_history
        JOIN research_queue ON order_history.order_or_item_number = research_queue.item_number    -- you had a LEFT JOIN here, which made no sense
    WHERE
        DATE(order_history.modified_date) = ???
        AND (research_queue.action LIKE '%SENT TO REVIEW' OR research_queue.action LIKE '%COMPLETED FROM REVIEW')
    ORDER BY
        order_history.order_id    -- or whatever the order ID is called
";

$order_items = DBHelper::execute($order_items_sql, 'test_twin', ORDER_DATE);

The code must be adjusted. Since the problem is so poorly documented, I had to guess most of the time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.