dennismonsewicz Posted October 2, 2008 Share Posted October 2, 2008 code i am working with: <?php /*ini_set ("display_errors", "1"); error_reporting(E_ALL);*/ include "includes/sql.php"; $id = 24; $query = mysql_query("SELECT * FROM added_projects WHERE id = '$id'")or die(mysql_error()); $results = mysql_fetch_object($query); $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); $checkbox_results = mysql_fetch_object($checkbox_qry); $field = mysql_num_fields($checkbox_qry); for($i = 2; $i < $field; $i++) { $names = mysql_field_name($checkbox_qry, $i); $title .= $names . "<br />"; /*$nobreak .= '"' . $names . '",';*/ } echo $title; ?> I am wanting to set the $names field into an array where the individual array values are the names of the individual SQL table column names... anyway of doing this? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Like this? <?php /*ini_set ("display_errors", "1"); error_reporting(E_ALL);*/ include "includes/sql.php"; $id = 24; $query = mysql_query("SELECT * FROM added_projects WHERE id = '$id'")or die(mysql_error()); $results = mysql_fetch_object($query); $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); while ($row = mysql_fetch_assoc($checkbox_qry)){ foreach ($row as $k=>$val){ echo "Column $k is $val<br>"; } ?> Not sure exactly how you're wanting to echo the results. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 thanks f1fan it worked like a charm!!! Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 is there anyway to use that code to not display the first two records? while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $column .= $k . " "; } } echo $column; Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Add this: $row = mysql_fetch_assoc($checkbox_qry) on the line just before the while loop. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 that didn't work updated code: $row = mysql_fetch_assoc($checkbox_qry); while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $column .= $k . " "; } } echo $column; Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Hmm... Try this: $count = 0; while($row = mysql_fetch_assoc($checkbox_qry)) { if ($count>0){ foreach ($row as $k=>$val) { $column .= $k . " "; } } $count++; } echo $column; Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 nope updated code: $count = 0; while($row = mysql_fetch_assoc($checkbox_qry)) { if ($count>0){ foreach ($row as $k=>$val) { $column .= $k . " "; } } $count++; } echo $column; Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Then I don't know. That should ignore the first row. Look at the logic. $count is 0. The first row is grabbed, $count is still 0, it does nothing. Then, $count is incremented, so $count is no longer 0 and, therefore, each remaining row would be added to your $column variable. It sounds like the row you're trying to remove is actually the second row or something. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 alrighty i will do some more digging about that one.. Heres another one for ya I have this script here: <?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $column .= "Column $k is $val<br>"; $values .= $val; } } if($values > 0) { $checked = 'checked = "checked"'; } $field = mysql_num_fields($checkbox_qry); for($i = 2; $i < $field; $i++) { $names = mysql_field_name($checkbox_qry, $i); $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); $title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $checked . ' /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>'; } echo $title; When the if($values > 0) runs it checkes all of the boxes instead of the ones with values great than 0 any ideas here? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 That's because you're setting the $checked value and then reusing the $checked value over and over, so it's checking all of them. You need to use a unique variable name (an array would be best) for each value. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 can you give me an example? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 You want an example of what you're doing wrong? I know what you're doing wrong, but I don't know exactly what you want. Your code is kinda hard to follow. Your brackets {} appear to be off, but again, I don't know what you're trying to do. The key is that you're using ONE value to assign the checked attribute, and the using it over and over. At one point, you assigned $checked to be 'checked = "checked"' and then you're using the same variable over and over and then expect it to be different in different cases. See your problem? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 what I am trying to do is check the $val var and see if it equals 1 if it does then make the variable $chk = 'checked = "checked"'; Updated code: while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $column .= "Column $k is $val<br>"; $values .= $val; } } if($values > 0) { $chk = 'checked = "checked"'; } Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 OK, your code is adding $val to $values as a string (using the .= code), so if you had 1, 2, and 3, you would get 123, rather than 6. Then AFTER the loop, if $values (a string) is greater than 0 (a number) then they are ALL checked. Are you trying to echo a checkbox for every value in the loop? If so, why isn't the echo inside the while loop? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 let me give you all of the code I am working with so you can see what I am trying to do <?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $column .= "Column $k is $val<br>"; $values .= $val; } } if($values > 0) { $chk = 'checked = "checked"'; } $field = mysql_num_fields($checkbox_qry); for($i = 2; $i < $field; $i++) { $names = mysql_field_name($checkbox_qry, $i); $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); $title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>'; } echo $title; ?> I have placed the $chk var in the $title var as to say if it is greater than 0 display checked="checked"... does this help? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 bump Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 <?php if($values > 0) { $chk = 'checked = "checked"'; } //... $title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>'; echo $title; ?> I have placed the $chk var in the $title var as to say if it is greater than 0 display checked="checked"... does this help? Do you understand what I said about string vs. numeric?? Variable $values is a string, not a number. Therefore, "if ($values >0) {" doesn't make sense to PHP. Also, because you're assigning $chk outside of ANY loop, it will only ever have ONE value. Assuming you don't want EVERY checkbox checked, what SHOULD determine if it is checked? Is it $val being greater than zero? Again I ask, why don't you do the echo in the while loop?? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 ah gotcha sorry i have been working on several things today and that for some reason I was over looking Here is the updated code: $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); $field = mysql_num_fields($checkbox_qry); for($i = 2; $i < $field; $i++) { $names = mysql_field_name($checkbox_qry, $i); $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); $title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>'; } while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $column .= "Column $k is $val<br>"; $values .= $val; } echo $title; } And yes what determines the check is if $val > 0... how would i accomplish this? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 OK, I see you've moved some things around, and you still didn't answer my question, so I'll assume it won't be a problem to do it all in the loop. Will this work for you? <?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $chk = $val>0?'checked = "checked"':""; echo '<div><input type="checkbox" name="checkboxes[' . $k . ']" class="checkbox" id="' . $k . '" ' . $chk . ' /> <label for="checkboxes[' . $k . '">' . ucwords($val) . '</label></div>'; } ?> Again, don't know exactly what you're doing, but there ya go. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 it works like a charm and actually you left off a bracket. Now here is one more thing! The $k is displaying the mysql table column names and I need it to not display the first two... anyway of doing that with your code? update code: $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); while($row = mysql_fetch_assoc($checkbox_qry)) { foreach ($row as $k=>$val) { $chk = $val>0?'checked = "checked"':""; $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); echo '<div><input type="checkbox" name="checkboxes[' . $k . ']" class="checkbox" id="' . $k . '" ' . $chk . ' /> <label for="checkboxes[' . $k . '">' . ucwords(str_replace($numbers, '', $k)) . '</label></div>'; } } Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 <?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); $count = 1; while($row = mysql_fetch_assoc($checkbox_qry)) { if ($count > 2) { // just change the 2 to however many rows you want to skip foreach ($row as $k=>$val) { $chk = $val>0?'checked = "checked"':""; $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); echo '<div><input type="checkbox" name="checkboxes[' . $k . ']" class="checkbox" id="' . $k . '" ' . $chk . ' /> <label for="checkboxes[' . $k . '">' . ucwords(str_replace($numbers, '', $k)) . '</label></div>'; } } $count++; } ?> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Author Share Posted October 2, 2008 now its not displaying anything ??? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Dunno. First of all, I forgot the ] again. Second, have you tried removing the $count part and trying again? Quote Link to comment 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.