What I’m trying to accomplish is a back-end Admin panel, with multiple forms to edit tables from a database.
I would like to run one SQL query on the page, & refer to it multiple times (possibly creating 3 different loops out of this same query, without having to query it more than once).
I can’t figure out how to make the query work outside of its initial loop, if possible at all.
I already have the SQL statement that would work on all the forms (since basically my forms just query the same 2 tables over & over again), but I can’t use it more than once…
I already tried it, but can’t get it to work.
I just want to run this query once, & simply refer back to it whenever I need to use it again on the page. (Perhaps by creating a new while loop? Tried it, doesn’t work. Use different variables? Doesn’t work. I’m itching to find out what DOES…)
This Admin panel is a single PHP page, divided by compartments based on administrative tasks.
For some idea, it is like so ~
// UPLOAD PHOTO //
(options available are…)
– Upload photo
– Add Photo Title
01. Select Category || SQL QUERY! LOOP – Drop-down list of Categories
02. Select Sub-categories || SQL QUERY! LOOP – Checkbox list of Categories + Sub-categories
– Add Tags
– Add Photo Description / Commentary
// MANAGE CATEGORIES //
(options available are…)
– Create a new Category
03. Delete Multiple Categories || SQL QUERY! LOOP – Checkbox list of Categories + # of entries each
04. Rename Category || SQL QUERY! LOOP – List of Categories
05. Edit Sub-Category || SQL QUERY! LOOP – List of Sub-categories
So on & so forth…
As you can see, all queries made are related to Categories.
I actually have completed a working version of this but one with too many SQL queries on just a single page (for the exact same tables over & over again), it just seems impractical and thoughtless. There has got to be a better way…
The query I’d like to use just once (& the one I’m using right now) is:
$sql = "SELECT category.id, category.name, COUNT(records.category_id) as number
FROM category
LEFT OUTER
JOIN records
ON category.id = records.category_id
WHERE
category.active = 1
GROUP
BY category.id
ORDER
BY category.name";
$result = mysql_query($sql);
while($rows = mysql_fetch_array($result)) {
echo "SAMPLE LOOP" . $rows['id'] . $rows['name'] . $rows['number']; }
Is there a way to achieve what I am trying to achieve?
Thank you for your time and wisdom!
Save the results to a new array and you can then iterate over that when you need to:
Then whenever you want: