M
M
Msim2015-05-22 15:03:15
PHP
Msim, 2015-05-22 15:03:15

How to display the list correctly?

<?php
    require(dirname(__FILE__).'/config/config.inc.php');
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $formCategory = $_POST['formCategory'];
    
    }
    $sql = 'SELECT name, link_rewrite FROM '._DB_PREFIX_.'category_lang where id_lang = 1';
    function getSimpleProducts($id_lang, $context = null, $fc) {
    
        $sql = 'SELECT p.`id_product`, pl.`name`, p.`id_category_default`, p.`link_rewrite`
    				FROM `' . _DB_PREFIX_ . 'product_shop` p
    				' . Shop::addSqlAssociation('product_shop', 'p') . '
    				LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (p.`id_product` = pl.`id_product` ' . Shop::addSqlRestrictionOnLang('pl') . ')
    				WHERE  pl.`id_lang` = ' . (int) $id_lang . ' and p.`id_category_default` = '.(int) $fc.'
    				ORDER BY pl.`name`
    				limit 100';
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
    }
    function getCategory(){
    
        $result = 'SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
    				FROM `' . _DB_PREFIX_ . 'category` c
    				' . Shop::addSqlAssociation('category', 'c') . '
    				LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` =1)
    				LEFT JOIN `' . _DB_PREFIX_ . 'category_group` cg ON (cg.`id_category` = c.`id_category`)
    				WHERE (c.`active` = 1 or c.`id_category` = 1)
    				ORDER BY `level_depth` ASC';
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($result);
    }
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
    <select name="formCategory">
        <?php
        $s = getCategory();
            foreach ($s as $row)
        echo '<option value='.$row['id_category'].'>'.$row['name'].'</option>'."\n";
        ?>
    </select>
    <input type="submit" value="Отправить">
        <p>
            <?php
        $prod = getSimpleProducts(1,null,$formCategory);
          foreach($prod as $p)
             echo $p['name']." ".$p['id_category_default']."<br/>";
        ?>
        </p>
    </body>
    </html>

Yesterday I wrote everything worked today swears at
Invalid argument supplied for foreach() in 57
It seems that the error is that the required value does not come, but yesterday everything was fine, but today the stars are not the same.
when checking for an array (select category) returns true, and the second request for getSimpleProducts does not return

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WebDev, 2015-05-22
@Msim

Well, arrange the var dumps, look at which ball the array stops coming. And always check if the array has arrived before running foreach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question