Sergio Angel
Sergio Angel

Reputation: 37

How to display a php variable in another php file?

I am working with this php code which makes a query and then displays the information in a table. There is a column returned from this query called 'queue' which I want to store in a variable called '$queue_tittle' and then display this variable in index.php but when I try to do so, the variable is empty, it does not display any information.

query.php

<?php
session_start();
$queue_tittle = "";

function fill(){
    
    if (isset($_GET['vid'])){
        $vid = $_GET['vid'];

        if (!$conn = new mysqli("localhost", "root", "", "zendesk_data")){
            $output="Connection failed: " . $conn->connect_error;      
            } 
        else {
            $sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach, queue FROM $vid";
            if ( $result = $conn->query($sql) ){
                if ($result->num_rows > 0) {
                    $output="<table class='queue_table'> 
                    <tr align='left'>
                    <th>Status</th>
                    <th>ID</th>
                    <th>Subject</th>
                    <th>Requester</th>
                    <th>Requested</th>
                    <th>Requested Updated</th>
                    <th>Service</th>
                    <th>Next SLA Breach</th></tr>";
                    while($row = $result->fetch_assoc()) {
                        $queue_tittle = $row["queue"];
                        $output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
                        $row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
                    }
                    $output.="</table>";
                } else { 
                    $output= "0 results"; 
                }
            } else {
                    $output="Error en la consulta: ".$conn->error;
            }
            $conn->close();
        }
        echo $output;
        }    
    } 
?>

index.php

<div id="inner_cont">
   <div id="queue">
      <?php
         include '../utilities/query.php';
         fill();
         echo $queue_tittle;
       ?>  
  </div>

I know this is may be an easy question but I have tried to follow online tutorials to solve this but nothing seems to work for my case. All help will be highly appreciated, thanks beforehand.

Upvotes: 0

Views: 52

Answers (2)

AKX
AKX

Reputation: 168966

You could just return the title from the function.

function fill() {
    $queue_tittle = "";
    // ...
        echo $output;
    }
    return $queue_tittle;
}

and

<?php
include '../utilities/fill.php';
$queue_tittle = fill();
echo $queue_tittle;
?>

If you need to return more bits of data, you can wrap them in an associative array.

Upvotes: 1

user2182349
user2182349

Reputation: 9782

You need to add global into fill to access $queue_tittle - like so:

function fill(){
    global $queue_tittle;

Upvotes: 0

Related Questions