First post here, so please bear with me.
I'm trying to create a treeview using bootstrap-treeview and I've nearly got my response page working but level 3 isn't, I can't get the next node working.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include "db_mysqli.php";
$result = mysqli_query($connection, "SELECT group_id, group_name FROM material_groups;");
$json_response = array(); //Create an array
while ($row = mysqli_fetch_array($result))
{
// GROUPs
$row_array = array();
$row_array['id'] = $row['group_id'];
$row_array['text'] = $row['group_name'];
$row_array['name'] = $row['group_name'];
$row_array['parent_id'] = $row['group_id'];
// $row_array['answers'] = array();
$group_id = $row['group_id'];
// MATERIAL TYPES
$type_qry = mysqli_query($connection, "SELECT material_types.material_type_id, material_groups.group_id, material_types.material_type_name FROM material_groups
INNER JOIN material_types ON material_groups.group_id = material_types.group_id
WHERE material_groups.group_id = ".$group_id."
ORDER BY material_types.material_type_id");
while ($type_row = mysqli_fetch_array($type_qry))
{
$row_array['nodes'][] = array(
// $itemsByReference[$item['id']]['nodes'] = array();
'id' => $type_row['material_type_id'],
'text' => $type_row['material_type_name'],
'name' => $type_row['material_type_name'],
'parent_id' => $type_row['group_id'],
);
$material_type_id = $type_row['material_type_id'];
// MATERIALS
$material_qry = mysqli_query($connection, "SELECT material_types.material_type_id, material_groups.group_id, materials.material_name, materials.material_id
FROM material_types INNER JOIN materials ON material_types.material_type_id = materials.material_type_id
INNER JOIN material_groups ON material_types.group_id = material_groups.group_id And materials.group_id = material_groups.group_id
WHERE material_types.material_type_id = ".$material_type_id." AND material_groups.group_id =".$group_id);
while ($material_row = mysqli_fetch_array($material_qry))
{
$row_array['nodes'][] = array(
'id' => $material_row['material_id'],
'text' => $material_row['material_name'],
'parent_id' => $material_row['material_type_id']
);
$mateial_id = $material_row['material_id'];
}
}
array_push($json_response, $row_array); //push the values in the array
}
echo json_encode($json_response);
?>
Levels 1 and 2 work perfectly, any help would be greatly appreciated.
Thanks
I've tried a lot of different code but this was the closest to a working solution.
Related
I try to encode my result in a json but it doesn't really work. When the query has more than one result than it doesn't work. When the result has only one line, than everything works fine.
if (isset($_REQUEST['query'])) {
$query = $_REQUEST['query'];
$sql = mysqli_query ($db, "SELECT a, b FROM table WHERE a LIKE '%{$query}%' OR b LIKE '%{$query}%'");
$array = array();
while ($row = mysqli_fetch_assoc($sql)) {
$array[] = array (
'label' => $row['a'].', '.$row['b'],
'value' => $row['a'],
);
}
//RETURN JSON ARRAY
echo json_encode ($array);
}
Try this code
if (isset($_REQUEST['query'])) {
$query = $_REQUEST['query'];
$sql = mysqli_query ($db, "SELECT a, b FROM table WHERE a LIKE '%{$query}%' OR b LIKE '%{$query}%'");
$array = array();
$i = 0;
while ($row = mysqli_fetch_assoc($sql)) {
$array[$i] = array (
'label' => $row['a'].', '.$row['b'],
'value' => $row['a'],
);
$i++;
}
//RETURN JSON ARRAY
echo json_encode ($array);
}
function practise()
{
$this->load->database();
$qry = mysql_query("select * from demmo");
if (mysql_num_rows($qry) > 0)
{
while ($row = mysql_fetch_array($qry))
{
$created = $row['created'];
//from here
$qry = mysql_query("select * from demmo where created = '$created'");
while ($res = mysql_fetch_array($qry))
{
$user_id = $res['id'];
$name = $res['name'];
$created2 = $res['created'];
$users[] = array('user_id' => $user_id, 'name' => $name);
}
$dotts[] = array('created' => $created2);
//till here
}
return array ($dotts,$users);
}
}
in demmo table i am trying to fetch data and showing that data according to date .the problem is that the code is only selecting one date from the table from created rows and showing that data only .fortunately data shown is not only last but the data with actual date.
You need to create an array and use array_push to get more than one result. Right now your code is only returning the last result of the while loop:
For example, to get all of the dates:
$dotts = array();
$allusers = array();
while ($res = mysql_fetch_array($qry))
{
$user_id = $res['id'];
$name = $res['name'];
$created2 = $res['created'];
array_push($dotts, $created2);
$users[] = array('user_id' => $user_id, 'name' => $name);
array_push($allusers, $users);
}
//
return array ($dotts,$allusers);
You need to create an array and use array_push function , then only it will have more than one value.
example:
create an empty array as
$allUser = array();
then after this line
$users[] = array('user_id' => $user_id, 'name' => $name);
use array_push as
array_push($allUser, $users);
}
return array($dots, $allUser);
My desired output
{"rowcount":4
[{"provider_id":"1","provider_name":"Crecent Computers","sub_name":["Hardware","Software","Networks"]}],[{"provider_id":"4","provider_name":"Testing Co. LLC","sub_name":["Hardware","Software","Networks"]}],[{"provider_id":"41","provider_name":"Itiology","sub_name":["Hardware","Software","Networks","All IT Services"]}],[{"provider_id":"42","provider_name":"ITiology","sub_name":["Hardware","Software","Networks","All IT Services","Website Design "]}]}
My desired output end
My current JSON output
{"rowcount":4,"0":[{"provider_id":"1","provider_name":"Crecent Computers","sub_name":["Hardware","Software","Networks"]}],"1":[{"provider_id":"4","provider_name":"Testing Co. LLC","sub_name":["Hardware","Software","Networks"]}],"2":[{"provider_id":"41","provider_name":"Itiology","sub_name":["Hardware","Software","Networks","All IT Services"]}],"3":[{"provider_id":"42","provider_name":"ITiology","sub_name":["Hardware","Software","Networks","All IT Services","Website Design "]}]}
JSON Output end
PHP CODE
$result = mysqli_query($con, "select * from service_provider where servicecategory_id = '1'");
if ($counter = mysqli_query($con, "select * from service_provider where servicecategory_id = '1'"))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($counter);
// Free result set
mysqli_free_result($counter);
}
$data_points = array();
$subcatArray = array();
$data_points["rowcount"] = $rowcount;
while($row = mysqli_fetch_assoc($result))
{
$subcatresult = mysqli_query($con, "SELECT sub_name, price FROM sub_services WHERE provider_id = " . $row['provider_id']);
while($subcatrow = mysqli_fetch_assoc( $subcatresult))
{
$subcatArray[] = $subcatrow['sub_name'];
unset($subcatrow);
$subcatrow = array();
}
$data_points[][] = [
'provider_id' => $row['provider_id'],
'provider_name' => $row['provider_name'],
'sub_name' => $subcatArray
];
// array_push("totalRow",$data_points, $point);
}
echo json_encode($data_points);
PHP CODE END
you desired output json is an invalid json. you can check this json out valid or not in the http://jsonviewer.stack.hu. json or xml has it's own patterns.
So i found out that my desired output is invalid. Sorry I'm new to PHP.
I removed this line
$data_points["rowcount"] = $rowcount;
and made this
$data_points[][] = [
'provider_id' => $row['provider_id'],
'provider_name' => $row['provider_name'],
'sub_name' => $subcatArray
];
into this
$data_points["Rows"][] = [
'provider_id' => $row['provider_id'],
'provider_name' => $row['provider_name'],
'sub_name' => $subcatArray
];
I got the output pretty much close to what i need. Thanks guys!
I've looked on the web but all I can find is how to echo data from arrays, but I need to add to them. This array is multidimensional so I need to add an array to an array all the time. How do I do this?
Heres the code:
<?php
$data = array(
"contacts" => array(
array(
'id'=> "1",
'catagory'=> "LifeStyle",
'title'=> "Some Cool Title",
'url'=> "http://example.com",
),
)
);
$sql = mysql_query("SELECT * FROM magazines WHERE category = '$cat'");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$cat = $row["category"];
$title = $row["title"];
$url = $row["url"];
// add to array
// array(
// 'id'=> "$id",
// 'catagory'=> "$cat",
// 'title'=> "$title",
// 'url'=> "$url",
// ),
}
mysql_close();
echo json_encode($data);
?>
Just do this...
while($row = mysql_fetch_array($sql)){
$data['contacts'][] = $row;
}
Or this...
while($row = mysql_fetch_array($sql)){
array_push($data['contacts'], $row);
}
Then a print_r will show you your array...
print_r($data);
Instead of doing SELECT * select only the fields you want to push into the array. Something like this
<?php
$sql = mysql_query("SELECT id, category, title, url FROM magazines WHERE category = '$cat'");
while($row = mysql_fetch_array($sql)){
$data['contacts'][] = $row;
}
?>
I would like to inner join to tables with sql like this:
$check_unscored = "select * from [user]
INNER JOIN [tenderrc]
on [user].[id] = [tenderrc].[userid]";
$result_unscored = mssql_query($check_unscored, $s);
while ($record_unscored = mssql_fetch_array($result_unscored))
{
$id = $record_unscored['id'];
$name = $record_unscored['name'];
$userid = $record_unscored['userid'];
$touser = $record_unscored['touser'];
$id = $record_unscored['id'];
$username = $record_unscored['username'];
$password = $record_unscored['password'];
}
I observed that a key name column called "id" exists in both table, and therefore $id = $record_unscored['id']; would be overwritten;
I tried $id = $record_unscored['user.id']; but it's not working! Actually how shall I specify the table name as well?
Use aliases:
$check_unscored = "
SELECT
u.id
, u.name
, u.userid
, u.touser
, t.id AS tender_id --- column alias
--- to remove duplicate names
, t.username
, t.password
FROM [user] AS u --- table alias her
INNER JOIN [tenderrc] AS t --- and here
ON u.[id] = t.[userid]
";
you could create a alias field etc
if you wont id from user you could use something like this
select [user].[id] as 'userid', [tenderrc].[*] from ...
I come from mySql world but i think its managable this way
and later access it this way
$id = $record_unscored['userid'];
You could do this using this function
http://hu2.php.net/manual/en/function.mssql-fetch-field.php
which can provide "column_source - the table from which the column was taken".
Example code:
$result = mssql_query("....");
$check_unscored = "
SELECT *
FROM [user]
INNER JOIN [tenderrc] ON [user].[id] = [tenderrc].[userid]";
$rows = fetch_multitable( mssql_query( $check_unscored, $s ));
foreach( $rows as $row ){
echo $row->user['id'];
echo $row->tenderrc['id'];
}
function fetch_multitable( $result ){
$fields = mssql_num_fields( $result );
$infos = array();
for( $i=0; $i < $fields; $i++ ){
$fieldinfo = mssql_fetch_field( $r, $i );
$infos[ $i ] = (object)array(
'table'=> $fieldinfo -> column_source
,'name' => $fieldinfo -> name
);
}
$rows = array();
while( $raw = mssql_fetch_row( $r )){
$row = array();
foreach( $raw as $i => $v ){
$info = $infos[ $i ];
$row[ $inf->table ][ $inf->name ] = $v;
}
$rows[] = (object)$row;
}
return $rows;
}