2

I am trying to create javascript variable from php, this is what i want to get.

<?PHP echo "var color_name=[<?PHP echo $color_name;?>]"; //var color_name=["Green","Pink"];

<?PHP echo "var style_name=[<?PHP echo $style_name;?>]"; //var style_name=["Basic","Classic"];

this is my predefined_attributes table this is my predefined_attributes table

this is what i have tried,

<?php
$predifined_qry = "SELECT attribute_column_name 
                    FROM predefined_attributes 
                    WHERE category_id=$id";
$predifined_result = mysqli_query($predifined_qry);
$columnNames = Array();
while ($predifined_result_row = mysqli_fetch_assoc($predifined_result)) {
    $columnNames[] = $predifined_result_row['attribute_column_name'];
}

now i can get the attribute_column_name in an array using a foreach loop, but i dont know how to get the attribute_column_value array in a variable like this $style_name, $color_name. please help.

3 Answers 3

1

First of all, if you only need 1 column, and the resource/data you get is not massive, you could just fetch all and extract the column

You could expose your php variables in js through JSON

<?php

$predifined_result = mysqli_query($predifined_qry);
$predifined_rows = mysqli_fetch_all($predifined_result, MYSQLI_ASSOC);
$columnNames = array_column($predifined_rows, 'attribute_column_name');
?>


<script>
    var columnames = JSON.parse("<?= json_encode($columnNames); ?>");
</script>

So now if we add in the other field :

<?php

$predifined_result = mysqli_query($predifined_qry);
$predifined_rows = mysqli_fetch_all($predifined_result, MYSQLI_ASSOC);
$columnNames = array_column($predifined_rows, 'attribute_column_name');
$columnValues = array_column($predifined_rows, 'attribute_column_value');
// array_combines uses the first array as the key and the second as the value
$columns = array_combine($columnNames, $columnValues);
?>

<script>
    var columns = JSON.parse("<?= json_encode($columns); ?>");
</script>

Edit as in the comments, a key can only appear once in a hashtable/array so we need to generate an array of values per key

<?php
$predifined_qry = "SELECT attribute_column_name, attribute_column_value 
                FROM predefined_attributes 
                WHERE category_id=$id";
$predifined_result = mysqli_query($predifined_qry);
$columns = [];
while($row = mysqli_fetch_assoc($predifined_result)) {
    if(!array_key_exists($row['attribute_column_name'], $columns)) {
        $columns[$row['attribute_column_name']] = [];
    }
    $columns[$row['attribute_column_name']][] = $row['attribute_column_value'];
}
?>

<script>
    var columns = JSON.parse("<?= json_encode($columns); ?>");
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

thank you, i have tried your solution, i am getting this output closest to what i want {"color_name":"Green","style_name":"Basic"} can you please help to get the output like this {"color_name":"Green","color_name":"Pink","style_name":"Basic","style_name":"Classic"}
you can use ksort($columns); to sort the array by the keys of the columns
thank you, actually i am getting one key once like this {"color_name":"Green","style_name":"Basic"} it is not repeating like this {"color_name":"Green","color_name":"Pink","style_name":"Basi‌​c","style_name":"Cla‌​ssic"}
you're right, slight mistake on my end, let me edit my answer, what you want is not possible, as you can only have each key once
thank you so much, it is giving the expected output now, works great.
|
1

1st : You need to select attribute_column_value column too

SELECT attribute_column_name,attribute_column_value FROM predefined_attributes where category_id=$id

2nd : You need push both column value in two different array like this

while ( $predifined_result_row = mysqli_fetch_assoc($predifined_result) ) {
    $color_name[] = $predifined_result_row['attribute_column_name'];
    $style_name[] = $predifined_result_row['attribute_column_value'];
    }

3rd : simple apply json_enocde and echo like this

<script>
  var color_name=<?php echo json_encode($color_name); ?>; 
  var style_name=<?php echo json_encode($style_name); ?>;
 </script>

1 Comment

thank you, $color_name[] = $predifined_result_row['attribute_column_name']; i believe this will not have array of color_name field. instead this will hold the array of attribute_column_name fields.
-1
$columnValues[] = $predifined_result_row['attribute_column_value'];

You can paste this code into your while loop

And dont't forget to get the need field form database:

$predifined_qry="SELECT attribute_column_name,attribute_column_value FROM predefined_attributes where category_id=$id";

1 Comment

Not a very constructive answer, better show the whole code, of what is needed

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.