Announcement

Collapse
No announcement yet.

[SOLVED] array return only one item instead of two

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [SOLVED] array return only one item instead of two

    sc_lookup(estimateFormID, "select EstimateForm.id from EstimateForm, EstimateItem
    where EstimateForm.id = EstimateItem.estimate_code
    and EstimateItem.price='$product_price'");

    $estimate_ids = {estimateFormID[0][0]};


    sc_alert("$estimate_ids[0]");


    array return only one item, should have two items

    if I use below script,then error message **mysql_fetch_row() expects parameter 1 to be resource, string given**

    sc_lookup(estimateFormID, "select EstimateForm.id from EstimateForm, EstimateItem
    where EstimateForm.id = EstimateItem.estimate_code
    and EstimateItem.price='$product_price'");

    $estimate_ids = {estimateFormID[0][0]};

    while ($estimate_id = mysql_fetch_row($estimate_ids)) {
    sc_alert("$estimate_ids[0]");
    }

    Regards,
    Franko TSE

  • #2
    sc_lookup return an array. So the first row is in element 0, the next in 1 etc. To find out how many items in the array you need to find the size of the array. The array already contains the full resultset that's why you can't use fetch_row.
    Last edited by aducom; 07-16-2013, 08:56 AM.
    Albert Drent
    aducom software netherlands
    scriptcase partner, reseller, support and (turn-key) development
    www.scriptcase.eu / www.scriptcase.nl

    Comment


    • #3
      Hello Albert,

      really appreciate your response, but why sc_alert only return one value, should be two id, 2 and 3 but just return 2

      sc_lookup(estimateFormID, "select EstimateForm.id from EstimateForm, EstimateItem
      where EstimateForm.id = EstimateItem.estimate_code
      and EstimateItem.price='$product_price'");

      $estimate_ids = {estimateFormID[0][0]};

      sc_alert("$estimate_ids[0]");

      Comment


      • #4
        Because

        $estimate_ids = {estimateFormID[0][0]};

        returns only one element (first field of first row)
        Albert Drent
        aducom software netherlands
        scriptcase partner, reseller, support and (turn-key) development
        www.scriptcase.eu / www.scriptcase.nl

        Comment


        • #5
          Hello Albert,

          so how to get all array elements, thank you

          Comment


          • #6
            It's a two dimensional array. TO find the number of rows you use sizeof: http://php.net/manual/es/function.sizeof.php
            the number of columns you know because that's the amount of fields in your select list.

            array[0][0], first field, first row
            array[0][1], second field, first row
            array[1][1], second field second row.,

            Does that help?
            Albert Drent
            aducom software netherlands
            scriptcase partner, reseller, support and (turn-key) development
            www.scriptcase.eu / www.scriptcase.nl

            Comment


            • #7
              sc_lookup(product_id, "SELECT id FROM EstimateForm");
              $product_id = {product_id[0][0]};

              Estimateform contains 2 id, 4,5
              print ($product_id); returns 4
              print ($product_id[0]); returns 4
              print ($product_id[1]); returns nothing

              Comment


              • #8
                Originally posted by frankotse View Post
                sc_lookup(product_id, "SELECT id FROM EstimateForm");
                $product_id = {product_id[0][0]};

                Estimateform contains 2 id, 4,5
                print ($product_id); returns 4
                print ($product_id[0]); returns 4
                print ($product_id[1]); returns nothing
                Try

                {product[0][0]}; like:

                Code:
                $check_sql = "SELECT id FROM EstimateForm";
                sc_lookup(rs, $check_sql);
                
                if (isset({rs[0][0]}))     // Row found
                {
                    {product} = {rs[0][0]};
                }
                
                if (isset({rs[1][0]}))     // Row found
                {
                    {product} = {rs[0][0]};
                }
                Since you can determine the size of the array, you know how many rows you have. Otherwize I would recommend:

                Code:
                $check_sql = 'SELECT id FROM EstimateForm";
                
                sc_select(rs, $check_sql);
                if (false == {rs})     // Error while accessing database
                {
                    sc_error_message('Error while accessing database.');
                }
                else
                {
                   while(!$rs->EOF)
                    {
                		{yourfield} = $rs->fields[0];
                		$rs->MoveNext();
                    }
                    $rs->Close();
                }
                Last edited by aducom; 07-18-2013, 07:33 AM.
                Albert Drent
                aducom software netherlands
                scriptcase partner, reseller, support and (turn-key) development
                www.scriptcase.eu / www.scriptcase.nl

                Comment


                • #9
                  I use below script for a AppGini generated app but don't know how to rewrite in scriptcase

                  $estimate_ids = sql("select EstimateForm.id from EstimateForm, EstimateItem where EstimateForm.id = EstimateItem.estimate_code and EstimateItem.price='$product_id'");
                  EstimateItem.estimate_code and EstimateItem.price='$product_id'");
                  while ($estimate_id = mysql_fetch_row($estimate_ids)) {
                  updateEstimateTotals($estimate_id[0]);
                  }

                  Comment


                  • #10
                    I used below traditional code to solve it, hope can help others

                    $id ={id};
                    mysql_connect("localhost", "root", "password");
                    mysql_select_db( "test" );
                    $query=mysql_query("select price from Product where id = '$id'");

                    $query1= mysql_fetch_row($query);
                    $product_price=$query1[0];
                    mysql_query("update EstimateItem
                    set total='$product_price'* qty where price='{id}'");
                    //print "$product_price";

                    $sql=mysql_query("select EstimateForm.id from EstimateForm, EstimateItem
                    where EstimateForm.id = EstimateItem.estimate_code
                    and EstimateItem.price='$id'");
                    while ($estimate_id = mysql_fetch_row($sql)) {
                    updateEstimateForm($estimate_id[0]);
                    //print "$estimate_id[0]";
                    }

                    Comment

                    Working...
                    X