rksprst Posted May 8, 2007 Share Posted May 8, 2007 I'm trying to create checkboxes from a database table header row and then pass that to another page. The checkboxes are gotten from the "header" row of the mysql table. I check which of these checkboxes are selected and if it is selected, I display the data for the rest of the rows under that same column which is selected as a checkbox. Here's the code to generate the checkboxes: while($row = mysql_fetch_row($result)) { $first = $row[1]; $last = $row[3]; $id = $row[0]; // 44 is type //echo $id; if($id == 1) { echo "<b>Please select the items you want to display:<br /><br /></b>"; echo "<form action='index.php?selected=$selected&com=com_view' method='post'>"; for($i = 1; $i < 44; $i++) //start at 1, since skipping the ID { echo "<table id='index'>"; $num = 0; echo "<tr><td>"; echo "<input type='checkbox' name='cat[]' value='$row[$i]' />" . $row[$i] . "<br />"; echo "</td><td>"; $i++; echo "<input type='checkbox' name='cat[]' value='$row[$i]' />" . $row[$i] . "<br />"; echo "</td></tr>"; echo "</table>"; } } } And here's the code that displays the data: while($row = mysql_fetch_row($result)) { $first = $row[1]; $last = $row[3]; $id = $row[0]; // 44 is type if($id == 1) { //cat[0] = row[1] $cat = $_POST['cat']; echo "<thead><tr>"; for($i = 1; $i < 44; $i++) { $r = $i + 1; if(isset($cat[$i])) { echo "<th>" . $row[$r] . "</th>"; //echo $row[$r]; } // echo "cat: " . $cat[$i]; } //echo "</tr></thead>"; } } I can't for the life of me figure out why this isn't working. Does anyone know? Quote Link to comment https://forums.phpfreaks.com/topic/50564-create-checkboxes-from-database-data-not-working/ Share on other sites More sharing options...
JakeTheSnake3.0 Posted May 8, 2007 Share Posted May 8, 2007 1) Are you getting the values from the database at all? 2) Are you referencing the values properly? Echo your $row[$i] and see what you get in different places. 3) Is your problem that the checkboxes won't show? If that's the case then it's definitely your data. If the checkboxes ARE being created then it's something that's happening post submit (pardon the pun). Quote Link to comment https://forums.phpfreaks.com/topic/50564-create-checkboxes-from-database-data-not-working/#findComment-248565 Share on other sites More sharing options...
boo_lolly Posted May 8, 2007 Share Posted May 8, 2007 can we see the query string? Quote Link to comment https://forums.phpfreaks.com/topic/50564-create-checkboxes-from-database-data-not-working/#findComment-248587 Share on other sites More sharing options...
rksprst Posted May 8, 2007 Author Share Posted May 8, 2007 1) Are you getting the values from the database at all? 2) Are you referencing the values properly? Echo your $row[$i] and see what you get in different places. 3) Is your problem that the checkboxes won't show? If that's the case then it's definitely your data. If the checkboxes ARE being created then it's something that's happening post submit (pardon the pun). 1. Yea, I am getting the values from the database. 2. Yes, the referencing seems fine. I did test it with echo it works fine. 3. The checkboxes show fine. The functionality that I want is that a user displays certain fields that he wants to see (via the checkboxes). These fields are derived from the header row of a table. Once the user selects what fields he wants to see I show him all the data (each row in the table) under each field (column in the table). The problem is that when I try to display this data which depends on the users previous choice (via the checkboxes) it does not work. I always display "Last Name" field even when it's not selected and something else is selected. Looking at the code, this simply doesnt make sense to me. There's no reason it shouldn't work or at the very least be doing what it is doing right now. Maybe there is another efficient way to do this? Sure... $query = 'SELECT * FROM people'; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/50564-create-checkboxes-from-database-data-not-working/#findComment-248595 Share on other sites More sharing options...
JakeTheSnake3.0 Posted May 8, 2007 Share Posted May 8, 2007 Without looking at the code, within the loop which creates the row itself, you need to check if the appropriate checkbox in the $_POST array is checked off or not. If so, display the row....perhaps you need to for every iteration of your loop, check every piece of data in the $_POST array and see if it matches...if nothing matches, then the checkbox obviously wasn't checked. ex) 50 checkboxes, all labeled sequentially...lbl1, lbl2, etc. user selects lbl1 and lbl3 but everything else is unchecked...the values of the checkboxes are the same as each of their names....lbl1.value = lbl1...... in your $_POST array, loop through each instance....if $row[$i] == $_POST['chkboxArray'][$r] then item is checked...get it? The $i is the instance of what row you are on...and the $r is just a counter for the checbox array....you could use a foreach loop as well.... Quote Link to comment https://forums.phpfreaks.com/topic/50564-create-checkboxes-from-database-data-not-working/#findComment-248608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.