I have get Serialised from Nestable, but I don't know how to write php code update my database like wordpress:
$data = '[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]';
function extract_value($value,$order=0){
if(is_array($value)){
foreach($value as $k => $v){
echo "UPDATE vod_page_menu SET ordering = '$order', parent_id = '$v' WHERE id = '$v'; <br/>";
extract_value($v,$order+1);
}
}
}
$json = json_decode($data,true);
extract_value($json);
You need to first decode the JSON, then you can use it in PHP:
$data = json_decode($data, true);
foreach ($data as $id => $value) {
...
}
function extract_value($array,$parent=0,$i=1){
foreach($array as $k => $v) {
$id = $array[$k]['id'];
echo "UPDATE vod_page_menu SET ordering = '$i', parent_id = '$parent' WHERE id = '$id'; <br/>";
if(isset($array[$k]['children'])){
$children = $array[$k]['children'];
foreach($children as $key => $obj){
$child = $children[$key]['id'];
$i++;
echo "UPDATE vod_page_menu SET ordering = '$i', parent_id = '$id' WHERE id = '$child'; <br/>";
if(isset($children[$key]['children']))
extract_value($children[$key]['children'],$child,++$i);
}
}
$i++;
}
}
Related
I have a db table (form table for commercial propose) , the table is already filed with default rows, I want to execute multiple update query to change default values to the inserted values from from submit with post method in code igniter.. I have built a function save_report().
I have tried to use $this->db->update_batch(); , its only perform for the first foreach loop ..
function save_report(){
$data['old_circle'] = $this->input->post('old_circle');
$data['sold'] = $this->input->post('sold');
$data['diffrent_come'] = $this->input->post('diffrent_come');
$data['total'] = $this->input->post('total');
$data['new_circle'] = $this->input->post('new_circle');
$data['sold_start_month'] = $this->input->post('sold_start_month');
$data['bought_start_month'] = $this->input->post('bought_start_month');
$data['paid_start_month'] = $this->input->post('paid_start_month');
foreach($data as $key=>$value){
$comp = array('sold_start_month','bought_start_month','paid_start_month');
if(in_array($key,$comp)){
$this->db->where(array('report_id'=>1,'name' =>$key));
$this->db->update('reports_det',array('value'=>$value));
}
$update_array[] = array(
'name' =>$key,
'recived' =>$value['recived'],
'paid' =>$value['paid'],
'recived_bill'=>$value['recived_bill'],
'paid_bill' =>$value['paid_bill']
);
$this->db->where(array('report_id'=>1));
$this->db->update_batch('reports_det',$update_array,'name');
}
redirect('c_panel/reports_det_mangment/1');
}
thanks a lot ..
sorry guys for wasting your time , yesterday i was unfocused during work so today am really focus haha, the problem was i did not set foreach loop for every post array the final answer is here :
function save_report(){
$report_id = $this->input->post('rid');
if(count($_POST) > 0){
$data['old_circle'] = $this->input->post('old_circle');
$data['sold'] = $this->input->post('sold');
$data['diffrent_come'] = $this->input->post('diffrent_come');
$data['total'] = $this->input->post('total');
$data['new_circle'] = $this->input->post('new_circle');
$data['sold_start_month'] = $this->input->post('sold_start_month');
$data['bought_start_month'] = $this->input->post('bought_start_month');
$data['paid_start_month'] = $this->input->post('paid_start_month');
foreach($data['old_circle'] as $key=>$value){
$this->db->where(array('report_id'=>$report_id,'name' =>'old_circle'));
$this->db->update('reports_det',array($key=>$value));
}
foreach($data['sold'] as $key=>$value){
$this->db->where(array('report_id'=>$report_id,'name' =>'sold'));
$this->db->update('reports_det',array($key=>$value));
}
foreach($data['diffrent_come'] as $key=>$value){
$this->db->where(array('report_id'=>$report_id,'name' =>'diffrent_come'));
$this->db->update('reports_det',array($key=>$value));
}
foreach($data['total'] as $key=>$value){
$this->db->where(array('report_id'=>$report_id,'name' =>'total'));
$this->db->update('reports_det',array($key=>$value));
}
foreach($data['new_circle'] as $key=>$value){
$this->db->where(array('report_id'=>$report_id,'name' =>'new_circle'));
$this->db->update('reports_det',array($key=>$value));
}
foreach($data['sold_start_month'] as $value){
$this->db->where(array('report_id'=>$report_id,'name' =>'sold_start_month'));
$this->db->update('reports_det',array('value'=>$value));
}
foreach($data['bought_start_month'] as $value){
$this->db->where(array('report_id'=>$report_id,'name' =>'bought_start_month'));
$this->db->update('reports_det',array('value'=>$value));
}
foreach($data['paid_start_month'] as $value){
$this->db->where(array('report_id'=>$report_id,'name' =>'paid_start_month'));
$this->db->update('reports_det',array('value'=>$value));
}
}
redirect('c_panel/reports_det_mangment/'.$report_id);
}
I have an array:-
$resolution = array();
foreach ($report as $key => $val) {
$queryToGetRes = "SELECT resolution,ticket_no FROM techzilla.bugs WHERE bug_id = '$val' ";
$sqlResult = mysql_query($queryToGetRes) or die (mysql_error());
while ($resolutionAns = mysql_fetch_array($sqlResult)) {
$resolution[$resolutionAns['resolution']][] = $resolutionAns['ticket_no'];
}
}
I need to print the array which stores the resolution and ticket no.
try something like this:
echo "<pre>" . print_r($resolution, true) . "</pre>";
For debuging purpuses you should use
var_dump($resolution);
If you want to use it
foreach ($resolution as $r => $ids){
echo "Resolution : $r\n";
foreach ($ids as $id) {
echo "$id\n";
}
}
I have been trying to convert the fields from a mysql_fetch_array (that are urlencoded) to urldecode before converting to JSON (json_encode)
Here's what I'm working with that doesn't work:The output is still urlencoded
$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_fetch_array(mysql_query($query));
foreach($result as $value) {
$value = urldecode($value);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));
Any ideas?
yeah....! you're not updating $result with the value returned by the function. $value needs to be passed by reference.
foreach($result as &$value) {
$value = urldecode($value);
}
or
foreach($result as $i => $value) {
$result[$i] = urldecode($value);
}
when you do this...
foreach($result as $value) {
$value = urldecode($value);
}
The result of the function is lost at at iteration of the foreach. You're trying to update each value stored in $result but that's not happening.
Also take note that the code only fetches one row from your query. I'm not sure if that's by design or not.
Try:
$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_query($query);
$value = array();
while($row = mysql_fetch_array($result))
$value[] = urldecode($row);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));
Hi all' I have a page into PHP where I retrieve XML data from a server and I want to store this data into an array.
This is my code:
foreach ($xml->DATA as $entry){
foreach ($entry->HOTEL_DATA as $entry2){
$id = (string)$entry2->attributes()->HOTEL_CODE;
$hotel_array2 = array();
$hotel_array2['id'] = $entry2->ID;
$hotel_array2['name'] = utf8_decode($entry2->HOTEL_NAME);
$i=0;
foreach($entry2->ROOM_DATA as $room){
$room_array = array();
$room_array['id'] = (string)$room->attributes()->CCHARGES_CODE;
$hotel_array2['rooms'][$i] = array($room_array);
$i++;
}
array_push($hotel_array, $hotel_array2);
}
}
In this mode I have the array hotel_array which all hotel with rooms.
The problem is that: into my XML I can have multiple hotel with same ID (the same hotel) with same information but different rooms.
If I have an hotel that I have already inserted into my hotel_array I don't want to insert a new array inside it but I only want to take its rooms array and insert into the exisiting hotel.
Example now my situation is that:
hotel_array{
[0]{
id = 1,
name = 'test'
rooms{
id = 1
}
}
[0]{
id = 2,
name = 'test2'
rooms{
id = 100
}
}
[0]{
id = 1,
name = 'test'
rooms{
id = 30
}
}
}
I'd like to have this result instead:
hotel_array{
[0]{
id = 1,
name = 'test'
rooms{
[0]{
id = 1
}
[1]{
id = 30
}
}
}
[0]{
id = 2,
name = 'test2'
rooms{
id = 100
}
}
}
How to create an array like this?
Thanks
first thing is it helps to keep the hotel id as the index on hotel_array when your creating it.
foreach ($xml->DATA as $entry){
foreach ($entry->HOTEL_DATA as $entry2){
$id = (string)$entry2->attributes()->HOTEL_CODE;
$hotel_array2 = array();
$hotel_array2['id'] = $entry2->ID;
$hotel_array2['name'] = utf8_decode($entry2->HOTEL_NAME);
$i=0;
foreach($entry2->ROOM_DATA as $room){
$room_array = array();
$room_array['id'] = (string)$room->attributes()->CCHARGES_CODE;
$hotel_array2['rooms'][$i] = array($room_array);
$i++;
}
if (!isset($hotel_array[$hotel_array2['id']])) {
$hotel_array[$hotel_array2['id']] = $hotel_array2;
} else {
$hotel_array[$hotel_array2['id']]['rooms'] = array_merge($hotel_array[$hotel_array2['id']]['rooms'], $hotel_array2['rooms']);
}
}
}
Whilst this is the similar answer to DevZer0 (+1), there is also quite a bit that can be done to simplify your workings... there is no need to use array_merge for one, or be specific about $i within your rooms array.
$hotels = array();
foreach ($xml->DATA as $entry){
foreach ($entry->HOTEL_DATA as $entry2){
$id = (string) $entry2->attributes()->HOTEL_CODE;
if ( empty($hotels[$id]) ) {
$hotels[$id] = array(
'id' => $id,
'name' => utf8_decode($entry2->HOTEL_NAME),
'rooms' => array(),
);
}
foreach($entry2->ROOM_DATA as $room){
$hotels[$id]['rooms'][] = array(
'id' => (string) $room->attributes()->CCHARGES_CODE;
);
}
}
}
Just in case it helps...
And this :)
$hotel_array = array();
foreach ($xml->DATA as $entry)
{
foreach ($entry->HOTEL_DATA as $entry2)
{
$hotel_code = (string) $entry2->attributes()->HOTEL_CODE;
if (false === isset($hotel_array[$hotel_code]))
{
$hotel = array(
'id' => $entry2->ID,
'code' => $hotel_code,
'name' => utf8_decode($entry2->HOTEL_NAME)
);
foreach($entry2->ROOM_DATA as $room)
{
$hotel['rooms'][] = array(
'id' => (string)$room->attributes()->CCHARGES_CODE,
);
}
$hotel_array[$hotel_code] = $hotel;
}
}
}
I retrieved MySQL data from three tables in XML using PHP:
$table_first = 'recipe';
$query = "SELECT * FROM $table_first";
$resouter = mysql_query($query, $conn);
$doc = new DomDocument('1.0');
$root = $doc->createElement('recipes');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($resouter)){
$outer = $doc->createElement($table_first);
$outer = $root->appendChild($outer);
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $outer->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}// foreach
//while
$table_second='instructions';
$query="SELECT instructions.instruction_id,instructions.instruction_text FROM $table_second where rec_id = ".$row['rec_id'];
$resinner=mysql_query($query, $conn);
$inner = $doc->createElement($table_second);
$inner = $outer->appendChild($inner);
while($row1 = mysql_fetch_assoc($resinner)){
$inner1=$doc->createElement('instruction');
$inner1=$inner->appendChild($inner1);
foreach ($row1 as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $inner1->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} // foreach
}// while
$table_third='ingredients';
$query="SELECT ingredients.ingredient_id,ingredients.ingredient_name,ingredients.ammount FROM $table_third where rec_id = ".$row['rec_id'];
$resthird=mysql_query($query, $conn);
$inner=$doc->createElement($table_third);
$inner=$outer->appendChild($inner);
while($row=mysql_fetch_assoc($resthird)){
$inner2=$doc->createElement('ingredient');
$inner2=$inner->appendChild($inner2);
foreach($row as $fieldname=> $fieldvalue)
{
$child=$doc->createElement($fieldname);
$child=$inner2->appendChild($child);
$value=$doc->createTextNode($fieldvalue);
$value=$child->appendChild($value);
}
}
}
mysql_close($conn);
$xml_string = $doc->saveXML();
echo $xml_string;
?>
This works perfectly, but now I want to retrieve that data in JSON. I retrieved data from one table using JSON, but how to retrieve the same data from these three tables using JSON instead of XML?
Create arrays in PHP and use json_encode() on your structure.