Jump to content

html select inside table


Go to solution Solved by Barand,

Recommended Posts

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 />';
Link to comment
https://forums.phpfreaks.com/topic/276921-html-select-inside-table/
Share on other sites

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);
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.