0

I'm new here , so basically what i'm trying to do , i need to take the values from database and put those values in one Array, see below my PHP:

    <?php
$db = mysqli_connect('localhost', 'krissko0o', 'krisko0o', 'KISP');
$sql = "SELECT * FROM products ORDER BY productName ASC";
$rsd = mysqli_query($db, $sql);
while($rs = mysqli_fetch_assoc($rsd)) {
    $id = $rs['id'];
    $productName = $rs['productName'];
    $quantity = $rs['quantity'];
    $litres = $rs['litres'];
    $type = $rs['type'];
    $singlePrice = $rs['singlePrice'];
}
?>

My array:

var arrValues = new Array('<?php echo $id; ?>~~~~~~~~<?php print $productName; ?> - <?php print $litres; ?>~~~~~~~~~~~~~~~~~~~~~~~~~~<?php print $quantity; ?>~~<?php print $type; ?>~~~~~~~~<?php print $singlePrice; ?>');

in this case the output code i want to receive is something like:

var arrValues = new Array('0001~~~~~~Кока Кола - 0.25 l.~~~~~~~~~~~~~~~~~~~~~~~~~~24~~каса~~~~~~~~0.71','0003~~~~~~Кока Кола - 0.5 l.РЕТ~~~~~~~~~~~~~~~~~~~~~~~~12~~стек~~~~~~~~1.09','0033~~~~~~Спрайт - 0.5 l.РЕТ~~~~~~~~~~~~~~~~~~~~~~~~~~~12~~стек~~~~~~~~1.09','0043~~~~~~Швепс Тоник - 0.5 l.РЕТ [недост.]~~~~~~~~~~~~12~~стек~~~~~~~~1.09','0053~~~~~~Сода Кинли - 0.5 l.РЕТ~~~~~~~~~~~~~~~~~~~~~~~12~~стек~~~~~~~~0.78','0112~~~~~~Ред Бул - 0.355 l.~~~~~~~~~~~~~~~~~~~~~~~~~~~24~~стек~~~~~~~~2.93','0305~~~~~~Капи Праскова Бут. - 0.25 l.~~~~~~~~~~~~~~~~~24~~каса~~~~~~~~0.79','0320~~~~~~Капи Ябълка Бут. - 0.25 l.~~~~~~~~~~~~~~~~~~~24~~каса~~~~~~~~0.79','0328~~~~~~Капи Банан - 1 l.*6 [недост.]~~~~~~~~~~~~~~~~6~~~стек~~~~~~~~2.27','0355~~~~~~Капи Вишна Бут. - 0.25 l.~~~~~~~~~~~~~~~~~~~~24~~каса~~~~~~~~0.79','1000~~~~~~Загорка РЕТРО БУТ. - 0.5 l.~~~~~~~~~~~~~~~~~~20~~каса~~~~~~~~1.08','1061~~~~~~Каменица св. БУТ. - 0.5 l.~~~~~~~~~~~~~~~~~~~20~~каса~~~~~~~~1.04','1068~~~~~~Наливно пиво Каменица - 30 l.~~~~~~~~~~~~~~~~30~~кег~~~~~~~~~2.17','1079~~~~~~Stella Artois БУТ. - 0.5 l.~~~~~~~~~~~~~~~~~~20~~каса~~~~~~~~1.37','10851~~~~~Шуменско БОМБИЧКА - 0.33l~~~~~~~~~~~~~~~~~~~~20~~каса~~~~~~~~0.70','1161~~~~~~Старопрамен БУТ. - 0.5 l.~~~~~~~~~~~~~~~~~~~~20~~каса~~~~~~~~1.17','4695~~~~~~Мента Карнобат- 0.7 l.~~~~~~~~~~~~~~~~~~~~~~~1~~~бр.~~~~~~~~~6.01','9188~~~~~~Нестий Праскова - 0.5 l.~~~~~~~~~~~~~~~~~~~~~12~~стек~~~~~~~~0.94','91881~~~~~Нестий Лимон - 0.5 l.~~~~~~~~~~~~~~~~~~~~~~~~12~~стек~~~~~~~~0.94');

but instead , i'm getting this one:

var arrValues = new Array('11~~~~~~~~Бъз лимон - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('7~~~~~~~~Вишна - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('24~~~~~~~~Вишна - 0~~~~~~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~~0');
var arrValues = new Array('33~~~~~~~~Вишна - 0~~~~~~~~~~~~~~~~~~~~~~~~~~0~~~~~~~~~~0');
var arrValues = new Array('18~~~~~~~~Газирана вода - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('34~~~~~~~~Газирана вода - 0~~~~~~~~~~~~~~~~~~~~~~~~~~0~~~~~~~~~~0');
var arrValues = new Array('8~~~~~~~~Горски плод - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('25~~~~~~~~Горски плод - 0~~~~~~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~~0');
var arrValues = new Array('17~~~~~~~~Грейпфрут - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('12~~~~~~~~Грозде - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('10~~~~~~~~Див лимон - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('15~~~~~~~~Дюля - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('27~~~~~~~~Дюля - 0~~~~~~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~~0');
var arrValues = new Array('2~~~~~~~~Жълта лимонада - 0~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~0');
var arrValues = new Array('19~~~~~~~~Жълта лимонада - 0~~~~~~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~~0');
var arrValues = new Array('28~~~~~~~~Жълта лимонада - 0~~~~~~~~~~~~~~~~~~~~~~~~~~0~~~~~~~~~~0');
var arrValues = new Array('51~~~~~~~~Жълта лимонада - 2л~~~~~~~~~~~~~~~~~~~~~~~~~~24~~Стек~~~~~~~~0.25');

Any help with that please ? :) :D

  • Getting an array from a database and sending an array from PHP to JS are TWO different questions and must be asked (or - better - googled up) separately. Either way, the standard to send data from PHP to JS is JSON – Your Common Sense Mar 24 '21 at 06:22
  • Hi , as you can see from my code i already got the database queries , my problem is that when i put those queries in array i'm receiving multiple vars Arrvalues , even if i try with json_encode , i'm receiving them on one row and i can't make it showing every query on a new row – Kristiyan Kostadinov Mar 24 '21 at 06:56
  • It is not about having a "query". It's about having an **array** from this query. After getting an array you may use json_encode. but again, sending an array from PHP to JS is a **different question** which also has been answered multiple times already. – Your Common Sense Mar 24 '21 at 07:04

1 Answers1

-1

I don't know what the code pieces 2-3 are or where they come from nor what you will do with that array, but i think in php you could do the following for getting the data in an array:

<?php
    $db = mysqli_connect('localhost', 'krissko0o', 'krisko0o', 'KISP');
    $sql = "SELECT * FROM products ORDER BY productName ASC";
    $rsd = mysqli_query($db, $sql);
    $arrValues = array();                               //will become a multidimensional array
    while($rs = mysqli_fetch_assoc($rsd)) {
        $tempArray = array(                             //one array per dataset
            "id" => $rs['id'],
            "productName" => $rs['productName'],
            "quantity" => $rs['quantity'],
            "litres" => $rs['litres'],
            "type" => $rs['type'],
            "singlePrice" => $rs['singlePrice']
        );
        array_push($arrValues, $tempArray);             //insert the dataset in the multidimensional array
    }
?>

If you want to get the data from php to js, it would be helpful to know on which way you want to get the data there (echo it in a script tag or send it as a response to an ajax request?)

Second part after the reply on my call back question in the comments

then of course you don't need a php array, just a long string:

<?php
    $db = mysqli_connect('localhost', 'krissko0o', 'krisko0o', 'KISP');
    $sql = "SELECT * FROM products ORDER BY productName ASC";
    $rsd = mysqli_query($db, $sql);
    $arrValues = '';
    while($rs = mysqli_fetch_assoc($rsd)) {
        $arrValues .= '"' . 
            $rs['id'] . '~~~~~~~~' . 
            $rs['productName'] . ' - ' . 
            $rs['quantity'] . '~~~~~~~~~~~~~~~~~~~~~~~~~~' . 
            $rs['litres'] . '~~' . 
            $rs['type'] . '~~~~~~~~' . 
            $rs['singlePrice'] . 
        '", ';
    }
    echo '<script> var arrValues = [' . $arrValues . ']; </script>';
?>
biberman
  • 5,606
  • 4
  • 11
  • 35
  • What's the point in rewriting $rs into $tempArray? – Your Common Sense Mar 24 '21 at 06:20
  • oh, i didn't read the question title properly and just answered the array part in the text - should i better delete my answer? – biberman Mar 24 '21 at 06:32
  • Hi Biberman and thank you for your answer , basically code pieces 2 and 3 are coming from the source code of the page , what i want is when i put those results in new Array , i want every result to be on a different row , however i'm getting a different var Arrvalues for every query i've got in my database , which means if i've got 10 rows in the database i will receive 10 rows with var arrValues = .... and what i need is to receive one var ArrValues but fullfilled with every query i've got, of course separated by coma. – Kristiyan Kostadinov Mar 24 '21 at 06:49
  • @KristiyanKostadiov: you didn't answer my question, on which way you want to get the data to js (echo it in a script tag or send it as a response to an ajax request?) – biberman Mar 24 '21 at 06:54
  • echo it in a script tag – Kristiyan Kostadinov Mar 24 '21 at 07:04
  • Creating the javascript code by hand is a dangerous business. One service character in the data and your whole site is broken. *Let alone deliberate attacks*. That's why json is used to communicate between the server and the browser – Your Common Sense Mar 24 '21 at 07:50
  • Biberman YOU ARE A LEGEND ! Thank you ! :) – Kristiyan Kostadinov Mar 24 '21 at 07:52