somdow
somdow

Reputation: 6318

Make variables available inside of a function.(PHP)

Im building an application to track certain information about the clients we deal with like name/date/hrs worked/phone number etc. This information is stored in a db. Because we have different departments like SEO/WEB/Sales etc, and different people within these teams, the app provides different ways to filter the information depending on the filter button pressed.

When someone presses a "filter button", in this example, lets say they pressed the "view by department" button, it takes them to actual hardcoded pages. As an example:

viewSeoAccs.php

viewWebAccs.php

ViewSalesAccs.php

And in these pages i have queries which pull the information based on the filter pressed but the html is the same. Now here comes the problem.

I have many different pages(based on filters) and every time there is an edit to be made to the html, i have to go into EVERY PHP page to implement the changes.

What i want to do is create a function that spits out the html for me. I have gotten about half way and i know the problem, just cant seem to find a solution.

Here is some code.

In my functions.php file, i have a function called "htmlBlockTEST" that has this code.

   EXAMPLE: (code chopped for easy reading)
    <?php
        function htmlBlockTEST(){

                echo '' ?>


    <h2 class="accName fl"><?php echo $row['company_name']; ?></h2>
    <div class="<?php echo $row['acc_risk']; ?>"> Risk Level. </div>


    //ALOT MORE CODE goes here lol.

    <?php
        }
    ?>

This is in the header and bought in via "include_once('functions.php'). Under this, i have specific variables that pull in the queried data. (example below)

$pullAllAccounts = "SELECT * FROM tlm_accounts ORDER BY company_name ASC;";
$pullAllAccountsDoIt = mysqli_query($c2d, $pullAllAccounts) or die ("could not pull WEB team data" . mysqli_error($c2d));
?>

now i loop through the db and display the information like so:

<?php

while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
$compName = $row['company_name'];


?>


<?php htmlBlockTEST(); ?>

<?php 

    }

?>

In this code directly above, where the function call "htmlBlockTEST" is, is where the problem is. Since The variables which hold the queries are outside the function, I'm assuming that they aren't being passed into the function. I dont want to put them inside the function because the HTML is the same throughout all the pages, but not ALL data.

I need the variables that hold for example $row['company_name'] to be also available inside the function so that it doesn't throw "undefined variable" errors.

How can i make this happen? What is the best way to get these variables in the while loop(or otherwise) to be available inside the function???

PS, ive google and found things like $GLOBALS['x'] etc but from what ive read, its not the best way or easiers and overall im confused on how to even use it.

Any help is greatly appreciated. Thanks in advanced.

Upvotes: 0

Views: 128

Answers (1)

Reza Mamun
Reza Mamun

Reputation: 6189

Option 1:

while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
    htmlBlockTEST($row);
}

Option 2:

global $row;
while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
    $compName = $row['company_name'];
    htmlBlockTEST();
}

//and in your htmlBlockTEST() function just right this:
function htmlBlockTEST(){
    global $row; ?>
    <h2 class="accName fl"><?php echo $row['company_name']; ?></h2>
    <div class="<?php echo $row['acc_risk']; ?>"> Risk Level. </div>

    <?php
    //ALOT MORE CODE goes here lol.
}
?>

You have some more options of course.

Upvotes: 2

Related Questions