0

Question: How can I tie together my query with my function? I would like to print my list, but I am not sure how to connect the two. Also, am I using "entries" correctly below?

/* data handling */
$result = mysql_query("SELECT * FROM Items LEFT JOIN Categories on Categories.CategoryID = Items.FK_CategoryID WHERE Items.FK_UserID = $_SESSION[user_id] ORDER BY CategorySort, CategoryName ASC, ItemSort, ItemTitle");

/* output logic */
function render_list($ItemTitle, array $entries)
{
    echo '<ul><li>' . $ItemTitle . '<ul>';
    foreach($entries as $entry)
    {
        echo '<li>' . $entry['ItemID'] . '</li>';
    }
    echo '</ul></li></ul>';
}

render_list();

Do I need to use a while clause?

    // loop through topics
    while($row = mysql_fetch_array($result)) {
        render_list;
    }

1 Answer 1

1

Yeah, replace your foreach loop with that while loop. Take that "type" (array) out of the function definition though, you don't need to do that in loosely typed php language. Here's a rework of the code you have:

/* output logic */
function render_list($ItemTitle, $entries)
{
    echo '<ul><li>' . $ItemTitle . '<ul>';
    while($entry = mysql_fetch_array($entries))
    {
        echo '<li>' . $entry['ItemID'] . '</li>';
    }
    echo '</ul></li></ul>';
}

$result = mysql_query("SELECT * FROM Items LEFT JOIN Categories on Categories.CategoryID = Items.FK_CategoryID WHERE Items.FK_UserID = $_SESSION[user_id] ORDER BY CategorySort, CategoryName ASC, ItemSort, ItemTitle");

render_list('My Title', $result);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Shredder: Thanks for the post. My output is supposed to be a Category with items underneath and it is supposed to print multiple categories (the end result being a series of unordered lists). The logic you supplied results in a single unordered list with all items. Is there a way to adjust?
@webdude77 add a string in there to store the category and on the next loop iteration, check if the new record is of the same category or not. I'll update the code to reflect that soon, I can't now tho, gtg. GL

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.