Note that there are some explanatory texts on larger screens.

plurals
  1. PORefer to one single SQL query 3 times on a page, and create 3 separate loops off that query?
    primarykey
    data
    text
    <p>What I'm trying to accomplish is a back-end Admin panel, with multiple forms to edit tables from a database.</p> <p>I would like to run one SQL query on the page, &amp; refer to it multiple times (<strong>possibly creating 3 different loops out of this same query, without having to query it more than once</strong>).</p> <p><em>I can't figure out how to make the query work outside of its initial loop</em>, if possible at all.</p> <p>I already have the SQL statement that would work on all the forms (since basically my forms just query the same 2 tables over &amp; over again), but I can't use it more than once... I already tried it, but can't get it to work. </p> <p><strong>I just want to run this query once, &amp; simply refer back to it whenever I need to use it again on the page.</strong> (Perhaps by creating a new <em>while</em> loop? Tried it, doesn't work. Use different variables? Doesn't work. I'm itching to find out what DOES...) </p> <p>This Admin panel is a single PHP page, divided by compartments based on administrative tasks. </p> <p>For some idea, it is like so ~</p> <hr> <h2>// UPLOAD PHOTO //</h2> <p><em>(options available are...)</em><br> - Upload photo<br> - Add Photo Title<br> <strong>01.</strong> Select Category <strong>|| SQL QUERY!</strong> LOOP <strong>-</strong> Drop-down list of Categories<br> <strong>02.</strong> Select Sub-categories <strong>|| SQL QUERY!</strong> LOOP <strong>-</strong> Checkbox list of Categories + Sub-categories<br> - Add Tags<br> - Add Photo Description / Commentary </p> <h2>// MANAGE CATEGORIES //</h2> <p><em>(options available are...)</em><br> - Create a new Category<br> <strong>03.</strong> Delete Multiple Categories <strong>|| SQL QUERY!</strong> LOOP <strong>-</strong> Checkbox list of Categories + # of entries each<br> <strong>04.</strong> Rename Category <strong>|| SQL QUERY!</strong> LOOP <strong>-</strong> List of Categories<br> <strong>05.</strong> Edit Sub-Category <strong>|| SQL QUERY!</strong> LOOP <strong>-</strong> List of Sub-categories </p> <hr> <p>So on &amp; so forth...<br> As you can see, all queries made are related to Categories. </p> <p>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 &amp; over again), it just seems impractical and thoughtless. There has got to be a better way...</p> <p>The query I'd like to use just once (&amp; the one I'm using right now) is:</p> <pre class="lang-sql prettyprint-override"><code>$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']; } </code></pre> <p>Is there a way to achieve what I am trying to achieve?<br> <strong>Thank you for your time and wisdom!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload