将PHP查询结果转换为JSON

将PHP查询结果转换为JSON

本文介绍了将PHP查询结果转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,用于查询多个表以获取有关活动的信息.我想将查询结果转换为数组,并在需要时调用它.我的问题是:此查询可以转换为数组吗?

I have the following code that queries multiple tables to get information about activities. I would like to convert the query result into array and call it whenever needed.My question is: can this query be converted into array?

<?php
function view_full_activity($activity_field){
            global $connection;

        $contact_id = $_REQUEST['contact_id'];
        $activity_id = $_REQUEST['activity_id'];
        $get = "SELECT * FROM activity_base WHERE activity_id = '$activity_id' "
                                    or die("Error: ".mysqli_error($connection));
        $query = mysqli_query($connection, $get);
    //Get activity base information
        while ($activity = mysqli_fetch_array($query)){
            $activity_related_to_id = $activity ['activity_related_to_id'];
            $activity_id = $activity['activity_id'];
            $activity_type_id = $activity['activity_type_id'];
      $activity_entity_type_id = $activity['activity_entity_type_id'];
            $activity_title = $activity['activity_title'];
            $activity_due_date = $activity['activity_due_date'];
            $activity_created_by = $activity['activity_created_by'];
            $activity_created_on = $activity['activity_created_on'];
            $activity_status_code_id = $activity['activity_status_code_id'];
            $activity_type_icon;
            $activity_due_date = $activity['activity_due_date'];
      $activity_create_date = date("m-d-Y", strtotime($activity['activity_created_on']));

      //Reverse Date
      $activity_due_date = date("m-d-Y ", strtotime($activity_due_date));

            if ($activity_type_id == "1"){
                $activity_type_icon = "fa fa-envelope";
            }else if ($activity_type_id == "3"){
                $activity_type_icon = "fa fa-suitcase";
            }else if ($activity_type_id == "2"){
                $activity_type_icon = "fa fa-phone";
            }

      if ($activity_entity_type_id == "1") {
        $acitvity_url = "acct_id";
        $acitivty_is_for ="accounts_base";
        $page_url = "account-profile.php";
      }else if ($activity_entity_type_id == "2") {
        $acitvity_url = "contact_id";
        $acitivty_is_for ="contacts";
        $page_url = "contact-profile.php";
      }else if ($activity_entity_type_id == "3") {
        $acitvity_url = "contact_id";
        $acitivty_is_for ="contacts";
        $page_url = "contact-profile.php";
      }
          //Get detailed activity information
      //If activity is Email
    if ($activity_type_id == "1") {
        $email_details = email_activity_details($activity_id);
        while ( $email = mysqli_fetch_assoc($email_details)) {
          $activity_details = nl2br($email['email_message']);

        }
    }else if ($activity_type_id == "2") {
        $call_details = call_activity_details($activity_id);
        while ( $call = mysqli_fetch_assoc($call_details)) {
          $activity_details = $call['call_details'];
          }
        }else if ($activity_type_id == "3") {
        $meeting_details = meeting_activity_details($activity_id);
        while ( $meeting = mysqli_fetch_assoc($meeting_details)) {
          $activity_details = $meeting['meeting_details'];
          }
        }
        //Get creator user info
          $user_query = get_user_info($activity_created_by);
          while ($user = mysqli_fetch_array($user_query)) {
            $activity_created_by = $user['user_full_name'];
          }
    }

    }



          ?>

推荐答案

确定:

创建一个名为$activity_store的变量,并使用while循环以与从数据库中输出数据相同的方式来设置数据.

Create a variable called $activity_store and use the while loop to set the data much in the same way it comes out of the database.

//Get activity base information
$activity_store = array();
while ($activity = mysqli_fetch_array($query)){
    $activity_store['activity_related_to_id'] = $activity['activity_related_to_id'];

如果您需要分别存储有关从数据库中提取的每个活动的信息,则可以添加索引并对其进行递增

If you need to store information about each activity pulled from the database separately you could add an index and increment it

//Get activity base information
$activity_store = array();
$i = 0;
while ($activity = mysqli_fetch_array($query)){
    $activity_store[$i]['activity_related_to_id'] = $activity['activity_related_to_id'];
    ...
    $i++; //increments $i by 1
}

请注意,稍后在代码中引用$activity_type_id时,现在需要引用'$ activity ['activity_type_id']`

Just be aware that later in your code when you refer to $activity_type_id you'll now need to refer to '$activity['activity_type_id']`

就用php返回JSON而言,您可以使用json_encode将变量编码为JSON格式:

In terms of returning JSON in php you can use json_encode to encode a variable into a JSON format:

$json_array = json_encode($variable);

这篇关于将PHP查询结果转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:05