Thundarfoot Posted January 17, 2008 Share Posted January 17, 2008 I am trying to make a drop down list form the drop box gets its choices from 1 table, then when the user clicks submit the value gets stored into a variable $name and is then used in a query to output the data of another table. Everything works great except the default option of ALL. I do not know what value to put in that will cause the query to return all records. here is the code thanks in advance for any help you can lend... //the query $query_Cards = "SELECT Name, `Class`, FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC"; //the form <form id="form1" name="form1" method="post" action=""> <label>Class <select name="select"> <option value=" ">ALL</option> <?php do { ?> <option value="<?php echo $row_Class['Class']?>"><?php echo $row_Class['Class']?></option> <?php } while ($row_Class = mysql_fetch_assoc($Class)); $rows = mysql_num_rows($Class); if($rows > 0) { mysql_data_seek($Class, 0); $row_Class = mysql_fetch_assoc($Class); } ?> </select> </label> <input type="submit" name="submit" value="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/ Share on other sites More sharing options...
adam291086 Posted January 17, 2008 Share Posted January 17, 2008 Well you can keep the option as 'All'. Purley becasue a user won't know the * means everything. Then where you process the form, you get the variable $name. You can check to see if it =='All'. If it == 'All' the $name = *. Then run your query using *. Else run the query using the value $name Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441704 Share on other sites More sharing options...
Thundarfoot Posted January 17, 2008 Author Share Posted January 17, 2008 I am sorry, I really should make a point of saying I am a noob in my post, kinda like my stupid sign.... Makes sense, but I havent touched statements yet.. I am thinking if I added a period to each entry I could have ALL = . in the first table so as to fill $name with a . when ALL is chosen, then as long as each record has a period in it it should be returned and I wont have to learn if else then yet Seems a fanagled way to go about it but should work yeah? Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441710 Share on other sites More sharing options...
adam291086 Posted January 17, 2008 Share Posted January 17, 2008 If stantement are easy. You should learn them now. I am not sure if your alternative will work, as i dont quiet understand what you mean. You if statement will look like this If ($name == All) { $name = "*"; } //then you do your query here. Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441711 Share on other sites More sharing options...
Thundarfoot Posted January 17, 2008 Author Share Posted January 17, 2008 Thank you for your code example, but I am having trouble making it fit.... This is the query statment, along with some other things like the $name //Database Cards Connect $name = $_POST['select']; mysql_select_db($database_Lw, $Lw); $query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC"; $Cards = mysql_query($query_Cards, $Lw) or die(mysql_error()); $row_Cards = mysql_fetch_assoc($Cards); $totalRows_Cards = mysql_num_rows($Cards); could you help me put the puzzle together.... thanks again Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441735 Share on other sites More sharing options...
adam291086 Posted January 17, 2008 Share Posted January 17, 2008 try $name = $_POST['select']; If ($name == All) { $name = "*"; } //Database Cards Connect mysql_select_db($database_Lw, $Lw); $query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE `Class` LIKE '$name%' ORDER BY Name ASC"; $Cards = mysql_query($query_Cards) or die(mysql_error()); $row_Cards = mysql_fetch_assoc($Cards); $totalRows_Cards = mysql_num_rows($Cards); Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441737 Share on other sites More sharing options...
Thundarfoot Posted January 17, 2008 Author Share Posted January 17, 2008 That did not work.. I am still pecking around at it trying to figure it out, but so far no luck Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441765 Share on other sites More sharing options...
adam291086 Posted January 17, 2008 Share Posted January 17, 2008 show me all your code? Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441792 Share on other sites More sharing options...
papaface Posted January 17, 2008 Share Posted January 17, 2008 <option value=" ">ALL</option> should be: <option value="All">ALL</option> and If ($name == All) { $name = "*"; } should be: If ($name =="All") { $name = "*"; } Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441810 Share on other sites More sharing options...
Thundarfoot Posted January 17, 2008 Author Share Posted January 17, 2008 Ok that didnt work either. So here is the whole code as it was prior to trying to insert if statement. Thanks again for the help. <?php require_once('../Connections/Lw.php'); ?> <?php //Database Class Connect mysql_select_db($database_Lw, $Lw); $query_Class = "SELECT `Class` FROM class_list ORDER BY id ASC"; $Class = mysql_query($query_Class, $Lw) or die(mysql_error()); $row_Class = mysql_fetch_assoc($Class); $totalRows_Class = mysql_num_rows($Class); //Database Cards Connect $name = $_POST['select']; mysql_select_db($database_Lw, $Lw); $query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC"; $Cards = mysql_query($query_Cards, $Lw) or die(mysql_error()); $row_Cards = mysql_fetch_assoc($Cards); $totalRows_Cards = mysql_num_rows($Cards); <body> <form id="form1" name="form1" method="post" action=""> <label>Class <select name="select"> <option value="All">ALL</option> <?php do { ?> <option value="<?php echo $row_Class['Class']?>"><?php echo $row_Class['Class']?></option> <?php } while ($row_Class = mysql_fetch_assoc($Class)); $rows = mysql_num_rows($Class); if($rows > 0) { mysql_data_seek($Class, 0); $row_Class = mysql_fetch_assoc($Class); } ?> </select> </label> <input type="submit" name="submit" value="submit"> </form> <table border="1" cellpadding="5" cellspacing="5"> <tr> <td>Name</td> <td>Class</td> <td>Drops</td> <td>Comments</td> <td>Pic</td> </tr> <?php do { ?> <tr> <td><?php echo $row_Cards['Name']; ?></td> <td><?php echo $row_Cards['Class']; ?></td> <td><?php echo $row_Cards['Drops']; ?></td> <td><?php echo $row_Cards['Comments']; ?></td> <td><?php echo $row_Cards['Pic']; ?></td> </tr> <?php } while ($row_Cards = mysql_fetch_assoc($Cards)); ?> </table> <br /> </body> </html> <?php //the following lines of code, does this mean when the page refreshes, or the submit button is clicked that the $name variable gets erased? if not what does it mean please? mysql_free_result($Class); mysql_free_result($Cards); ?> Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441974 Share on other sites More sharing options...
roopurt18 Posted January 17, 2008 Share Posted January 17, 2008 Change <?php $query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC"; ?> to <?php $where = '1' if(strlen($name)){ $where = "skill_cards.`Class` LIKE '{$name}%'"; } $sql = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE {$where} ORDER BY Name ASC"; (edit) And change <option value="All">ALL</option> to <option value="">ALL</option> Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-441985 Share on other sites More sharing options...
Thundarfoot Posted January 17, 2008 Author Share Posted January 17, 2008 I made your changes, but still option ALL returns zero records. Picking any other choice results in proper records ouptut. Thank you for your help though Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442234 Share on other sites More sharing options...
roopurt18 Posted January 17, 2008 Share Posted January 17, 2008 When all else fails, echo the query before you send it to mysql_query() and copy and paste it directly into phpMyAdmin. Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442244 Share on other sites More sharing options...
Thundarfoot Posted January 17, 2008 Author Share Posted January 17, 2008 I guess I dont understand, what I would be pasting into phpmyadmin? Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442268 Share on other sites More sharing options...
roopurt18 Posted January 17, 2008 Share Posted January 17, 2008 <?php $sql = "YOUR MYSQL STATEMENT HERE"; echo $sql; ?> That will echo your MySQL query. You can then run it in phpMyAdmin and see the output directly. Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442272 Share on other sites More sharing options...
Thundarfoot Posted January 17, 2008 Author Share Posted January 17, 2008 Ok....but why would I want to? Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442276 Share on other sites More sharing options...
Thundarfoot Posted January 18, 2008 Author Share Posted January 18, 2008 sorry if that sounded rude...I re-read it and it kinda does. Im just lost lol Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442282 Share on other sites More sharing options...
Thundarfoot Posted January 18, 2008 Author Share Posted January 18, 2008 FIXED! I changed $query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC"; to $sql = $query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC"; echo $sql; To get the query output to go run in phpadming to check it for errors (thank you for the tip!) however adding $sql = seems to make the script work! I removed the echo and it still works, remove the $sql = and it breaks. Thanks a ton I have been at this for 3 days....lol VICTORY IS MINE errrr or is it yours Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442289 Share on other sites More sharing options...
roopurt18 Posted January 18, 2008 Share Posted January 18, 2008 Ok....but why would I want to? Looks like you figured that out. Remember to mark your topics as solved in the future. Also, when debugging, you have to take steps to determine what is wrong. You can only fix the problem if you have good information about what the problem is. Echo statements are a quick way to track program execution paths, the data in variables, etc. Just make sure you remove them when you're done. Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442296 Share on other sites More sharing options...
Thundarfoot Posted January 18, 2008 Author Share Posted January 18, 2008 I understand thank you for taking the time to explain it. Quote Link to comment https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/#findComment-442475 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.