• 0

(PHP) Dynamically generate tabindex from MySQL query?


Question

Hello,

Ive been trying to put together a stock search system with PHP and MySQL - so far so good. The data is displayed in a table and to aid usuability i want to populate some cells with a tabindex so users can tab through the results table - but im not sure how.

Here is (what i think is) the relevent part of my code:

// Search the database
		$query = mysql_query("SELECT * FROM `Stock Search` WHERE `Part Number` LIKE '%$PartSearch%'") or die(mysql_error());

		// Display the results
		$NumberResults = mysql_num_rows($query);

		// If no results are found
		if ($NumberResults == 0) {
			echo 'No results were found for: "' . $PartSearch . '"<br />';
			echo '<a href="/search.php">Click here</a> to go back <br />';
		} 

		// Display the reuslts that are found
		else
		{
			echo $NumberResults . ' matching parts found: <br />';

			// Show the results table and build a new form
			echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
			echo '<table border="1" align="center">';
			echo '<tr>';
			echo '<th>RFQ</th>';
			echo '<th>Part Number</th>';
			echo '<th>Manufacturer</th>';
			echo '<th>Stock</th>';
			echo '<th>Quantity</th>';
			echo '<th>Delivery</th>';
			echo '</tr>';
			while ($rows = mysql_fetch_array($query))
			{
				echo '<tr>';
				echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
				echo '<td>' . $rows['Part Number'] . '</td>';
				echo '<td>' . $rows['Manufacturer'] . '</td>';
				echo '<td>' . $rows['Stock'] . '</td>';
				echo '<td><input type="text" name="Quantity" /></td>';
				echo '<td><select name="Delivery">
						<option value="Any">Any</option>
 	<option value="Emergency">Emergency</option>
 	<option value="Next Day">Next Day</option>
 	<option value="2-3">2-3 Days</option>
 	<option value="4-6">4-6 Days</option>
 	<option value="7-10">7-10 Days</option>
 	<option value="10+">10+ Days</option>
					</select></td>';
				echo '</tr>';
			}
			echo '</table>';

I want to have a tabindex on each Quantity and Delivery cell, but im not sure how to achieve it? (To be honest i dont think it helps that i dont fully understand how the while query works).

Any help would be greatly appreciated

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Here you go. Untested but it's so basic it shouldn't need testing.

// Search the database
 $query = mysql_query("SELECT * FROM `Stock Search` WHERE `Part Number` LIKE '%$PartSearch%'") or die(mysql_error());

 // Display the results
 $NumberResults = mysql_num_rows($query);

 // If no results are found
 if ($NumberResults == 0) {
 echo 'No results were found for: "' . $PartSearch . '"<br />';
 echo '<a href="/search.php">Click here</a> to go back <br />';
 } 

 // Display the reuslts that are found
 else
 {
 echo $NumberResults . ' matching parts found: <br />';

 // Show the results table and build a new form
 echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
 echo '<table border="1" align="center">';
 echo '<tr>';
 echo '<th>RFQ</th>';
 echo '<th>Part Number</th>';
 echo '<th>Manufacturer</th>';
 echo '<th>Stock</th>';
 echo '<th>Quantity</th>';
 echo '<th>Delivery</th>';
 echo '</tr>';
 $tabindex = 0;
 while ($rows = mysql_fetch_array($query))
 {
 echo '<tr>';
 echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
 echo '<td>' . $rows['Part Number'] . '</td>';
 echo '<td>' . $rows['Manufacturer'] . '</td>';
 echo '<td>' . $rows['Stock'] . '</td>';
 $tabindex++;
 echo '<td><input type="text" name="Quantity" tabindex="' . $tabindex . '" /></td>';
 $tabindex++;
 echo '<td><select name="Delivery" tabindex="' . $tabindex . '">
 <option value="Any">Any</option>
 <option value="Emergency">Emergency</option>
 <option value="Next Day">Next Day</option>
 <option value="2-3">2-3 Days</option>
 <option value="4-6">4-6 Days</option>
 <option value="7-10">7-10 Days</option>
 <option value="10+">10+ Days</option>
 </select></td>';
 echo '</tr>';
 }
 echo '</table>';

Link to comment
Share on other sites

  • 0

Perfect thank you. So simple!

I actually modiefied it a bit to simply:

			$tabindex = 0;
			while ($rows = mysql_fetch_array($query))
			{
				echo '<tr>';
				echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
				echo '<td>' . $rows['Part Number'] . '</td>';
				echo '<td>' . $rows['Manufacturer'] . '</td>';
				echo '<td>' . $rows['Stock'] . '</td>';
				echo '<td><input type="text" name="Quantity" tabindex="' . $tabindex++ .'" /></td>';
				echo '<td><select name="Delivery" tabindex="' . $tabindex++ .'">
						<option value="Any">Any</option>
 	<option value="Emergency">Emergency</option>
 	<option value="Next Day">Next Day</option>
 	<option value="2-3">2-3 Days</option>
 	<option value="4-6">4-6 Days</option>
 	<option value="7-10">7-10 Days</option>
 	<option value="10+">10+ Days</option>
					</select></td>';
				echo '</tr>';

Cheers

Link to comment
Share on other sites

  • 0

I have another (more indepth) question, that i was looking for some help for:

As you can see from the code above, the search query outputs a table. The user first checks a row corresponding to an item they want (the RFQ column) and then one of the columns (called Quantity), in where the user can enter a quantity of the product they want. They then click a button and a request for quotation will be sent to the company (at the moment im just trying to echo out the selections made).

The query and output:

			while ($rows = mysql_fetch_array($query))
			{
				echo '<tr>';
				echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
				echo '<td>' . $rows['Part Number'] . '</td>';
				echo '<td>' . $rows['Manufacturer'] . '</td>';
				echo '<td>' . $rows['Stock'] . '</td>';
				echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[]" tabindex="' . $tabindex++ .'" /></td>';
				echo '<td><select name="Delivery" tabindex="' . $tabindex++ .'">
						<option value="Any">Any</option>
 	<option value="Emergency">Emergency</option>
 	<option value="Next Day">Next Day</option>
 	<option value="2-3">2-3 Days</option>
 	<option value="4-6">4-6 Days</option>
 	<option value="7-10">7-10 Days</option>
 	<option value="10+">10+ Days</option>
					</select></td>';
				echo '</tr>';
			}
			echo '</table>';
			echo $rows;

			// Send request for quotation
			echo '<input type="submit" name ="SendRFQ" value="Request Quotation">';
			echo '</form>';

Which is then displayed:

// Check if parts have been selected for quotation already
	if (isset($_POST['SendRFQ']))
	{
		$RFQ = $_POST['RFQ'];
		$NumberRFQ = count($RFQ);

		// List parts selected for quotation
		if ($NumberRFQ>0)
		{
			echo 'RFQs chosen: '.$NumberRFQ.'<br /><br />';
			echo 'You chose the following:<br /<br />';

			echo '<table border="1" align="center">';
			echo '<tr><th>Part Number</th>';
			echo "<th>Quantity?</th></tr>";

			for ($i=0; $i<$NumberRFQ; $i++)
			{
				echo '<tr>';
				echo '<td>' . $RFQ[$i] . '</td>';
				echo '<td>' . $Quantity[$i] . '</td>';
				echo '</tr>';


			}
			echo "</table>";
			echo '<br /><br />';
		}
		else
		{
			echo 'No parts have been chosen for quotation<br /><br />';
			echo '<a href="/search.php">Click here</a> to go back<br /><br />';
		}
	}

However i cant get the quantities that are entered to display correctly. I think its something to do with the Quantity array and the for loop (vague i know) but i have no idea how to fix it?

Any ideas?

(I may start a new topic, as this is kinda off track from the OP!)

Thanks

Link to comment
Share on other sites

  • 0

I have another (more indepth) question, that i was looking for some help for:

...

I think its something to do with the Quantity array and the for loop (vague i know) but i have no idea how to fix it?

According to the code you just pasted, you don't have a Quantity array. All you have is an RFQ array...

$RFQ = $_POST['RFQ'];

Link to comment
Share on other sites

  • 0

According to the code you just pasted, you don't have a Quantity array. All you have is an RFQ array...

$RFQ = $_POST['RFQ'];

Thanks for the reply.

I added in:

$Quantity=$_POST['Quantity'];

Which kinda works.

If i select say, the first three rows sequentially, the quantities are displayed correctly.

But say i select rows 1, 3 and 5 (and enter quantaties as 1, 3 and 5 respecitively), then row 1 shows quantity 1, row 2 shows nothing and row 3 shows quantity 3?!

Any ideas?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.