2

system/article.php

<?php 

$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row["articleTitle"];
        echo $row["articleSummary"];
        echo $row["articleContent"];
    }
} else {
    echo "0 results";
}

include 'template/homepage.php';

retrieves articles from the article table.

I have included the homepage.php which is supposed to act as a template.

template/homepage.php

<?php include 'template/common/header.php'; ?>

  <h1>Article Title here</h1>
  <p>articleSummary</p>

<?php include 'template/common/footer.php'; ?>

How do I now pass the retrieved data to the homepage.php to display it on the browser ?

Edit

smarber pointed me to

In the first file:

global $variable;
$variable = "apple";
include('second.php');

In the second file:

echo $variable;

which works. But how do I implement the same with my problem up top?

Daksh B
  • 269
  • 1
  • 8
  • 45

2 Answers2

1

You may do that via GET, Session or Post; But why don't you simply and efficiently define a function and pass those variables to it, just for example:

function displayArticle($title, $summary, $content) {
    displayHeader(); // maybe some concepts you've used in template/common/header.php
    echo "<h1>$title</h1><p>$summary</p><div>$content</div>";
    displayFooter(); // again, what you've provided in footer.php
}
Community
  • 1
  • 1
someOne
  • 1,975
  • 2
  • 14
  • 20
0

Well then, you may do the following:

change the template/homepage.php file to:

<?php
include 'template/common/header.php';

echo "<h1>$articleName</h1>";
echo "<p>$articleSummary</p>";

include 'template/common/footer.php';
?>

and change the system/article.php to:

<?php 
global $articleName;
global $articleSummary;
global $articleContents;

$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $articleName = $row["articleTitle"];
        $articleSummary = $row["articleSummary"];
        $articleContents = $row["articleContent"];

        include 'template/homepage.php';
    }
} else {
    echo "0 results";
}

However, It's so better to create a cleaner and more reusable code using some facilities you have in the programming language, like using functions and classes :)

someOne
  • 1,975
  • 2
  • 14
  • 20
  • thank you, I do not know oop in php. Functions I can manage. You sure I should include 'template/homepage.php' within the while ()? – Daksh B May 09 '15 at 10:06
  • @ManishB Well, it's the logic of your original post, and I just preserved it; if you don't place the `include` there, and place it at the bottom of code, then it will only be carried out once the `while()` loop is over and regardless of the outer `if` statement and only applies for the **last** item fetched by the `fetch_assoc` (which ignores all the preceding data) :) – someOne May 09 '15 at 10:17
  • thanks man, guess will have to look for a better alternative. – Daksh B May 09 '15 at 10:19
  • @ManishB Your welcome, the procedural approach is very easy and intuitive and is the preferred way; – someOne May 09 '15 at 10:23
  • accepted your first answer, a request though, will be glad if you can guide me to an online tutorial, if possible. – Daksh B May 09 '15 at 10:26