Jump to content

Variable not being passed to include


Plugnz13

Recommended Posts

 

Hi All,

I've been trying to access xml data from an API feed, but it needs a Variable to be passed to an include file without a form. the include is at the header of the page.

in the code below, i'm trying to retrieve the variable $project_add from the $xml_2 feed. $proj_add_ID comes from another feed and is being passed ok. 

foreach ($projects_1 as $proj_1) {

                        $job_no =$proj_1['job_no'];
                        $job_name =$proj_1['job_name'];
                        $job_status =$proj_1['job_status'];
                        $job_staff =$proj_1['job_staff'];
                        $job_due =$proj_1['job_due'];
                        $clr_1 = $colors_1[$job_status];
                        
                        if ($err_2) {
                        echo "cURL Error #:" . $err_2;
                         } else {
                        $_POST['job_id']= $job_no;
                        $xml_2=simplexml_load_string($response_2) or die("Error: Cannot create object");
                        $proj_add = $xml_2->xpath("CustomFields/CustomField[ID ='$proj_add_ID']");                         
                            foreach($proj_add as $item_2) {
                            $project_add = "$item_2->Text";
                        }            
                        echo "<div class='row no-gutters'>";
                        echo "<div class='col-sm bg-white border rounded'><a href='managejob.php?job_id=$job_no' class='text-left ml-1'><strong>$job_no</strong></a></div>";
                        echo "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'>$job_name</p></div>";                         
                        echo "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'></p>$project_add</div>";                        
                        echo "<div class='col-sm-2 bg-white border rounded'>" . state_dropdown_1($job_no, $job_status, $clr_1) . "</div>";                        
                        echo "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'>$job_staff</p></div>"; 
                        echo "<div class='col-sm-2 bg-white border rounded'><form action='CALL_API_PUTTaskDate.php' method='POST'>"
                                . "<input type='hidden' name='task_ID' value ='". $jdata['task_ID'] ."'>"
                                . "<input type='hidden' name='est_min' value ='". $jdata['est_min'] ."'>"
                                . "<input class ='form-control bg-white text-dark font-weight-bold' type='date' name='start_date' value='" . date('Y-m-d', strtotime( $jdata['display_date_1'] )) . "' onchange='this.form.submit()'>"
                                . "</form></div>";
                        echo "<div class='col-sm bg-white border rounded'><p class='text-left ml-1 mt-1'><strong>" . date('d/m/Y', strtotime( $jdata['display_date'])) . "</strong></p></div></div>";
                        
                       }

The include file is like this

/ collect Custom Felds
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of status input field
    $job_no = $_REQUEST['job_id'];
      } 

if (isset($_POST['job_id'])) {
$job_no = $_POST['job_id'];
}

if (isset($_GET['job_id'])) {
$job_no = $_GET['job_id'];
}

$curl_2 = curl_init();

curl_setopt_array($curl_2, array(
  CURLOPT_URL => "https://api.xxx.com/job.api/get/$job_no/customfield?apiKey=xxx&accountKey=xxx",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Accept-Encoding: gzip, deflate",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Host: api.xxx.com",
    "Postman-Token: xxx",
    "User-Agent: PostmanRuntime/7.17.1",
    "cache-control: no-cache"
  ),
));

$response_2 = curl_exec($curl_2);
$err_2 = curl_error($curl_2);

curl_close($curl_2);

The problem is the variable $job_no isn't getting passed to CURLOPT_URL so it's not retrieving the data required. i need to be able to retrieve this data without submitting a form as it's in a foreach loop and the Id is specific to each iteration. is there a way to do this? I have been able to achieve this by putting the include within the div tags that it will be displayed but it just slows down everything too much and hangs. 

Any help will be much appreciated. 

Kind regards,

Michael Bennington.

Link to comment
Share on other sites

it is inefficient to query for data inside of a loop. your goal should be to use the least amount of queries that get the data you want, generally in the order that you want it, then just loop over the data to produce the output. when the query to get data involves going to an external source, such as using curl to read xml data from an api, the problem is even worse, due to the communications and the parsing of the data. you want to reduce the number and size of the communications and you want to reduce the number of times you parse the same data values.

you need to use a data-centric approach, rather than a piecemeal approach to getting data. for the application you are creating, what is all the data you need, and can you get it all using a single query? if not, can you get multiple sets of information (the custom field information) for multiple ids in a single query?

also, how often does any of the source data change, so that you would be able to cache previously fetched and parsed data locally in a database?

 

Link to comment
Share on other sites

Hi mac_gyver,

 

Thanks for your reply.

I am starting to accept that the database cache is the best way forward. I'm connecting to a commercial software that has limited functionality and so I'm spending a lot of time on inefficient work-arounds. Initially, It seemed like doubling up as all the data is available through the api, but there is a significant lag in the parsing of the data, which the database would reduce significantly. I am using a database as well so looks like that is the solution. 

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.