Question

So I have a categories table in my database and each category has a certain amount of steps assigned to it. I want to have all of the categories displayed as tabs titled with the category name so I need this from the database. I then have a form inside the tabs to insert the amount of steps needed for that category, but this form is looping... I need it inside the foreach to get the category id but without the form looping... Hope that makes sense..

Here is my code:

<?php foreach($categories as $category){ ?>

    <div id="<?php echo $category->category ?>" class="tab">

         <form id="maxSteps" method="POST" name="maxSteps" action="<?php $PHP_SELF; ?>" enctype="multipart/form-data">

             <label for="maxSteps">Amount of steps in form: </label><input style="width:50px;" id="maxSteps" type="text" name="maxSteps" />

             <input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" />

             <input type="Submit" value="Go" name="maxStepsSubmit" />

         </form>

         <table id="amountOfStepsForm">

         <?php $maxStepsById = $wpdb->get_results( "SELECT * FROM metal_work_max_steps WHERE cat_id = '$category->cat_id'" ); ?>

         <?php foreach($maxStepsById as $maxStep){ ?>

             <tr><td id="maxStepsRow<?php echo $maxStep->id; ?>"><?php echo "<p>Amount of steps in form is: <b>".$maxStep->steps."</b>" ?></td><td id="editRow<?php echo $maxStep->id; ?>"><a id="<?php echo $maxStep->id; ?>" class='edit'>Edit</a></td></tr>

             <input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" id="catId<?php echo $maxStep->id; ?>" />

         <?php } ?>

         </table>

    </div>

  <?php } ?>

Regards

Was it helpful?

Solution

The best thing you can do is to run only one query and retrieve all your data inside an associative array. Then just loop trough this data structure.

The query can be done using a JOIN statement, in order to retrieve the information regarding the max steps for each category.

OTHER TIPS

1- put the Form tag above the for loop

2- <input type="hidden" name="catId" val... need to have dynamic name: name="catId_<?php echo $category->cat_id;?>"

3- the button Submit label GO, and the colse tag of Form need to be out of the for loop (below)

so all the fields will be in 1 form, and you will have 1 submit button, if that what you need, and the field names are not the same, so on submit you can read all of them

these are general comments, i think you need to pay attention to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top