hoopplaya4 Posted December 4, 2008 Share Posted December 4, 2008 Hi All, Using PHP & MySQL. I've been trying to work out the logic on this one for a while, but I just can't figure it out. But, I think I'm close!! I have several First and Last names in a form, which are in an Option field that is being pulled from the database. Users have the ability to select multiple options at once. As a result, these are being $_POST'd as an array. Once the form is submitted, I'd like to echo back all the First & Last names that were posted. Right now, it only displays one name. Here's the Form: <form name="filterplayer" method="post" action="<?= $_SERVER['PHP_SELF'] ?>?show=2"> <select multiple name="players[]" id="players" size="5"> <?php $sql = "SELECT usrID, usrFirst, usrLast, usrPosition"; $sql .= " FROM tblUsers"; $sql .= " WHERE usrActive = 1"; $sql .= " ORDER BY usrLast"; require("../connection.php"); $rs=mysql_db_query($DBname,$sql,$link); if ($rs) { while ($row=mysql_fetch_array($rs)){ print("<option value='" . $row["usrID"] . "'>"); print($row["usrFirst"] . " " . $row["usrLast"] ); print(" (" . $row["usrPosition"] . ")</option>\n"); } // end while } else { // No Events found print("No Active players"); mysql_close($link); } // end else ($rs) ?> </select><br> <input type="submit" value="Show Availability" /> </form> And here is the PHP to Display those options that were Posted: <?php $pname = $_POST['players']; // Get The Form Values $new =implode(' and ',$pname); ////// IMPLODE VALUES //////// $sql = "SELECT usrID, usrFirst, usrLast"; $sql .= " FROM tblUsers"; $sql .= " WHERE usrID = $new"; require("../connection.php"); $rs=mysql_db_query($DBname,$sql,$link); if ($rs) { while ($row=mysql_fetch_array($rs)){ $first = $row['usrFirst']; $last = $row['usrLast']; $full = $first . " " . $last; print ("<div>"); print $full; print ("</div>"); } //end while } // end if ?> And all that it prints is: (although I'm selecting multiple values). John Smith Quote Link to comment https://forums.phpfreaks.com/topic/135455-display-array-from-_post/ Share on other sites More sharing options...
premiso Posted December 4, 2008 Share Posted December 4, 2008 You are missing the close bracket to the foreach: foreach($_POST['players'] as $pz){ $sql = "SELECT usrID, usrFirst, usrLast"; $sql .= " FROM tblUsers"; $sql .= " WHERE usrID = $pz"; } require("../connection.php"); $rs=mysql_db_query($DBname,$sql,$link); if ($rs) { while ($row=mysql_fetch_array($rs)){ $first = $row['usrFirst']; $last = $row['usrLast']; $full = $first . " " . $last; print ("<div>"); print $full; print ("</div>"); } //end while } // end if } // end foreach ?> There should have been a syntax error thrown on that. but yea. Hopefully that is the issue. Quote Link to comment https://forums.phpfreaks.com/topic/135455-display-array-from-_post/#findComment-705686 Share on other sites More sharing options...
hoopplaya4 Posted December 4, 2008 Author Share Posted December 4, 2008 Hi Premiso: Thanks for the reply. My apologies, I posted the wrong code. I've corrected it in my original post above. Perhaps you can take a look at it. Should I put a 'for each' in there? If so, how would I put it in? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/135455-display-array-from-_post/#findComment-705690 Share on other sites More sharing options...
premiso Posted December 4, 2008 Share Posted December 4, 2008 Only if $_POST['players'] is an array. The updated code works as expecte you only give it one userid. You would need an array of players id's somehow for it to work like you want it to. Quote Link to comment https://forums.phpfreaks.com/topic/135455-display-array-from-_post/#findComment-705691 Share on other sites More sharing options...
hoopplaya4 Posted December 4, 2008 Author Share Posted December 4, 2008 Okay, thanks. Any ideas of how I might set this up, or should I take it from a completely different approach? Quote Link to comment https://forums.phpfreaks.com/topic/135455-display-array-from-_post/#findComment-705692 Share on other sites More sharing options...
hoopplaya4 Posted December 5, 2008 Author Share Posted December 5, 2008 Does anyone have any other ideas on how I might achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/135455-display-array-from-_post/#findComment-706353 Share on other sites More sharing options...
mrdamien Posted December 5, 2008 Share Posted December 5, 2008 The problem is the SQL. (assuming usrID is the primarykey) SELECT * FROM `tblUsers` WHERE usrID = 1 and 2 and 3 That will only return 1 row. Try $pname = $_POST['players']; // Get The Form Values $new =implode(', ',$pname); $sql = "SELECT * FROM `tblUsers` WHERE usrID IN (" . $new . ")"; Quote Link to comment https://forums.phpfreaks.com/topic/135455-display-array-from-_post/#findComment-706491 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.