frank13 Posted August 18, 2008 Share Posted August 18, 2008 Sup peeps, I am having a problem with my code. The option i select don't work, it always the latest entry of the table that the value of the variable. <form action="<?=$PHP_SELF?>" method="post" id="type"> <select name="category"> <?php $sql="select * from client"; $result=mysql_query($sql); while($ze = mysql_fetch_array($result)) { $sub[] = $ze['type']; } $newData = array_unique($sub); foreach ($newData as $dropDown) { ?> <option value="<? echo $dropDown?>"><? echo $dropDown?></option> <?php } ?> <input type="submit" value="show" /> </select> </form> thx in advance. peace Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/ Share on other sites More sharing options...
steelmanronald06 Posted August 18, 2008 Share Posted August 18, 2008 Try: <?php $categories=array(); $sql="SELECT * FROM client"; $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)){ $categories[]=$row['type']; } echo '<form action="' . $PHP_SELF . '" method="post" id="type"> <select name="category">'; foreach ($categories as $category){ echo "<option >$category</option>\n"; } echo ' </select> <input type="submit" value="show" /> </form> '; ?> I think the main problem might have been your </select> was after the <input type...etc....>. Anyways, cleaned it up a bit, put it in order, and didn't break out of php so much. This way it is cleaner and easier to read and hopefully you'll be able to understand it a little better. Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-618902 Share on other sites More sharing options...
frank13 Posted August 18, 2008 Author Share Posted August 18, 2008 thank you for the fast awnser. I tried your code, but i get the some problem whatever value a select, only the last entry of de table is selected. I verified by a echo "$category"; below the code thx in advance peace Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-619343 Share on other sites More sharing options...
steelmanronald06 Posted August 18, 2008 Share Posted August 18, 2008 Can i see your Insert code? The block that inserts the value from the drop box into the database. Actually, you're using $PHP_SELF, so it should all be the same page, so can you post all the code here. If it is over 100 lines use pastebin. Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-619430 Share on other sites More sharing options...
frank13 Posted August 18, 2008 Author Share Posted August 18, 2008 I am not trying do insert data in the table, I want to show a certain data : code : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php @include ("php/connect.php"); $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF']; ?> <?php $sql="select * from interior order by type asc"; $result=mysql_query($sql); //$categories=array_unique(); while($row=mysql_fetch_assoc($result)){ $categories[]=$row['type']; } echo '<form action="' . $PHP_SELF . '" method="post" id="type"> <select name="category">'; foreach ($categories as $category){ echo "<option >$category</option>\n"; } echo ' </select> <input type="submit" value="show" /> </form> '; ?> <br /> <?php echo "$category"; ?> <br /> <?php $result=mysql_query("select * from interior where type='$category'"); $i = 0; while($row=mysql_fetch_assoc($result)) { ?> <p><? echo $row['name']; ?><br /> <? echo $row['type']; ?></p> <?php $i+=1; } ?> </body> </html> The only data selected is the lastest insert. Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-619499 Share on other sites More sharing options...
steelmanronald06 Posted August 19, 2008 Share Posted August 19, 2008 Where are you telling the difference between data and form? You should an if statment like so: if (!submit) { code for showing the form } else { code for showing the chosen category } On top of that, you need to grab the form data when the page refreshes using $POST['category'];, assign that to $category, and go from there. See if you can work with that. if you run into more problems, post what you have and I'll sit down and write up some actual code. Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-619768 Share on other sites More sharing options...
frank13 Posted August 19, 2008 Author Share Posted August 19, 2008 I added below the code of the dropdown : <?php $select= isset($_POST['select']) ? $_POST['select'] : ''; ?> <?php if (!empty($_POST['select'])) { $sql = mysql_query("SELECT * FROM interior WHERE type='$select' ORDER BY ncontractor", $connection) or die("error querying database"); $i = 0; while($result = mysql_fetch_assoc($sql)) ... { The code was not working because a forgot to put $_POST , now it works. By adding the if I don't get a error when nothing was selected. So for now all is working fine. Thank for your help. Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-619792 Share on other sites More sharing options...
frank13 Posted August 19, 2008 Author Share Posted August 19, 2008 ps. Oh, yeah i changed category to select in the code that I posted here. Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-619798 Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Share Posted August 19, 2008 Any chance you could set the topic as solved? Link to comment https://forums.phpfreaks.com/topic/120132-solved-problem-dropdown-select-list-with-table/#findComment-619805 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.