1

i'm learning php.

i have a function with mysql query select.

than i use it with foreach list..

but it work me only if the foreach is inside the function. i don't know how to get it work outside the function..

what i'm doing wrong?

working code - https://phpbox.info/d3GCP

no working code:

function volaco ()
{
$query = $db->getQuery(true);

$query = "select a.id, count(i.id) as all_items, a.name,  SUM(i.state = '1') published, SUM(i.state = '0') unpublished"
    . " FROM item as i"
    . " JOIN application a ON a.id = i.application_id"
    . " group by i.application_id";

$db->setQuery($query);

$apps= $db->loadObjectList();

}

$apps = volaco();
?>

    <table >
        <?php if (count($apps)) : foreach ($apps as $app) : ?>
            <tr >
                <td  width="40%"><?php echo $app->name; ?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->all_items;?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->published; ?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->unpublished; ?></td>
            </tr>
        <?php endforeach; else : ?>
<?php endif; ?>
    </table>

thanks a lot

2
  • 1
    You need to return your variable in the function Commented Sep 15, 2015 at 12:06
  • return results form your function then you can get it like $apps = volaco(); Commented Sep 15, 2015 at 12:11

3 Answers 3

1

You function volaco() need return type

return $apps= $db->loadObjectList();

Read Returning values

Sign up to request clarification or add additional context in comments.

Comments

0

Add a return to your volaco() function, $apps is out of the scope.

function volaco ()
        {
        $query = $db->getQuery(true);

        $query = "select a.id, count(i.id) as all_items, a.name,  SUM(i.state = '1') published, SUM(i.state = '0') unpublished"
            . " FROM item as i"
            . " JOIN application a ON a.id = i.application_id"
            . " group by i.application_id";

        $db->setQuery($query);

        return $db->loadObjectList();
        ?>

Comments

0

Try this:

function volaco ()
{
$query = $db->getQuery(true);

$query = "select a.id, count(i.id) as all_items, a.name,  SUM(i.state = '1') published, SUM(i.state = '0') unpublished"
    . " FROM item as i"
    . " JOIN application a ON a.id = i.application_id"
    . " group by i.application_id";

$db->setQuery($query);

$apps= $db->loadObjectList();
return $apps; 

}

$apps = volaco();
?>

    <table >
        <?php if (count($apps)) : foreach ($apps as $app) : ?>
            <tr >
                <td  width="40%"><?php echo $app->name; ?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->all_items;?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->published; ?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->unpublished; ?></td>
            </tr>
        <?php endforeach; else : ?>
<?php endif; ?>
    </table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.