phpclown Posted April 14, 2013 Share Posted April 14, 2013 i have a table that is built off a select statement pulling records from mysql. the table has the following fields. id,transaction_date,description,check_number,amount,transaction_category I import a csv that has all the info but the transaction_category. I want to have a select box that is created from a separate query in each row of the html table. The idea is i can quickly assign a category to a transaction using the drop down and save my changes. I am stuck on to areas. 1. The select box is off by one column in my table 2. Only the first select box has data. I get a correct looking table with all the data and the select dropdown on every row. echo '<table border="1" cellpadding="5" cellspacing="5" class="db-table">'; echo '<tr><th>ID</th><th>Date</th><th>Description</th><th>Check Number</th><th>Amount<th>Category</th></tr>'; while($row = $sql_select->fetch()) { echo '<tr>'; foreach($row as $key=>$value) { echo '<td>',$value,'</td>'; } echo "<td>"; echo "<select name=\"category\">"; while($row2 = mysql_fetch_array($results_cate)) { echo "<option value='".$row2['category']."'>".$row2['category']."</option>"; } echo "</select>"; echo "<td>"; echo "</tr>"; //echo '</tr>'; } echo '</table><br />'; Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/ Share on other sites More sharing options...
jcbones Posted April 14, 2013 Share Posted April 14, 2013 Are all the select boxes the same? Except the name of course. Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424626 Share on other sites More sharing options...
davidannis Posted April 14, 2013 Share Posted April 14, 2013 You need to allow multiple values for category so change this echo "<select name=\"category\">"; to echo "<select name=\"category[]\">"; You will then get an array $_POST['category'][] with all of your category values. Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424627 Share on other sites More sharing options...
davidannis Posted April 14, 2013 Share Posted April 14, 2013 FWIW you can also code it like this echo "<select name=\"category[$value]\">"; and then you get an array $_POST['category'] that looks like this ('myvalue1'=>'mycategory', 'myvalue2'=>'mycat2', ...) Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424631 Share on other sites More sharing options...
phpclown Posted April 14, 2013 Author Share Posted April 14, 2013 well while i was trying the suggestions above i realized i have another issue. I am converting over to PDO and i know nothing about it. Looks like i can't have two querys. Here is what i have. It gives me a php parser error on the second query. I can't test the solution for the html select untill i get this fix i guess. I can post a new topic if thats what i need to do. try { $DBH = new PDO("mysql:host=localhost;dbname=xxxxxx", xxxx, xxxx); } catch(PDOException $e) { echo $e->getMessage(); } $sql_select = $DBH->query('SELECT * FROM `transaction`'); # setting the fetch mode $sql_select->setFetchMode(PDO::FETCH_OBJ); $results = mysql_query($sql_select); //sql_select_cate = $DBH->query('select * from `categories`'); # setting the fetch mode sql_select_cate->setFetchMode(PDO::FETCH_ASSOC); $results_cate = mysql_query($sql_select_cate); Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424695 Share on other sites More sharing options...
Jessica Posted April 14, 2013 Share Posted April 14, 2013 You should uncomment the line that has the query. If you have an error, post it here. Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424706 Share on other sites More sharing options...
phpclown Posted April 14, 2013 Author Share Posted April 14, 2013 here you go. [14-Apr-2013 14:33:09] PHP Parse error: syntax error, unexpected '=' in /Applications/MAMP/htdocs/finance/assigncate.php on line 25 this is line 25 sql_select_cate = $DBH->query('select * from `categories`'); Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424720 Share on other sites More sharing options...
jcbones Posted April 14, 2013 Share Posted April 14, 2013 $ <- you missed it. Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424726 Share on other sites More sharing options...
phpclown Posted April 14, 2013 Author Share Posted April 14, 2013 (edited) wow thats embarassing. Now back to the orginal post. I added the [ ] to the echo "<select name=\"category[]\">"; I still get one drop down with data and it off by one colum. Edited April 14, 2013 by phpclown Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424743 Share on other sites More sharing options...
Solution Barand Posted April 15, 2013 Solution Share Posted April 15, 2013 When you create the first selection you loop through all the rows until none are left. When you try to create the next there are none to read. Create the options first and store in a string var then just output the string each time. Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1424751 Share on other sites More sharing options...
phpclown Posted April 16, 2013 Author Share Posted April 16, 2013 Thanks Barand that works. I also realized that my problem with the select box being off by a row was do to returning an extra column in my sql query. I am going to mark this solved. Thanks everyone for the help. Quote Link to comment https://forums.phpfreaks.com/topic/276921-html-select-inside-table/#findComment-1425216 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.