Jump to content

OldWest

Members
  • Posts

    296
  • Joined

  • Last visited

    Never

Everything posted by OldWest

  1. Ken, I am messing around with your For loop code, and I am able to integrate it to the same degree as I have been... The select boxes won't stay checked.. But I have the print_r printing the selected array items after the submit which I think means something is being "held"... Now I just need to figure out how to get those checkboxes checked... I don't want you to tell me the answer, but if you could tell me where to investigate in the code... I don't even know if I am using the forloop right with the limited (center) $row var I have in there, but it seems to work so Thanks for any guidance. I think I am close to being a step ahead of where I was 48 hours ago in using arrays. Here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Arrays</title> </head> <body> <?php require('db_cxn.php'); ?> <?php $cxn = mysqli_connect($host,$user,$pass,$db) or die("Could not connect to the server."); $query = "SELECT DISTINCT cat FROM product ORDER BY cat"; $results = mysqli_query($cxn,$query) or die("Could not execute query."); ?> <?php if (isset($_POST['runForLoop'])) { echo '<pre>' . print_r($_POST,true) . '</pre>'; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?php $tmp = array(); for($i=0;$row = mysqli_fetch_array($results);++$i) { extract($row); $chk = (isset($_POST['checkbox']) && in_array("checkbox$i",$_POST['checkbox']))?'checked':''; $tmp[] = '<label for="checkbox' . $cat . '">Checkbox ' . $cat . '</label>'; $tmp[] = '<input type="checkbox" name="checkbox[]" value="checkbox' . $cat . '" ' . $chk . '><br>'; } echo implode("\n",$tmp) . "<br>\n"; echo count($tmp); // counts all array elements ?> <input type="submit" name="runForLoop" value="Run For Loop >>" /> </form> </body> </html>
  2. Well Im going back to re study arrays and for loops.. Trying to find out if I can nest a while loop inside a for loop (or if this even makes sense)... Need to get back to basics. I think this endeavor was a bit too steep a learnin curve. Thanks for the help and tips.
  3. Ken, That's some mad skills! Well at least I know now I was not totally wasting my time on something that was impossible! Thank you for curing my madness!! I've got at least 10 hours in trying to solve this.. Had some great tips along the way, but I think this closes the case.. I like how you echoed the Array out as well.. That certainly helps paint the picture as well..
  4. Here is my table dump so you can see where I am getting my CakePHP from: -- phpMyAdmin SQL Dump -- version 3.2.0.1 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Oct 28, 2010 at 02:03 AM -- Server version: 5.1.36 -- PHP Version: 5.3.0 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- Database: `dummies` -- -- -------------------------------------------------------- -- -- Table structure for table `product` -- CREATE TABLE IF NOT EXISTS `product` ( `id` int(11) NOT NULL AUTO_INCREMENT, `product` varchar(100) NOT NULL, `cat` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; -- -- Dumping data for table `product` -- INSERT INTO `product` (`id`, `product`, `cat`) VALUES (1, 'Programming Book for CakePHP', 'CakePHP'), (2, 'jQuery Programming for Dummies', 'jQuery'), (3, 'Jason''s Book', 'General'), (4, 'The Story of PHP & MySQL', 'MySQL'), (5, '', 'Ruby'), (6, '', 'Python');
  5. Hi Ken I'm sorry my code did not go up.. Here is a WORKING static version I am testing with (and below it is the ARRAY version which I think php is buggy with): <html> <head> <title>Checkboxes</title> <?PHP $ch1 = 'unchecked'; $ch2 = 'unchecked'; $ch3 = 'unchecked'; $ch4 = 'unchecked'; $ch5 = 'unchecked'; if (isset($_POST['Submit1'])) { if (isset($_POST['ch1'])) { $ch1 = $_POST['ch1']; if ($ch1 == 'net') { $ch1 = 'checked'; } } if (isset($_POST['ch2'])) { $ch2 = $_POST['ch2']; if ($ch2 == 'word') { $ch2 = 'checked'; } } if (isset($_POST['ch3'])) { $ch3 = $_POST['ch3']; if ($ch3 == 'excel') { $ch3 = 'checked'; } } if (isset($_POST['ch4'])) { $ch4 = $_POST['ch4']; if ($ch4 == 'web') { $ch4 = 'checked'; } } if (isset($_POST['ch5'])) { $ch5 = $_POST['ch5']; if ($ch5 == 'php') { $ch5 = 'checked'; } } } ?> </head> <body> <FORM NAME ="form1" METHOD ="POST" ACTION ="<?php echo $_SERVER['PHP_SELF']; ?>"> <Input type = 'Checkbox' Name ='ch1' value ="net" <?PHP echo $ch1; ?>> Value 1 <P> <Input type = 'Checkbox' Name ='ch2' value="word" <?PHP echo $ch2; ?>> Value 2 <P> <Input type = 'Checkbox' Name ='ch3' value="excel" <?PHP echo $ch3; ?>> Value 3 <P> <Input type = 'Checkbox' Name ='ch4' value="web" <?PHP echo $ch4; ?>> Value 4 <P> <Input type = 'Checkbox' Name ='ch5' value="php" <?PHP echo $ch5; ?>> Value 5 <P> <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Choose your books"> </FORM> </body> </html> Array version (which is throwing errors now because Im on the 23rd iteration and variation of code to get this working!) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Array Populate w/ Database</title> </head> <body> <h1>Array *Checkbox Populate w/ Database</h1> <?php require('db_cxn.php'); ?> <?php $cxn = mysqli_connect($host,$user,$pass,$db) or die("Could not connect to the server."); $query = "SELECT DISTINCT cat FROM product ORDER BY cat"; $results = mysqli_query($cxn,$query) or die("Could not execute query."); ?> <p> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="submit" name="runQueryCheckboxes" value="Run Query for Category Radio Options" /> </form> </p> <ul> <?php $cat = 'unchecked'; if(isset($_POST['runQueryCheckboxes'])) { if (isset($_POST['$cat'])) { $chk1 = $_POST['$cat']; if ($chk1 == 'CakePHP') { $chk1 = 'checked'; } } }; if(isset($_POST['runQueryCheckboxes'])) { while($row = mysqli_fetch_array($results)) { extract($row); echo "<li><input type='checkbox' name='selection[$cat]' id='$cat' value='$cat' checked='$chk1' /> <label for='$cat'>$cat</label></li>"; var_dump(''); } }; ?> </ul> </body> </html>
  6. Hi Ken, I just posted what I have.. Ive been through about 20 iterations and variations of the above. The above IS throwing errors! I just CANNOT for the life of me see how non-static array values can be used in this way.. I can see how I could do this all if all of the html and name="" values were static.. But using the array like I am, I am starting to think it is just not possible or PHP does not support this.
  7. I've got about 4 more hours down the pipe on this, and I am starting to think there is a bug with php and checkbox values being set... Just for kicks, I added some input fields and had them echoed $_POST back data stored OK.. Tried the same method with the checkboxes no dice.
  8. Thanks and makes sense. I'll get it solved for my learning needs and post my code up when done.. Yeah no doubt its so easy to overthink this "simple" stuff.
  9. Pikachu2000, I am still unable to debug this... I am starting to think there might not be much purpose to keep checkboxes checked on a php self form submission.. In a real life scenario I think passing the values from page to page would make much more sense in a multi page form or something like that.. And I THINK using a $_SESSION might work better for this... Anyhow looks like it's back to the drawing board to get this to work on a multi page form.. Need to rethink this thing now... Any tips for a better or smarter direction are welcome
  10. Nice profile pic. We've almost got the whole family on here now. Just need the Dinosaur and the others.. I like your approach on the number system, just not sure if that will work with my current code setup, so I will see what I can do. Thanks.
  11. Nice in_array might be exactly what I needed.. I figure 1,000,000 programmers before me ran into this same issue, so php seems to be way ahead of me! Thanks for the guidance. I'm going to reformulate my code to get this ticking..
  12. oops! I realized my original idea was not working and posted a new thread specifically in relation to this issue.. I guess it can be removed. Thanks
  13. I am simply trying to keep a check box checked once selected. What I have works, but not correctly. Because I am calling each array element in my checkbox like name="checkbox[]" And I am using this as the input return the check: <input type="checkbox" name="checkbox[]" value="checkOneValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> I have 5 checkboxes that use the same code! And as I said it works, but returns ALL checkboxes as selected when submitted. I only want the actual selected boxes to remained checked. Any insight on a work around? The problem is I cannot solve how to identify unique array values as they are all named the same... Maybe I am not thinking right on all of this. Here is my full source code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Checkbox Echoing Test</title> </head> <body> <form name="" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> One <input type="checkbox" name="checkbox[]" value="checkOneValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Two <input type="checkbox" name="checkbox[]" value="checkTwoValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Three <input type="checkbox" name="checkbox[]" value="checkThreeValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Four <input type="checkbox" name="checkbox[]" value="checkFourValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Five <input type="checkbox" name="checkbox[]" value="checkFiveValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> <input type="submit" name="submit" value="Echo Checkbox Data!" /> </form> <?php if (isset($_POST['checkbox']) && (isset($_POST['submit']))) { $checkbox = $_POST['checkbox']; foreach ($checkbox as $checkboxValue) { var_dump($checkboxValue); if (!empty($checkboxValue)) { echo "<br />$checkboxValue is checked"; } } }; // End of foreach loop if (!isset($_POST['checkbox']) && (isset($_POST['submit']))) { echo "<br />No checkboxes were selected!"; }; ?> </body> </html>
  14. Also for any nooby who might be following this thread, you can maintain the checkbox checkmark by doing this: <?php if(isset($_POST['checkbox'])) echo "checked"; ?> For example: <input type="checkbox" name="checkbox[]" value="checkOneValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> So basically once the form is submitted, the checkbox will stay in the box so as to inform the user what he/she selected - I guess its user friendly
  15. Interesting nesting with foreach... Well I am not sure if this code is very efficient, but I segmented it out and added some "thought" to the process but the below works.. I think my next step is to try to figure out how to echo the values that were NOT selected, but not sure if that's possible without javascript seeing that they must be set in order to get the value (at least I think!). Am I correct in my understanding? For example, if I wanted to echo to the browser: $valueOne was checked. $valueTwo was not checked. $valueThreewas not checked. .... <?php if (isset($_POST['checkbox']) && (isset($_POST['submit']))) { $checkbox = $_POST['checkbox']; foreach ($checkbox as $checkboxValue) { if (!empty($checkboxValue)) { echo "<br />$checkboxValue is checked"; } } }; // End of foreach loop if (!isset($_POST['checkbox']) && (isset($_POST['submit']))) { echo "<br />No checkboxes were selected!"; }; ?>
  16. I also tried if (!isset($_POST['checkbox'])) for the else and that did not work either for testing the isset.
  17. thorpe, that def removed the errors, but i cannot get it to work as intended now.. throwing in the towel for tonight.. I just don't understand while I keep getting a parse error on this. I tried about 5 variations of the else and braces etc and it won't give - painful at this point! Maybe I need to run the foreach twice for each if and else? Tried that but did not work.. The parse error I am getting is on the else line. Parse error: parse error in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 30 <?php if (isset($_POST['checkbox'])) { $checkbox = $_POST['checkbox']; foreach ($checkbox as $checkboxValue) { echo "<br>$checkboxValue is checked"; else { echo "$checkboxValue is not checked."; } } }; ?>
  18. I don't think I understand exactly what you mean (and I'm about to fall asleep while I type ). I just tried a literally echo of my query result like: $result = mysql_query("SELECT * FROM orig_codes_1a") or die(mysql_error()); // END :: Create query to be displayed as final results of original codes table. echo "<p>$result</p>"; And as output I get: Resource id #4
  19. I realized after I posted and then modified my post code to have $checkbox = $_POST['checkbox']; BEFORE the foreach.. When I select boxes and submit all works as intended, but when nothing is selected I get this junk: Notice: Undefined index: checkbox in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 24 Warning: Invalid argument supplied for foreach() in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 26
  20. Excuse my late night slop! But I read a thread earlier and I am working on a dynamic way to address checkboxes with some intuitive messages while processing, and I cannot get this to work right w/out errors.. Any ideas where I am going totally wrong with this? <form name="" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> One <input type="checkbox" name="checkbox[]" value="checkOneValue" /><br /> Two <input type="checkbox" name="checkbox[]" value="checkTwoValue" /><br /> Three <input type="checkbox" name="checkbox[]" value="checkThreeValue" /><br /> Four <input type="checkbox" name="checkbox[]" value="checkFourValue" /><br /> Five <input type="checkbox" name="checkbox[]" value="checkFiveValue" /><br /> <input type="submit" name="submit" value="Echo Checkbox Data!" /> </form> <?php if (isset($_POST['submit'])) { $checkbox = $_POST['checkbox']; foreach ($checkbox as $checkboxValue) if (empty($checkboxValue)) { echo "$checkboxValue is emtpy"; } else { echo "<br>$checkboxValue is checked"; } }; ?> When no checkboxes are selected, I am dealing with this S%#$! Otherwise it works as intended.. Notice: Undefined index: checkbox in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 24 Warning: Invalid argument supplied for foreach() in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 26
  21. php handles checkboxes differently that your type="text". This is how I address the problem you are facing. For each checkbox option I create an if block like so: if(isset($_POST["checkBoxName"]["checkBoxalue"])) { echo $_POST['checkBoxName']; } else { exit(); }; The above code might not work right, but it should give you the right direction! You basically want to store each checkbox Name and Value and then once it's stored you can insert it into MySQL or a variable for email, posting to a page, etc..
  22. This serves more or less a meaningless purpose for my testing, but I've been trying to echo out the names of the tables that are being used in my UPDATE and SELECT queries and I have yet found a way to do this.. Did some research on php.net and Google and did not find anything that seems to directly address this.. My guess is it's so stupid simple, I probably will have an answer by the time I wake up.. But id there are any suggestions on this, Id be glad to take.
  23. Pikachu2000, I think you just asked the right question. I'm probably just as tired as you missing this simple point. What hours of studying php do to the mind - melt... Brain on php You made me wonder this simple question: How in the heck could I expect an UPDATE to output results? So I added a SELECT query and it's all working now: $result = mysql_query("SELECT * FROM orig_codes_1a") or die(mysql_error()); Now I just need to find out what the heck my app is doing
×
×
  • 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.