Passing multiple dimension array in PHP - php

MySql query returns me a multi-dimensional array :
function d4g_get_contributions_info($profile_id)
{
$query = "select * from contributions where `project_id` = $profile_id";
$row = mysql_query($query) or die("Error getting profile information , Reason : " . mysql_error());
$contributions = array();
if(!mysql_num_rows($row)) echo "No Contributors";
while($fetched = mysql_fetch_array($row, MYSQL_ASSOC))
{
$contributions[$cnt]['user_id'] = $fetched['user_id'];
$contributions[$cnt]['ammount'] = $fetched['ammount'];
$contributions[$cnt]['date'] = $fetched['date'];
$cnt++;
}
return $contributions;
}
Now I need to print the values in the page where I had called this function. How do I do that ?

change the function like this:
while($fetched = mysql_fetch_array($row, MYSQL_ASSOC))
{
$contributions[] = array('user_id' => $fetched['user_id'],
'ammount' => $fetched['ammount'],
'date' => $fetched['date']);
}
return $contributions;
Then try below:
$profile_id = 1; // sample id
$result = d4g_get_contributions_info($profile_id);
foreach($result as $row){
$user_id = $row['user_id']
// Continue like this
}

Related

PHP overwrite array mysql query?

i have this code and i want to return an array of object and inside that object an associative array.
something like this
[
{"ids":["id1",'id2','id3'],
"item":["name","name2","name3"]},
{"ids":["id4",'id5','id6'],
"item":["name4","name5","name6"]},
{"ids":["id1114",'id1115','id1116'],
"item":["name11114","name11115","name1116"]},
]
it sends the objects with the array but it only has the last element in the array
//get customers of single item
if($this->connect()->connect_error){
echo die("Connection failed: " . $this->connect()->connect_error);
}else{
for($i=0; $i<count($final_array['item']); $i++){
$single = $final_array['item'][$i];
$sql = "SELECT ACCT_ID, PARTS_EXPIRY, dcii_item, dcii_desc, dcii_acct_id, dcii_qty AS QTY
FROM dos_account AS DOS
JOIN dos_client_invoice_items as DOS2
ON DOS.ACCT_ID = DOS2.dcii_acct_id
WHERE dcii_item = '$single'
AND DOS.PARTS_EXPIRY BETWEEN '$start' AND '$end'
ORDER BY DOS.PARTS_EXPIRY";
$result = $this->connect()->query($sql);
$numRows = $result->num_rows;
if($numRows > 0){
// $array_container = [];
while($row = $result->fetch_assoc()){
$item[] = $row['dcii_item'];
$acctID[] = $row['ACCT_ID'];
$array_container5 = array();
$array_container5[] = $row['dcii_item'];
$array_container = array();
$array_container[] = $row['ACCT_ID'];
//array_push($array_container, $row['dcii_item']);
}
$array_container2['item'] = $array_container5;
$array_container2['ids'] = $array_container;
$final_array['objectContainer'][] = $array_container2;
}
}
$jsonData = json_encode($final_array);
echo $jsonData;
}

can't get result from sp into array

I'm trying to get the result of a stored procedure in MySQL into an array with use of php. The code I have:
function get_all_uzovi($dbc) {
//$query = "SELECT diagnose_id, diagnose_code, specialisme_agb_code_fk
// FROM tbl_diagnoses
// WHERE specialisme_agb_code_fk = '$var_chosen_specialism'
// ORDER BY diagnose_code ASC";
$result = mysqli_query($dbc,"CALL spGetUzovi");
//WHAT DOES NOT WORK:
//$data = array();
//while ( $row = $result->fetch_assoc() ) {
// $data[] = $row;
//}
//return json_encode($data);
//WHAT DOES WORK:
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
}
The code under "//WHAT DOES NOT WORK" is what I need: the result as a json format. For some reason it gives me nothing..
The code under "//WHAT DOES WORK", does work, but this is not what I want.
the code for the sp:
CREATE DEFINER=`ziekenh3`#`localhost` PROCEDURE `spGetUzovi`()
NO SQL
SELECT *
FROM tbl_uzovi
the code that I use when working with queries (which does work) instead of sp:
function get_diagnoses($dbc,$var_chosen_specialism) {
$query = "SELECT diagnose_id, diagnose_code, specialisme_agb_code_fk
FROM tbl_diagnoses
WHERE specialisme_agb_code_fk = '$var_chosen_specialism'
ORDER BY diagnose_code ASC";
$result = mysqli_query($dbc,$query);
$data = array();
//while ( $row = $result->fetch_assoc() ) {
while ( $row = mysqli_fetch_assoc($result) ) {
$data[] = $row;
}
return json_encode($data);
}
Any thoughts?
Does this work?
function get_all_uzovi($dbc) {
$result = mysqli_query($dbc,"CALL spGetUzovi");
$data = array();
while ( $row = $result->fetch_assoc() ) {
$data[] = $row;
}
return json_encode($data);
}
I Solved it. A bit of a stupid problem: there where some fields in the result set that contained characters like: , ( . etc. I don't think json likes this..
When I did a SELECT * FROM on another table it worked. So, my code that works now is:
function get_all_uzovi($dbc) {
$q = "CALL spGetUzovi";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_assoc($r)) {
$data[] = $row;
}
return json_encode($data);
}
I'm sorry for the inconvenience!

SQL Array into Array based function

Default Set Up
$stream->setTrack (array('cookie'));
Trial
$sql = "SELECT DISTINCT text FROM keywords"; $query = mysql_query($sql) or die($sql . ' - ' . mysql_error());
$keys = array();
while ($row = mysql_fetch_array($query)) {
$keys[] = $row[0];
}$stream->setTrack ($keys);
The default set up takes the written word and places it into an array
The trial set up should retrieve a set of keywords from a mysql table as an array and insert them into the function.
It doesn't work. What's wrong?
public function setTrack($trackWords)
{
$trackWords = ($trackWords === NULL) ? array() : $trackWords;
sort($trackWords); // Non-optimal, but necessary
if ($this->trackWords != $trackWords) {
$this->filterChanged = TRUE;
}
$this->trackWords = $trackWords;
}

check table from my sql query

Hello I'm checking duplicated data from tables. I have a problem that from where data selected. My code is:
$sub_cat = array();
$select = array("core_network","daisy_chain", "rf_bts", "rf_power", "rf_transmission");
$d='0';
for ($i=0;$i<=4;$i++){
$SQL = "SELECT sub_cat FROM (".$select[$i].") WHERE location=('".$id."')";
$result = mysql_query($SQL);
$cs=$d;
if ($result) {
while ($db_field = mysql_fetch_array($result)) {
if(!in_array($db_field['sub_cat'],$sub_cat)) {
$sub_cat[]= $db_field['sub_cat'];
$cs++;
$d=$cs;
$d--;
}
}
}
I need to know that sub_cat selected from which $select[i]. How to find it?
To get the values, do this:
$sub_cat = array();
$select = array("core_network","daisy_chain", "rf_bts", "rf_power", "rf_transmission");
$d='0';
for ($i=0;$i<=4;$i++){
$SQL = "SELECT sub_cat FROM (" . $select[$i] . ") WHERE location=('".$id."')";
$result = mysql_query($SQL); // deprecated - use PDO
$cs = $d;
if ($result) {
while ($db_field = mysql_fetch_array($result)) {
if(!in_array($db_field['sub_cat'], $sub_cat)) {
$table = $select[$i];
$sub_cat[$table][] = $db_field['sub_cat'];
// I have no clue what's going on here in your example:
$cs++;
$d=$cs;
$d--;
}
}
}
}
Then, to retrieve it:
foreach ($sub_cat as $table_name => $values) {
foreach ($values as $row) {
// output values here
}
}

json output array php

I have this piece of code:
Tipo_Id = mysql_real_escape_string($_REQUEST["tipo"]);
$Sql = "SELECT DISTINCT(tabveiculos.Marca_Id), tabmarcas.Marca_Nome
FROM tabmarcas, tabveiculos
WHERE tabmarcas.Tipo_Id = '$Tipo_Id'
AND tabmarcas.Marca_Id = tabveiculos.Marca_Id
ORDER BY tabmarcas.Marca_Nome Asc";
$Query = mysql_query($Sql,$Conn) or die(mysql_error($Conn));
$marcas = array();
while ($Rs = mysql_fetch_array($Query)) {
$marcas[] = array(
$Rs['Marca_Id'] =>
$Rs['Marca_Nome']
);
}
echo ( json_encode($marcas) );
this returns a result like this:
[{"2":"Chevrolet"},{"7":"Citro"},{"4":"Fiat"},{"3":"Ford"},{"6":"Peugeot"},{"1":"Volkswagen"}]
so how i can change to returns like this:
{"2":"Chevrolet","7":"Citro","4":"Fiat","3":"Ford","6":"Peugeot","1":"Volkswagen"}
You're currently creating a new array for each key value pair, hence, ending up with a multidimensional array. Do the following instead.
while ($Rs = mysql_fetch_array($Query)) {
$marcas[ $Rs['Marca_Id'] ] = $Rs['Marca_Nome'];
}

Categories