Jump to content

PHP while statement within a while within a foreach


albmed7589

Recommended Posts

Hey guys I need some help, 

Im trying the make a page that will list the selected customer's name with a drop down bow that will show ( and let the user change ) the customer's status

 

this is what I've got so far
 

<?php
include("_credentials.php");
include("_mysql.php");
include("_login.php");
$is_AddingToOffice = $_POST["AddToOffice"];
?>
 
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
 
        <link rel="stylesheet" href="css/common.css" />
        <link rel="stylesheet" href="css/grid.css" />
 
        <script src="ckeditor/ckeditor.js"></script>
        <script src="js/jquery.min.js"></script>
        <script src="js/moment.js"></script>
    </head>
    <body>
    <?php include("common_header.php"); ?>
 
    <div class="container">
        <div class="page page_margin box">
            <h1>
                <b><?=$is_AddingToOffice ? 'Add Customers to Office' : 'Edit Customers Status'?></b>
            </h1>
 
            <?php if ($is_AddingToOffice): ?>
            <form method="POST" action="actions/edit_task.php">
            <input type="hidden" name="cid" value="<?=$cid?>" />
            <div class="grid">
 
 
 
 
<?php
//this set of code will handle the bulk user status settings.
if (isset($_POST["AddToOffice"])):
 
$ids = $_POST['check_list'];
foreach ($_POST['check_list'] as $checks)
  {
 
        $query = "SELECT `id`,`name`,`status` FROM `customers` WHERE `id`=?";
        $stmt = $db->prepare($query);
        $stmt->bind_param("i", $checks);
        $stmt->execute();
        $stmt->bind_result($cid, $name, $status);
        while($stmt->fetch())
          { ?>
 
            <div class="form_row grid__col grid__col--2-of-2">
                <div class="label"><?=$name?></div>
            </div>
 
 
 
        <?php  }
 
  }
  endif
 
?>
 
<div class="form_row grid__col grid__col--1-of-2">
    <input type="submit" value="Submit Edit" class="button button-primary" />
</div>
</div>
</form>
<?php endif ?>
 
 
 
 
 
 
              <?php if (!$is_AddingToOffice): ?>
              <div class="form_row grid__col grid__col--1-of-3">
              <form method="GET" action="view_task.php">
                     <input type="hidden" name="cid" value="<?=$cid?>" />
                     <input type="hidden" name="edit" value="1" />
                     <input type="submit" value="Edit Task" class="button button-primary" />
             </form>
           </div>
              <div class="form_row grid__col grid__col--1-of-3">
               <form method="POST" action="actions/mark_complete.php">
                     <input type="hidden" name="task_id" value="<?=$task_id?>" />
                     <input type="submit" value="Mark as Complete" class="button button-error" />
               </form>
                   </div>
 
             <?php else: ?>
             <?php endif ?>
            </div>
        </div>
 
        <script>
        <script src="js/common.js"></script>
    </body>
</html>

and I need to add somthing like this to the while statement

 

            <div class="form_row grid__col grid__col--2-of-3">
              <div class="label">Status</div>
              <select type="text" name="status" class="form_element">
                  <option value="NULL"></option>
            <?php
                  $stmt = $db->prepare("SELECT * FROM `statuses`");
                  $stmt->execute();
                  $stmt->bind_result($id, $statuses);
 
                  while($stmt->fetch())
                  {
                      ?>
                      <option value="<?=$id?>"><?=htmlspecialchars($statuses)?></option>
                      <?php
                  }
                  ?>
              </select>
          </div>
Edited by albmed7589
Link to comment
Share on other sites

your code needs to be organized differently, so that it is easy to see what the logic is doing. in fact, i cannot even tell what your code is trying to accomplish. there are forms that are not used and it's not clear if you are trying to edit the status of one customer at a time or a group of customers all at once.

 

the code in your file should be laid out in this general order -

1) initialization - require/initialize things your code needs

2) post method form processing - form processing that alters data

3) get method business logic - code that knows how to retrieve/produce data that's needed to display the page

4) get method presentation logic - code that knows how to produce the dynamic content on the page

5) html document - starts with the <!DOCTYPE tag and ends with the </html> tag.

 

this will move most the php code to the top of the file and it will group together common types of code. the get method business logic contains the queries and database statements needed to retrieve data and store data into php variables and would not contain any html markup. the get method presentation logic would take the data from all the sections of code above it and produce the dynamic content for the page and would not contain any database statements. the html document would contain a minimum of php statements, simple loops and conditional statements, calls to (output) functions, and php echo statements.

 

also, by arranging your code like this, it is easier to test. you can make sure each section produces the result you want, before going onto the next section.

 

you should use get parameter(s) in the url to determine what the get method business logic will do. if the 'check_list' data is going to cause a group of customer information to be displayed, the 'check_list' data should be supplied as $_GET data, not $_POST data. you would also prepare the SELECT query once, then only populate the $checks variable and then execute the query inside the loop.

 

if you are selecting a single customer to edit at a time, the $cid value would be passed as a get parameter and you would reference it using $_GET['cid'] in the code. despite one of the forms using method='get', there's no code using the submitted data that would be in $_GET['cid'].

 

when you output the select/option list, you would want to pre-select the current setting. if you organize the code as i have suggested, you would retrieve the statuses data into a php array variable. you would loop over this pre-retrieved data when you produce the select/option list for each customer.

 

so, better organize your code and try to get it to do what you wan't. if you need more help, post your new code and if it's not clear from the code what you are trying to do, provide an explanation of what the work-flow should be.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.