Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253586
Share on other sites

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. ???

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253780
Share on other sites

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");

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253792
Share on other sites

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
}
}

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253806
Share on other sites

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";
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253864
Share on other sites

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)";
?>

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253883
Share on other sites

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");

Link to comment
https://forums.phpfreaks.com/topic/51490-checkboxes-in-arrays/#findComment-253913
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.