Hi,

I am getting this error:

Warning: implode() [function.implode]: Invalid arguments passed in ... on line 28

I am not sure if what I am doing is possible or if I have shanked the code somehow..

if(!isset($_GET['b'])) {
        ///// DISPLAY THE NORMAL PROMOTIONAL HOME PAGE
} elseif($_GET['b'] == 't') {

$sql = "SELECT * FROM brands";
$setBrand=array();
foreach ($conn->query($sql) as $row) {   
$setBrand[]=<<<END
    <div class="brandRowM">
        <div class="brandRow">
            <div class="brand"><a href="#">$row[brand]</a></div><br />
            <div class="productSmPic">
                <ul>
END;
    $sql = "SELECT * FROM products WHERE top = '1' && brand = '".$row['brand']."'";
            foreach ($conn->query($sql) as $info) {   
            $setInfo[]=<<<END
                    <li><a href="product.php"><img src="$info[PFN1]" width="100px" height="60px" /></a>
                        <ul>
                            <li><div class="productSmName">$info[name]</div></li>
                            <li><div class="productSmCode">$info[icode]</div></li>
                            <li><div class="productSmRRP">RRP: $$info[cost]</div></li>
                            <li><div class="productSmQRP">Click to see price</div></li>
                        </ul>
                    </li>
END;
            }
            $setInfo=implode("\r\n",$setInfo);

<<<END
                </ul>    
            </div>        
        </div>
    </div>
END;
    }
    $setBrand=implode("\r\n",$setBrand);

}

I have been trying to get this work for a while now, so any help would be SSOOOO appreciated.

Cheers,

Dani AI

Generated

the warning means implode() is receiving something that is not an array. In your case $setInfo becomes a string after the first implode(), and on the next brand loop you try to treat that string like an array. Also, inside double-quoted heredocs you are using $row[brand] and $info[name], which PHP treats as undefined constants. Use quoted indexes.

@rajarajan2017 is right that you must declare $setInfo as an array, but you also need to reset it for each brand and actually concatenate your pieces. A compact pattern is to build a brand block, implode only the product items, then append the block to the final output.

Example rewrite:

$brands = $conn->query("SELECT * FROM brands");
$html = '';

foreach ($brands as $row) {
    $items = []; // reset per brand
    $products = $conn->query(
        "SELECT name, icode, cost, PFN1 FROM products WHERE top = 1 AND brand = '".$conn->quote($row['brand'], PDO::PARAM_STR)."'"
    );
    foreach ($products as $p) {
        $items[] =
            '<li><a href="product.php"><img src="' . htmlspecialchars($p['PFN1']) . '" width="100" height="60" /></a>' .
            '<ul>' .
                '<li><div class="productSmName">' . htmlspecialchars($p['name']) . '</div></li>' .
                '<li><div class="productSmCode">' . htmlspecialchars($p['icode']) . '</div></li>' .
                '<li><div class="productSmRRP">RRP: $' . htmlspecialchars($p['cost']) . '</div></li>' .
                '<li><div class="productSmQRP">Click to see price</div></li>' .
            '</ul></li>';
    }

    $html .=
        '<div class="brandRowM"><div class="brandRow">' .
        '<div class="brand"><a href="#">' . htmlspecialchars($row['brand']) . '</a></div><br />' .
        '<div class="productSmPic"><ul>' . implode("\n", $items) . '</ul></div>' .
        '</div></div>';
}

echo $html;

Notes:

  • Always do $items = []; inside the brand loop before collecting product rows.
  • Use {$row['brand']}/{$p['name']} or string concatenation to avoid the undefined-constant quirk.
  • implode([]) is safe and returns an empty string, but implode(null) triggers your warning, so initialize your arrays every time.
Member Avatar for Member #334542
$setInfo = array();

The above is not declared

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.