Hi everybody. I'm trying to print all sub-category items using my own defined PHP function. The purpose of this function is to print all items, which parent value is $parent. There is some thing wrong with this function why it doesn't show information as I wish. To make you understand my aim, suppose that we've items categorized in different groups each has own groups ID. Calling this function I wish to get the all items of same group by feeding of group Id as argument ($parent). Could someone help me to find out the reason why this function doesn’t work, please. Thanks a lot in advance.

<?php
include ('connect.php');
include ('head.html');

echo '<div class="categ"><p class="b">Health</p>';
function retrieve_column($parent){
    $sql = "SELECT item FROM category WHERE parent = $parent ORDER BY item ASC";
    $r = mysqli_query($dbc, $sql);
        if (!$r) {
            echo "Couldn’t make a connection to DB.";
        } else {
            while($row = mysqli_fetch_assoc($r)){
                for ($i=0; $i < mysqli_num_rows($r); $i++) {
                    echo '<a class="text" href="">' . $row['item'][$i] . '</a><br />';
                }
            }
        }
    }

$par = 1;
retrieve_column($par);
mysqli_free_r($r);
mysqli_close($dbc);
?>

Hi,

Sorry, unfortunately I know you tried to explain, but unfortunately I'm still confused. For example, you are saying that you have items categorized in different groups, so for example you have a table category with columns parent and item. So I see you want to fetch a list of items where the parent is a specific thing. Firstly, I would escape $parent in the database so it is parent = '$parent' or you could end up with a MySQL injection attack or worse. Secondly, what is your for loop on line 13 supposed to do? Why can't you just do:

        while($row = mysqli_fetch_assoc($r)){
             echo '<a class="text" href="">' . $row['item'] . '</a><br />';
        }

Thank you so much, Dani. You are exactly right about the loop in line 13. I removed this loop and the result was same. And about $parent variable also you are right. I've tried your suggestion but fixing this didn't get newresult. I gues problem is in sql syntax. But I can't find the fault.

By doing some changes, now it's working correctly. Thanks for your helping Dani and Reverend Jim.

function retrieve_column($parent, $dbc){
Be a part of the DaniWeb community

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