AL123 Posted February 28, 2011 Share Posted February 28, 2011 I am new to coding and I am trying to write a simple business registration app. a foreach loop populates a select list and a nested loop checks to see if something new is being added. If so write to the DB. Great. Only part of it is working, I am getting the correct businessId but the categoryId is an array: 188 Array 189 Array 190 Array 191 Array 192 Array 195 Array 196 Array Here is the code: <form method="post" action="?"> <table> <tr><td class="picklist"><?php echo $pickMessage; ?> <select name="bizCatSelect[]" size="4" multiple> <?php $sql = "SELECT categoryId FROM categories"; $sth = $dbh->prepare($sql); $sth-> execute(); $result = $sth->fetchAll(PDO::FETCH_COLUMN, 0); foreach($result as $value) { if($addRecord == 1) { $selected = false; if(in_array($value, $bizCatSelect)) // $row[1] { $sql = "INSERT INTO businessCat(businessId,categoryId) VALUES(:bizId, :bizCatSelect)"; $sth = $dbh->prepare($sql); $sth->bindValue(':bizId', $bizId, PDO::PARAM_STR); $sth->bindValue(':bizCatSelect', $bizCatSelect, PDO::PARAM_STR); $query = $sth->execute(); $params = array($bizId, $bizCatSelect); // $row[0] $value print_r($params); //$resp = $sth->execute($query, $params); echo "<option selected=\"$value\">$value</option>\n"; // $row[1] $selected = true; } if($selected == false){echo "<option value=\"$value\">$value</option>\n";} } else{ echo "<option value=\"$value\">$value</option>\n";} //echo "<option value=\"$value\">$value</option>\n"; } ?> </select> </td> <td class="addlist"> <table> <tr><td class="formLable">Business Name:</td> <td><input type="text" name="bizName" size="40" maxlength="255" value="<?php echo $bizName; ?>"></td> </tr> <tr><td class="formLable">Address:</td> <td><input type="text" name="bizAddress" size="40" maxlength="255" value="<?php echo $bizAddress; ?>" ></td> </tr> <tr><td class="formLable">City:</td> <td><input type="text" name="bizCity" size="40" maxlength="128" value="<?php echo $bizCity; ?>" ></td> </tr> <tr><td class="formLable">Telephone:</td> <td><input type="text" name="bizTele" size="40" maxlength="64" value="<?php echo $bizTele; ?>" ></td> </tr> <tr><td class="formLable">URL:</td> <td><input type="text" name="bizUrl" size="40" maxlength="255" value="<?php echo $bizUrl; ?>" ></td> </tr> </table> </td> </tr> </table> <p><input type="hidden" name="addRecord" value="1"> <?php if($addRecord == 1) {echo "<p><a href=\"?\">Add another business</a></p>";} else {echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Add Business\">";} ?> </p> </form> I just can't seem to get this last part, any suggestions? AL Link to comment https://forums.phpfreaks.com/topic/229189-array-extract/ Share on other sites More sharing options...
Zane Posted February 28, 2011 Share Posted February 28, 2011 obviously $value is an array what happens when you change all $value to $value[0] in the areas outlined for elements. Link to comment https://forums.phpfreaks.com/topic/229189-array-extract/#findComment-1180998 Share on other sites More sharing options...
samoht Posted February 28, 2011 Share Posted February 28, 2011 you set the value to an array here: <select name="bizCatSelect[]" size="4" multiple> Link to comment https://forums.phpfreaks.com/topic/229189-array-extract/#findComment-1180999 Share on other sites More sharing options...
AL123 Posted February 28, 2011 Author Share Posted February 28, 2011 I changed every thing to $value[0] and only the first letter of my array is chosen. EX: echo "<option value=\"$value[0]\">$value[0]</option>\n";} I also tryed this to no avail. if(in_array($value[0], $bizCatSelect)) And both together. Do I need to do 2 insert statements or maybe run another loop on $value??? AL Link to comment https://forums.phpfreaks.com/topic/229189-array-extract/#findComment-1181003 Share on other sites More sharing options...
AL123 Posted March 1, 2011 Author Share Posted March 1, 2011 I changed $bizCatSelect to $value in the bindparam and it Works. guess I just needed to post - AL Link to comment https://forums.phpfreaks.com/topic/229189-array-extract/#findComment-1181010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.