dzone Posted May 15, 2007 Share Posted May 15, 2007 Could someone please help me with a problem I'm having? Here's what I'm trying to do: I have a webpage that I need to collect information from and pass on to a result page. The initial page lists product categories from a MySQL database and the result page should show the price list for those selected products. My problem is that I don't want to create an individual item for each category. It should be done dynamically so that as the databse is updated, the webpage is automatically updated. I also want the page to be formatted in a certain way. I need to have checkboxes out beside each dynamically "pulled" product category so that the end-user can select which products they would like to see a price list for. I am able to get the list to print on the initial screen just like I want, HOWEVER, with the way I've set it up, the checkboxes all have the same name, and therefore can't pass unique data to the result page. How can I make the checkboxes "automatically" be assigned unique names when they are part of an array of other information? Here is the code that I'm currently using: <?php print ("<table width=100% border=0 cellpadding=2 cellspacing=0>\n"); $colsPerRow = 4; $colWidth = (int)(100/$colsPerRow); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if ($i % $colsPerRow == 0) { print ("<tr>\n"); } print ("<td>\n"); print ("<table width=100% border=0 cellpadding=0 cellspacing=0>\n"); print ("<tr>\n"); print ("<td><div align=left valign=middle><input type=checkbox name= value=Y> {$row['ProductCategry']}</div></td>\n"); print ("</tr>\n"); print ("</table>\n"); print ("</td>\n"); if ($i % $colsPerRow == $colsPerRow - 1) { print ("</tr>\n"); } $i += 1; } if ($i % $colsPerRow != 0) { while ($i++ % $colsPerRow != 0) { print ("<td width=$colWidth%> </td>\n"); } print ("</tr>\n"); } print ("</table>\n"); ?> <input type="submit" value="Generate Price List"> Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/ Share on other sites More sharing options...
MadTechie Posted May 15, 2007 Share Posted May 15, 2007 why not <?php print ("<td><div align=left valign=middle><input type=checkbox name='{$row['ProductCategry']}[]' value=Y> {$row['ProductCategry']}</div></td>\n"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253573 Share on other sites More sharing options...
dzone Posted May 15, 2007 Author Share Posted May 15, 2007 It still views the boxes as the same name. When I submit the form using that name for the checkbox, it passe "%5B%5D=Y&%5B%5D=Y" to the URL of the submit page. It is basically saying 'Chkbox1 = Y and Chkbox1 = Y'. I need it to differentiate b/t the checkboxes... Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253583 Share on other sites More sharing options...
Psycho Posted May 15, 2007 Share Posted May 15, 2007 why not <?php print ("<td><div align=left valign=middle><input type=checkbox name='{$row['ProductCategry']}[]' value=Y> {$row['ProductCategry']}</div></td>\n"); ?> Hmm...this name='{$row['ProductCategry']}[]' will create a single item array for every product. I would suggest either losing the [] at the end so you have a uniquely named checkbox for each category, or do this: name='categories[]' value='{$row['ProductID']}' Then on your receiveing page you will have $_POST['categories'] as an array of all the product IDs that the user selected. I guessed on 'ProductID' - you would use whatever is the unique identifier for that data. It still views the boxes as the same name. When I submit the form using that name for the checkbox, it passe "%5B%5D=Y&%5B%5D=Y" to the URL of the submit page. It is basically saying 'Chkbox1 = Y and Chkbox1 = Y'. I need it to differentiate b/t the checkboxes... You must be doing something wrong. Even though I wouldn't go that route, there is no reason you should ahve the checkboxes all with the same name. Unless all the categories are named the same. Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253586 Share on other sites More sharing options...
MadTechie Posted May 15, 2007 Share Posted May 15, 2007 you could use EDIT: same mjdamato Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253587 Share on other sites More sharing options...
dzone Posted May 15, 2007 Author Share Posted May 15, 2007 Thanks for the responses. That's getting me closer. However, it still isn't working like I need. I'm using the "GET" form action to pass the variables to the URL so the result page can say 'If Chkbox #1 = Y, then show the prices for that ProductID, etc'. With both methods mentioned (name='{$row['ProductCategry']}[]' & name='categories[]' value='{$row['ProductID']}') it does send a unique name to the URL. But, some of my ProductID's begin the same, and it cuts off the name at the first space. For example: Product 1 = Hot Dog and Prodct 2 = Hot Shot. If both checkboxes are checked, it passes to the URL Hot=Y & Hot=Y, or by the second method, Category=Hot & Category=Hot. I know from this point, the easiest thing to do would be to change the ProductID's in the database, but that isn't an option for me. I have to work with what I'm given. Am I asking for the impossible? My limited PHP knowledge is starting to make me think so. ??? Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253780 Share on other sites More sharing options...
Psycho Posted May 15, 2007 Share Posted May 15, 2007 Then you need to encode the value for the query string using rawurlencode() print ("<td><div align=left valign=middle>"); print ("<input type=checkbox name='categories[]' value='{$row['ProductID']}'> ".rawurlencode($row['ProductCategry'])); print ("</div></td>\n"); Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253792 Share on other sites More sharing options...
yzerman Posted May 15, 2007 Share Posted May 15, 2007 if (!isset($_POST['submit'])) { while ($row = mysql_fetch_array($result)) { if ($row[1] == '1') { $banned = '<font color="red">YES</font>'; } elseif ($row[1] == '0') { $banned = "<form action=\"$_SERVER[php_SELF]\" method=\"post\"><input type=\"checkbox\" name=\"Selected[]\" value=\"$row[0]\" />"; } } } I am going to assume that this is the type of thing you are after. Basically it takes the value of the db entry, and adds it to the post data. Then to parse the selected boxes, if (isset($_POST['submit'])) { foreach ($Selected as $s) { //do something } } Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253806 Share on other sites More sharing options...
sasa Posted May 15, 2007 Share Posted May 15, 2007 try <?php // generate test data $a =array('prvi', 'drugi', 'treci'); foreach ($a as $b) $rows[]['ProductCategry'] = $b; echo '<form>'; foreach ($rows as $row) {//print_r($row); print ("<input type=\"checkbox\" name=\"chk[{$row['ProductCategry']}]\" value=\"Y\"> {$row['ProductCategry']}<br />\n"); } echo '<input type="submit" value="Generate Price List">'; echo '</form>'; if ($_GET['chk']) { echo '<hr /><pre>'; foreach ($_GET['chk'] as $k => $v) { echo "Checkbox $k is checked and it has value $v <br />\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253864 Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 Unusual I know, but I'm going to argue with you here Sasa. Better to make the item id the checkbox value and not the key. This way, to display the selected items you simply <?php $idlist = join(',', $_POST['chk']); $sql = "SELECT * FROM items WHERE id IN ($idlist)"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253883 Share on other sites More sharing options...
Psycho Posted May 15, 2007 Share Posted May 15, 2007 Then you need to encode the value for the query string using rawurlencode() print ("<td><div align=left valign=middle>"); print ("<input type=checkbox name='categories[]' value='{$row['ProductID']}'> ".rawurlencode($row['ProductCategry'])); print ("</div></td>\n"); Ah, I miffed that. Should be like this: print ("<td><div align=left valign=middle>"); print ("<input type=checkbox name='categories[]' value='".rawurlencode($row['ProductID'])."'> {$row['ProductCategry']}"); print ("</div></td>\n"); Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253913 Share on other sites More sharing options...
dzone Posted May 16, 2007 Author Share Posted May 16, 2007 I think I've got it now. Thanks all for your help!!! Quote Link to comment https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-254806 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.