user3310429
user3310429

Reputation: 59

How to transform some PHP lines to function and call?

I get this lines on web and just created a function name to that. The problem is: it is not working anymore and I dont know why, already tried things but nothing. Can someone help me please?

Code:

 <?php
    require 'PDOClasses/connection.php';
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    function ShowCategories() {
        $query = $db->query("SELECT id, name FROM categories ORDER BY name ASC");
        $option = '';
        while($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $option .= '<option value = "'.$row['id'].'">'.$row['name'].'</option>';
        }
    }
?>

This is how I used to call:

<select id="category" name="category" tabindex="16" required>
   <?php echo $option; ?>
</select>

This is how I'm calling now:

<select id="category" name="category" tabindex="16" required>
   <?php ShowCategories(); ?>
</select>

Upvotes: 1

Views: 50

Answers (4)

user3310429
user3310429

Reputation: 59

It is working like this

    <?php
    function ShowCategories() {
        require 'PDOClasses/connection.php';
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        $query = $db->query("SELECT id, name FROM categories ORDER BY name ASC");
        $option = '<option value="">Selecione uma categoria</option>';
        while($category = $query->fetch(PDO::FETCH_ASSOC)) {
            $option .= '<option value = "'.$category['id'].'">'.$category['name'].'</option>';
        }
        echo $option;
    }
<?php ShowCategories(); ?>

Upvotes: 1

Marina
Marina

Reputation: 491

$db is undefined in your function. Either pass it as argument or use "global"

and add echo $option in the end

Upvotes: 2

Pratik Soni
Pratik Soni

Reputation: 2588

The problem here is you are not printing content to the HTML Page. So you after generating HTML string you will need to echo the string.

your code will look like this.

<?php
function ShowCategories() {
        $query = $db->query("SELECT id, name FROM categories ORDER BY name ASC");
        $option = '<option value=''>--Please select--</option>';
        while($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $option .= '<option value = "'.$row['id'].'">'.$row['name'].'</option>';
        }
        echo $option;
    }
?>

in HTML now you can use the existing syntax.

<select id="category" name="category" tabindex="16" required>
   <?php ShowCategories(); ?>
</select>

Upvotes: 2

Pradeepta
Pradeepta

Reputation: 458

You are neither returning the value inside the function or echoing it.Do it like this

function ShowCategories() {
        $query = $db->query("SELECT id, name FROM categories ORDER BY name ASC");
        $option = '';
        while($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $option .= '<option value = "'.$row['id'].'">'.$row['name'].'</option>';
        }
        return $option;
    }

Then while calling

<select id="category" name="category" tabindex="16" required>
   <?php echo ShowCategories(); ?>
</select>

or you can directly echo the $option inside the function itself.Like

echo $options;

instead of returning it.

Upvotes: 3

Related Questions