Jump to content

xfilterx

Members
  • Posts

    19
  • Joined

  • Last visited

xfilterx's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. So I got it working. Not sure if this is the most elegant solution but this works. $arrAssets = array(); while($row = mysqli_fetch_array($selectedAssets)) { $arrAssets[] = $row; } while ($rowAssets = mysqli_fetch_array($assets)) { $assetid = $rowAssets["asset_id"]; for ($x = 0; $x < count($arrAssets); $x ++) { if($arrAssets[$x]["asset_id"] == $assetid) { $selectedAsset = "checked"; break; } else { $selectedAsset = null; } } echo "<input type='checkbox' name='assets[]' {$selectedAsset} value='" . $rowAssets["asset_id"] . "'>" . $rowAssets["asset"] . "<br>"; } This is about all of my 72 hours of PHP knowledge could muster. I think I'd be dangerous had I started using PHP back in the early 2000's when I first learned ASP.NET.
  2. Also tried it this way....same thing...nothing is checked. $arrAssets = array(); while($row = mysqli_fetch_array($selectedAssets)) { $arrAssets[] = $row; } //echo $arrAssets[2]['asset_id']; //Debug while ($rowAssets = mysqli_fetch_array($assets)) { $assetid = $rowAssets["asset_id"]; foreach($arrAssets as $value) { if($assetid==$value) { $selectedAsset = "checked"; } else { $selectedAsset = null; } } echo $assetid; //Debug echo "<input type='checkbox' name='assets[]' {$selectedAsset} value='" . $rowAssets["asset_id"] . "'>" . $rowAssets["asset"] . "<br>"; }
  3. Tried something a little different but getting the same result. $selectedAsset is null. $arrAssets[] = mysqli_fetch_array($selectedAssets); while ($rowAssets = mysqli_fetch_array($assets)) { $assetid = $rowAssets["asset_id"]; if (in_array($assetid, $arrAssets)) { $selectedAsset = "checked"; } else { $selectedAsset = null; } echo "<input type='checkbox' name='assets[]' {$selectedAsset} value='" . $rowAssets["asset_id"] . "'>" . $rowAssets["asset"] . "<br>";} This seems to make sense. Dump the selected asset_ids into an array ($arrAssets). Within the loop check to see if the assetid exists within the array. If it does, set a variable to checked. If it doesn't, set it to null. Unfortunately it's always null. Should be 2, 3, and 5 that are the selected checkboxes....
  4. Yeah it's valid. Modified my code slightly to look like this: //while ($rowAssets = mysqli_fetch_array($assets)) //{ // $assetid = $rowAssets["asset_id"]; while ($rowSelAssets = mysqli_fetch_array($selectedAssets)) { //$selectedAsset = ($assetid == $rowSelAssets['asset_id']) ? "checked" : null; echo "<input type='checkbox' name='assets' value='" . $rowSelAssets["asset_id"] . "'>" . $rowSelAssets["asset_id"] . "<br>"; } //echo "<input type='checkbox' name='assets' {$selectedAsset} value='" . $rowAssets["asset_id"] . "'>" . $rowAssets["asset"] . "<br>"; //} And it spits out (with checkboxes next to them): 2 3 5 So yeah, that appears to be working...
  5. Pretty sure. I'll verify by isolating that loop into a drop down menu or something. Stand by...
  6. This is what is being output: <input type='checkbox' name='assets' value='4'>Pipeline<br> Notice the extra space between here ----------^
  7. Nothing. There's an empty space where "checked" should be in code. Also, I did some other outputs and the standard_assets table is being connected to and data is being read correctly. No issues there. The problem is with the nested loop.
  8. Yeah I posted my code. It seems like my nested loop is correct...not sure why it's not setting $selectedAsset to "checked".
  9. This is what I'm working with: while ($rowAssets = mysqli_fetch_array($assets)) { $assetid = $rowAssets["asset_id"]; while ($rowSelAssets = mysqli_fetch_array($selectedAssets)) { $selectedAsset = ($assetid == $rowSelAssets['asset_id']) ? "checked" : null; } echo "<input type='checkbox' name='assets' {$selectedAsset} value=" . $rowAssets["asset_id"] . ">" . $rowAssets["asset"] . "<br>"; } My rationale for writing it like this is...I loop through the main Assets table and display all of the checkboxes with associated values. In total there are 10 including the following: Bridge Containment Liners Offshore Structure Pipeline Pressure Vessel Ship Structure Tank, General Purpose Tank, Chemical & Solvent Tank, Caustic Storage Tank, Potable Water The checkboxes are showing up fine. Next what I do within the loop is set the $assetid variable equal to the asset_id of the current item within the loop. Then I perform a nested loop to loop through the 3 items that are selected within the intermediary table "standards_assets". In that table asset_id's 2, 3, and 5 are set. Within the loop I check to see if $assetid is equal to the asset_id value within that table. If it is, I set $selectedAsset to "checked". Finally I echo out the checkbox...hoping that the checked state would be set....but it is not. Any ideas?
  10. That's the point, I need to select the checkboxes with values of 2, 3, and 5 if those are the values that are returned from the table for standard_id=1. In terms of your array...I flat out don't know how to do that in PHP. I'm in ASP.NET guy that got thrust into a PHP project. Any pointers would be greatly appreciated.
  11. Alright so I got one last issue here as it relates to my project....I have a main standards table. I also have several tables like assets, substrates, organizations, etc. For each standard, several assets can exist. For each standard several substrates can exist, etc. etc. You get the picture. I've attached a DB sample to help visualize. There are many more tables but they all look exactly like this....I think you get the picture. So essentially, for every standard, there can be numerous environments and processes associated with it. So let's say one standard (standard_id=1) would have several processes associated with it. So in the standards_process table it could look something like this: 1 1 2 2 1 3 3 1 5 What I need to do is be able to select the process_id's 2, 3, and 5 from that table and preselect the corresponding checkboxes in the web app. My code looks like this....mind you I have not even attempted to preselect the checkboxes....at this point I'm just pulling the processes and creating a checkbox list on the fly. That works great (I commented out the code that Zane helped me with on the pre-selection of the DropDownList for organizations. That worked great but it was a dropdown list and not a series of independent checkboxes!). //Process echo "<tr><td style='font-weight:bold;vertical-align:top;'>Process</td><td>"; while ($rowProcess = mysqli_fetch_array($process)) { //$selected = ($orgid == $rowOrganizations['organization_id']) ? "selected='selected'" : null; echo "<input type='checkbox' name='process' value=" . $rowProcess["process_id"] . ">" . $rowProcess["process"] . "<br>"; } echo "</td></tr>"; In ASP.NET this is actually pretty easy. I would dump the table into a Dataset (a collection essentially). Use a For Each loop to loop through that collection. Then use a nested loop to loop through the collection of checkbox items in a CheckBoxList control and select the checkbox item if it's value matches the value in the row within the Dataset. Not sure how to go about doing this in PHP....any help would be greatly appreciated....
  12. I plan too....thanks. Hope to see your response on my next question....stay tuned!
  13. Zing! That did it! Thanks so much bro....I'm knee deep in this project so I'm sure I'll have another question. I usually try Google searching what I need but I keep coming up with CodeProject and StackOverflow questions that are similar to mine that go completely unanswered. The next critical piece of this will be to pull values from several lookup tables into multiple checkbox lists. Same thing....a standard can and will have several possibilities for check boxes so I will need to loop through all of the selected values within a table and check all of the corresponding check boxes in the list. Fun fun fun.
  14. Thanks for the attempt Zane. Your code makes sense to me however it's not working. The list is still bound with the values from the organizations table however the appropriate id is not being selected. It just defaults to the first item in the list. I did have to add a couple of quotes to your code...one after the value and one after the second period (a concatonation operator I assume). Here's the whole script so you can see what's going on. This organization_id is being retrieved correctly..,as it's being printed out in a text box below the while loop... <?php echo "<table>"; while($rowStandards = mysqli_fetch_array($standards)) { echo "<tr><td>Standard ID</td><td><input type='text' value='" . $rowStandards['standard_id'] . "' name='standard_id' disabled></td></tr>"; echo "<tr><td>Organization</td><td><select name='organization'>"; $selected = $rowStandards["organization_id"]; while ($rowOrganizations = mysqli_fetch_array($organizations)) { $selected = ($selected == $rowOrganizations['organization_id']) ? "selected='selected'" : null; echo "<option {$selected} value='" . $rowOrganizations["organization_id"] . "'>" . $rowOrganizations["organization"] . "</option>"; } echo "</select></td></tr>"; echo "<tr><td>Organization ID</td><td><input type='text' value='" . $rowStandards['organization_id'] . "' name='organization_id'></td></tr>"; echo "<tr><td>Item No</td><td><input type='text' value='" . $rowStandards['item_no'] . "' name='item_no'></td></tr>"; echo "<tr><td>Standard No</td><td><input type='text' value='" . $rowStandards['standard_no'] . "' name='standard_no'></td></tr>"; echo "<tr><td>Title</td><td><input type='text' value='" . $rowStandards['title'] . "' name='title'></td></tr>"; echo "<tr><td style='font-weight:bold;'>Keywords</td><td><input type='text' value='" . $rowStandards['keywords'] . "' name='keywords'></td></tr>"; echo "<tr><td>Abstract</td><td><textarea name='abstract'>" . $rowStandards['abstract'] . "</textarea></td></tr>"; echo "<tr><td>Link</td><td><input type='text' value='" . $rowStandards['link'] . "' name='link'></td></tr>"; } echo "</table><br>"; if (!$standards) { printf("<br><br>Error: %s\n", mysqli_error($con)); exit(); } ?>
  15. This list binds fine with this line here: echo "<option value='" . $rowOrganizations["organization_id"] . "' " . ' ' . ">" . $rowOrganizations["organization"] . "</option>"; It's when I add the if statement that it gives me an error saying unexpected token if. echo "<option value='" . $rowOrganizations["organization_id"] . "' " . if($selected == $rowOrganizations["organization_id"]){ print "selected";} . ">" . $rowOrganizations["organization"] . "</option>"; Looking through my PHP book and looks like my syntax for the if is correct. Not sure what's going on here...
×
×
  • 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.