-2

How to get the array string values from a function and echo them separately in a table which is located in another page?

functions.php:

function get_program()
    {
        $connection = db_connect();
        $username = $_SESSION['username'];
        $query = "SELECT * FROM utilizatori WHERE username = '$username'";
        $results = mysqli_query($connection, $query);
        $array = mysqli_fetch_array($results, MYSQLI_ASSOC);
        return $dateOne = $array['weekOneFirst'];
        return $dateTwo = $array['weekTwoFirst'];
    }

index.php:

<?php require_once 'functions.php'; ?>

<div>
<?php get_program(); ?>
<table>
  <tr>
    <th>
    Week 1
    </th>
    <th>
    Week 2
    </th>
  </tr>

  <tr>
    <td>
    <?php echo $dateOne; ?> // date 1 from array string here
    </td>
    <td>
    <?php echo $dateTwo; ?> // date 2 from array string here
    </td>
  </tr>
</table>
</div>

It returns me errors:

Notice: Undefined variable: dateOne in ...

Notice: Undefined variable: dateTwo in ...

Community
  • 1
  • 1
Bogdan2305
  • 17
  • 12

1 Answers1

1

There are other ways to do what you are doing, but this is probably going to serve you best going forward and be more flexible:

In get_program() you only want 1 return and just return the array:

return $array;

Then in the index.php:

<?php $result = get_program(); ?>

html here

<?php echo $result['weekOneFirst']; ?>
<?php echo $result['weekTwoFirst']; ?>

Now you can use the function for any columns in the query. If you want a function that only returns those two columns then:

$query = "SELECT weekOneFirst, weekTwoFirst FROM utilizatori WHERE username = '$username'";
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87