Problem while looping associative array

Discussion in 'PHP' started by anindya.roy, Apr 17, 2009.

  1. #1
    Function getStockBySizeDetails

    function getStockBySizeDetails($stockId)
    {
    $sizeDetails = "";

    $querySize = "SELECT * FROM stock_by_size WHERE stock_id = $stockId";
    $resultSize = mysql_query($querySize);

    while($rowSize = mysql_fetch_array($resultSize))
    {
    $sizeDetails[$index]['stockSizeId'] = $rowSize['id'];
    $sizeDetails[$index]['size'] = $rowSize['size'];
    $sizeDetails[$index]['length'] = $rowSize['length'];
    $sizeDetails[$index]['stockQty'] = $rowSize['stock_qty'];
    $sizeDetails[$index]['leftForProduction'] = $rowSize['left_for_production'];
    $index++;
    }
    return $sizeDetails;
    }

    Function getCareLabelStockByStyle

    function getCareLabelStockByStyle($basicInfoId)
    {
    $index = 0;
    $careLabelStockInfoByStyle = "";
    $materialCatId = getMaterialCatIdByName('care label');

    $query = " SELECT sm.id,sm.buyer_id,sm.material_id,sm.material_cat_id,sm.total_stock_qty,sm.total_qty_left_for_production,sm.location,clml.basic_info_id,clml.description,clml.color,clml.ref_no,clml.unit_id,clml.total_req_qty,clml.total_rcv_qty,clml.balance,clml.rate";
    $query .= " FROM stock_material as sm, care_label_material_list as clml WHERE ";
    $query .= "(sm.material_cat_id = $materialCatId AND sm.material_id = clml.id)";
    $query .= " AND clml.basic_info_id = $basicInfoId";

    $result = mysql_query($query);

    while($row = mysql_fetch_array($result))
    {
    $careLabelStockInfoByStyle[$index]['stockId'] = $row['id'];
    $careLabelStockInfoByStyle[$index]['buyerId'] = $row['buyer_id'];
    $careLabelStockInfoByStyle[$index]['materialId'] = $row['material_id'];
    $careLabelStockInfoByStyle[$index]['materialCatId'] = $row['material_cat_id'];
    $careLabelStockInfoByStyle[$index]['totalStockQty'] = $row['total_stock_qty'];
    $careLabelStockInfoByStyle[$index]['totalQtyLeftForProduction'] = $row['total_qty_left_for_production'];
    $careLabelStockInfoByStyle[$index]['location'] = $row['location'];
    $careLabelStockInfoByStyle[$index]['basicInfoId'] = $row['basic_info_id'];
    $careLabelStockInfoByStyle[$index]['description'] = $row['description'];
    $careLabelStockInfoByStyle[$index]['color'] = $row['color'];
    $careLabelStockInfoByStyle[$index]['refNo'] = $row['ref_no'];
    $careLabelStockInfoByStyle[$index]['unit'] = get_unitname($row['unit_id']);
    $careLabelStockInfoByStyle[$index]['totalReqQty'] = $row['total_req_qty'];
    $careLabelStockInfoByStyle[$index]['totalRcvQty'] = $row['total_rcv_qty'];
    $careLabelStockInfoByStyle[$index]['balance'] = $row['balance'];
    $careLabelStockInfoByStyle[$index]['rate'] = $row['rate'];
    $careLabelStockInfoByStyle[$index]['sizeDetails'] = getStockBySizeDetails($row['id']); /* Function getStockBySizeDetails */
    $index++;
    }
    return $careLabelStockInfoByStyle;
    }

    Function checkStockInfoToTransfer

    function checkStockInfoToTransfer($stockInfo,$transferQty)
    {
    $errorId = 1;
    $stockQuery = "";

    for($i = 0; $i < count($stockInfo); $i++)
    {
    $id = $stockInfo[$i]['stockId'];
    if(is_array($stockInfo[$i]['sizeDetails']))
    {
    $sizeDetails = $stockInfo[$i]['sizeDetails'];
    for($j = 0; $j < count($sizeDetails); $j++)
    {
    $sizeId = $sizeDetails[$j]['stockSizeId']; *******
    $stockQuery = "SELECT * FROM stock_by_size WHERE id = $sizeId";
    $stockResult = mysql_query($stockQuery);
    $row = mysql_fetch_array($stockResult);
    $stockQty = $row['stock_qty'];
    if($transferQty[$id][$sizeId] > $stockQty)
    {
    $errorId = 0;
    return $errorId;
    }
    }
    }
    else
    {
    $stockQuery = "SELECT * FROM stock_material WHERE id = $id";

    $stockResult = mysql_query($stockQuery);
    $row = mysql_fetch_array($stockResult);
    $stockQty = $row['total_stock_qty'];

    if($transferQty[$id] > $stockQty)
    {
    $errorId = 0;
    return $errorId;
    }
    }
    }
    return $errorId;
    }

    $careLabelStockInfoByStyle = getCareLabelStockByStyle($basicInfoId);
    $errorId = checkStockInfoToTransfer($careLabelStockInfoByStyle,$_REQUEST['transferCareLabel']);

    While looping in function checkStockInfoToTransfer, i cant get the value of of $sizeId(Look at the starred line) at first iteration but after the first iteration i get the remaining $sizeId value.

    May be i made something wrong while looping which i cant fix....
    I need help.

    Thanx in advance

    Anindya
     
    anindya.roy, Apr 17, 2009 IP