0

I have two lists of check boxes. I am checking which check box is selected and based on that I am trying to get an array.

Let's say the two lists are size which have small, medium, large boxes checked and the other one is color which have red, green, blue boxes checked. The array should look something like:

array[['small', 'medium', 'large']['red', 'green', 'blue']]

But I am getting this:

array[["small"],["medium"],["large"]] [["red"],["green"],["blue"]] 

This is the code:

$counter = 0;
$attributes_list = [];
foreach($features_cuts_list as $k => $features_cut) {
    $inner_counter = 0;
    if ($features_cut["selectedid"] != "") {
        $attributes_list[$counter] = [];
        $title_to_get = $features_cut["features_cuts_id"];

        /* Gets the name of the box that is checked */
        $query = "SELECT title FROM features_cuts_translations WHERE lang = '$lang' AND features_cuts_id = '$title_to_get' LIMIT 1;";

        $result = mysql_query($query) or die("Cannot query");
        $attribute_name = mysql_fetch_row($result);
        foreach ($attribute_name as $q) {
            array_push($attributes_list[$counter], $q);
        }
        $counter++;
    } else {

    }
}

EDIT:

This is the deceleration process for $features_cuts_list:

    function getListValuesSql($sql){
            global $link;         //Database connection
            $data=array();
            $subData{0}=array();
    
            $res=mysql_query($sql,$link);
    
            if(mysql_num_rows($res)>0){
                $i=0;
                while($row=mysql_fetch_array($res)){
                for($j=0;$j<mysql_num_fields($res);$j++){
    
                        $field=mysql_field_name($res, $j);
                        $subData{$i}[$field]=$row[$field];
                    }
                    $data[$i]=$subData{$i};
                    $i++;
                }
                    return $data;
            }else{
                return 0;
            }
        } 
$feature_id = $feature["features_id"];
$features_cuts_list = $data->getListValuesSql("
    SELECT DISTINCT fct.*, fc.sort, fc.inner_id, fc.price,
    fcp.features_cuts_id AS selectedid, IFNULL(fcpv.price,fc.price)
    AS price, fcpv.sku
    FROM `features_cuts` as fc
    JOIN `features_cuts_translations` as fct ON fct.features_cuts_id=fc.id
    LEFT JOIN `features_cuts_product_values` as fcpv ON fc.id=fcpv.features_cuts_id AND fcpv.product_id='$pageid'
    LEFT JOIN `features_cuts_products` as fcp ON fc.id=fcp.features_cuts_id AND fcp.product_id='$pageid'
    WHERE fc.features_id='$feature_id' AND fct.lang='$lang'
    Order by fc.sort
");
1
  • @JitendraSoftgrid I edited the question with some more information, I hope it is helpful Commented Apr 23, 2018 at 7:37

1 Answer 1

1

From reading your code I think you have unnecessarily provided the $counter variable.Try this modified code:

$attributes_list = [];
foreach($features_cuts_list as $k => $features_cut) {
$inner_counter = 0;
if ($features_cut["selectedid"] != "") {
    $title_to_get = $features_cut["features_cuts_id"];

    /* Gets the name of the box that is checked */
    $query = "SELECT title FROM features_cuts_translations WHERE lang = '$lang' AND features_cuts_id = '$title_to_get' LIMIT 1;";

    $result = mysql_query($query) or die("Cannot query");
    $attribute_name = mysql_fetch_row($result);
    foreach ($attribute_name as $q) {
        array_push($attributes_list, $q);
    }
    $counter++;
} else {

}

}

If not completely, it will mostly solve your issue. Let me know what the result are , after running this piece of code.

Sign up to request clarification or add additional context in comments.

4 Comments

@Omer, you cant get the same results from these two different codes. There must be some different, most probably you will get a single array in the updated code whereas in the previous code you are getting multiple arrays of single element. try again !!!
Oh sorry for mistaking! It did solve the issue, I just didn't notice the difference at first from looking too long at the wrong output I got. Thanks!
Now that I am looking at it, I am not getting what I need. This is what I am getting: ["Red","Green","Blue"]["Large","Medium","Small"]["Square","Rounded"] and this is what I need: [["Red","Green","Blue"],["Large","Medium","Small"],["Square","Rounded"]] . If I'll push each array to a "master" array, will it work?
@Omer, yes Omer you have to push each array into another array, then you will got the required array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.