richrock Posted January 7, 2009 Share Posted January 7, 2009 Maybe it's because I've got stinkin' man flu or what, I just can't get this right.... I need to show a list of products, with tickboxes next to each, so that people can express an interest in them. I have the database fields set up, and customer name set up too. Here's my current code: // Get the clients $getClients = "SELECT user_id, firstname, lastname FROM jos_comprofiler ORDER BY firstname ASC"; $resClients = mysql_query($getClients) or die(mysql_errno() . mysql_error()); // Generate User Lists $row_selectuser = mysql_fetch_assoc($resClients); $totalRows_selectuser = mysql_num_rows($resClients); ?> <form action="sale.php" method="post" name="processtele"> <input type="hidden" name='telebids' value='telebids' /> <input type="hidden" name="updatetele" value="updatetele" /> <tr> <td width="150"><?php echo PAD_select_client; ?></td> <td> <select name="getTELE"> <option>-- Select a Name --</option> <?php do { echo "<option value='".$row_selectuser['id']."'>".$row_selectuser['firstname']." ".$row_selectuser['lastname']."</option>"; } while ($row_selectuser = mysql_fetch_assoc($resClients)); $rows = mysql_num_rows($resClients); if($rows > 0) { mysql_data_seek($resClients, 0); $row_selectuser = mysql_fetch_assoc($resClients); } ?> </select> </td> </tr> </table> <br /> <table width="928" cellpadding="0" cellspacing="0" border="0"> <?php // Get the sale $getSALE = "SELECT lot_order, title, cat, jos_bid_auctions.id AS bidid FROM jos_bid_auctions LEFT JOIN jos_bid_categories ON jos_bid_categories.id WHERE jos_bid_categories.active = 1 AND jos_bid_categories.id = jos_bid_auctions.cat"; $resSALE = mysql_query($getSALE) or die(mysql_errno() . mysql_error()); while ($row_lots = mysql_fetch_array($resSALE)) { echo "<tr>\n"; echo "<td width='50'>\n"; echo $row_lots['lot_order']; echo "</td>\n"; echo "<td>\n"; echo $row_lots['title']; echo "</td>\n"; echo "<td>\n"; echo $row_lots['cat']; echo "</td width='50'>\n"; echo "<td>\n"; echo "<input type='hidden' name='declare[]' value='".$row_lots['bidid']."' />"; echo "<input type='checkbox' name='telebidset[]' value='".$row_lots['bidid']."' />"; echo "</td>\n"; echo "</tr>\n"; } echo "<tr><td colspan='4'><br /><br />"; echo "Save Telephone Requests: "; //echo "</td><td colspan='3'>"; echo "<input type='submit' name='submittele' value='Save' />"; echo "</td></tr>"; ?> </form> </table> <?php } I've never had much luck with arrays... When I get some spare time I'll do some proper learning of them, but deadlines are tight... I am aware of using [] like this: name='something[]' , but with checkboxes, I can't get the fields to match up. For example, if I had 5 items, and selected the 2nd and 3rd, the checkbox would show [0] => id2 and [1] => id3, but the declare[] results with [0] => id1 and [1] => id2. Heres the POST result of selecting the second item on it's own: [declare] => Array ( [0] => 13 [1] => 14 ) [telebidset] => Array ( [0] => 14 ) This obviously screws up the results, and then I need to insert the results into the database using update. Would I be right in guessing that I need to use a foreach loop to do the insert? If this is right I can use the id because that's the ID number in the database, and that's pretty easy. Help Quote Link to comment Share on other sites More sharing options...
cytech Posted January 7, 2009 Share Posted January 7, 2009 Hey, You have the right concept - normally I would do something like this: $array_items = array("item1", "item2", "item3"); foreach($items as $key=>$val){ echo "<input type=\"checkbox\" name=\"items[]\" value=\"{$val}\" /> $val <br />"; } Then in your post: echo "The user selected: "; foreach($_POST['items'] as $key=>$val){ echo $val.", "; // can do inserts/updates here } Yep, you can do a foreach loop just like above to do your inserts/updates. Quote Link to comment Share on other sites More sharing options...
richrock Posted January 7, 2009 Author Share Posted January 7, 2009 Thanks! Glad I'm heading in the right direction I'm doing it all in a while... loop, is that wrong? Also, the problem I have with arrays is this $array_items = array("item1", "item2", "item3"); Sounding really dull, but what do I do with this, as this information is from a database. Would I be right again in thinking that everything for the table row can be placed in the foreach loop underneath this? Quote Link to comment Share on other sites More sharing options...
cytech Posted January 7, 2009 Share Posted January 7, 2009 How your displaying the information is fine(while loop), you just need to do a foreach loop on the post once the form gets submitted. So in your case it would be: foreach($_POST['declare'] as $key=>$val){ echo $val."<BR>"; } The above would display as: 13 14 (given of course you selected those two items). Quote Link to comment Share on other sites More sharing options...
richrock Posted January 7, 2009 Author Share Posted January 7, 2009 You are an absolute star! Now I can use that to do the update to the database, and get on with my work Quote Link to comment Share on other sites More sharing options...
cytech Posted January 7, 2009 Share Posted January 7, 2009 Glad to help 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.