Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Some PHP Help please


NodsDorf

Recommended Posts

Posted

I'm trying to learn so forgive what probably is a basic question.

 

I'm trying to make a select multiple box, that populates from a database. In turn when a user selects 1 or more items a query is performed, and the results of that row are echoed into two different cells.

 

I have been really trying to get this to function myself with no great success. I've read a ton of stuff but I can only seem to get 1 particular item to work at a time.

 

As of now I have been able to retrieve data from my MySQL database to populate my Select box, but I can not get the other data from the selection to display the results.

 

I've tried javascript with php, and php alone, to no avail.

 

I would really appreciate any help someone can give me to accomplish this.

 

Here is the way the Table I'm working with looks; table name is chemicals

| id | | chemical | | nitrile | <--- columns

1, chemical name, value <---row 1

 

Here is the code I have so far

can be seen here

<?
$con = mysql_connect("localhost","xxx","xxx","xxx");
		if (!$con)
	 {
			 die('Could not connect: ' . mysql_error());
		 }
		mysql_select_db("xxx", $con);

$sql="SELECT * FROM chemicals";
$result=mysql_query($sql); 
$options="";

while ($row=mysql_fetch_array($result)) {
$fluid=$row["chemical"];
$rating=$row["nitrile"];
$id=$row["id"];
$options.="<OPTION VALUE=\"$id\">".$fluid.'</option>';

}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
	<table width="850" border="1">
	<tr valign="top">
	<th width="350" scope="row"><div align="left">
	<select name="test[]" size="7" multiple="multiple" class="select1">
	<option value=0>... Selections
	<?=$options?>
	</option>
	</select>
	</div></th>
	<td width="375" rowspan="2"><div align="center"><strong>Fluid Selection</strong></div></td>
	<td width="103" rowspan="2"><div align="center"><strong>Fluid Rating</strong></div></td>
	</tr>
	<tr>
	<th width="350" scope="row"><div align="right">
	<input type="reset" /> 
	<input type="submit" value="submit" name="submit" />
	</div></th> 
	</tr>
<?php 
//THIS ISN'T WORKING
	$test=$_POST['$options'];
		if ($test){
		foreach ($test as $t){
		echo "<tr valign='top'>" . "<td align='center'>" . $t . "</td>";
		echo "<td>" . $rating . "</td>" . "</tr>";
 					}
								}

mysql_close($con);
?>
	</table>
</div></th>
	</form>

<td width="226" valign="top"><div align="center"></div></td>
</body>
</html>

 

 

Again any help is appreciated. I have search a ton of sites trying to find a way to display multi-selections from a select box and pass that info into an echo.

Posted

Change

<option value=0>... Selections
       <?=$options?>
       </option>

to

<option value="0">... Selections</option>
       <?php echo $options; ?>

(you had the closing tag for the "Selections" in the wrong place) and change

$test=$_POST['$options'];
                       if ($test){
                       foreach ($test as $t){

to

if (isset($_POST['test']) && !empty($_POST['test'])) {
                       foreach ($_POST['test'] as $t){

See if that helps. No '$' on an array element name, and the form variable is "test", not "options". I'm not sure whether you need the [] on "test" (in the form) for multiple selections. Try removing the brackets if it still doesn't work.

 

I trust that you're aware that the PHP code to handle the selected option(s) won't be run until the form has been submitted and sent back to the server (it's not handled on the client browser side).

Posted

Thank you Phil!

 

This is very close to what I need.

 

One problem is it appears the $t was returning the db column 'id' where I need it to return the db column 'chemical'

 

So I made this change.

 

if (isset($_POST['test']) && !empty($_POST['test'])) 
		{
		foreach ($_POST['test'] as $t){
//Change here switch $t to $fluid
		echo "<tr valign='top'>" . "<td align='center'>" . $fluid . "</td>";
		echo "<td>" . $rating . "</td>" . "</tr>";
 					}
								}

 

Now the problem is it always returns the last value for $fluid which is Zinc Sulfate. Even if you select multiple items it just spits out Zinc Sulfate x amount of times.

 

I tried clearing the $fluid variable since it was already used in the select box by

$fluid=""

$fluid=$row["chemical"]

 

But this didn't solve the issue

 

Then I tried making a new variable

$chemical=$row["chemical"] but that didn't work either.

 

Any advice on how to resolve this?

Posted

$t should correspond to the original table's "id". Can you map "id" somehow to "fluid" and "rating" values? If you want to directly return the "fluid" value/name, you need to put that in the option "value" attribute.

Posted

$t should correspond to the original table's "id". Can you map "id" somehow to "fluid" and "rating" values?

 

Hi Phil,

 

Yes, the way the table is setup is id, chemical, nitrile

The first row of the table reads (1, 1-Butene 2-Ethyl, 1)

the second row of the table (2, 1-Chloro 1-Nitro Ethane, 1)

 

So when a user selects the first option in the select box he is picking row 1 of the database all this lines up, I just need to get it to display

 

If you want to directly return the "fluid" value/name, you need to put that in the option "value" attribute.

 

Do you mean I need to generate the select box by setting nearly 1000 values like:

<option value="1-Butene 2-Ethyl">1-Butene 2-Ethyl</option>

<option value="1-Chloro 1-Nitro Ethane">1-Chloro 1-Nitro Ethane</option>

<option value="AN-O-3 Grade M">AN-O-3 Grade M</option>

<option value="AN-O-6">AN-O-6</option>

<option value="AN-VV-O-366b Hydr. Fluid">AN-VV-O-366b Hydr. Fluid</option>

<option value="ASTM Oil, No.1">ASTM Oil, No.1</option>

<option value="ASTM Oil, No.2">ASTM Oil, No.2</option>

<option value="ASTM Oil, No.3">ASTM Oil, No.3</option>

 

This would kind of defeat the purpose of using the database to generate the values in the select.

Or could I use something like

<select name="test[]" size="7" multiple="multiple" class="select1">

<option value=<?php echo $options ?></options>

<?php echo $options?>

</select>

Posted

Okay I gotcha on the last part.

 

I just needed to fix the variable

 

$options.="<OPTION VALUE=\"$id\">".$fluid.'</option>';

 

to

$options.="<OPTION VALUE=\"$fluid\">".$fluid.'</option>';

 

So that is good,

 

But now how do I get stop displaying only the last result in the database for the rating this is the column 'nitrile'.

 

I need it to use the $t variable and then get value in the next column 'nitrile'

 

Here is the code I have now.

 

<?
$con = mysql_connect(" xxx ");
		if (!$con)
	 {
			 die('Could not connect: ' . mysql_error());
		 }
		mysql_select_db("xxx", $con);

$sql="SELECT * FROM chemicals";
$result=mysql_query($sql); 
$options="";

while ($row=mysql_fetch_array($result)) {
$fluid=$row["chemical"];
$rating=$row["nitrile"];
$id=$row["id"];
$options.="<OPTION VALUE=\"$fluid\">".$fluid.'</option>';
$color_array = array("x" => "white", "1" => "green", "2" => "blue", "3" => "yellow", "4" => "red" );

}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
	<table width="850" border="1">
	<tr valign="top">
	<th width="350" scope="row"><div align="left">
	<select name="test[]" size="7" multiple="multiple" class="select1">
	</option>
	<?php echo $options?>
	</select>
	</div></th>
	<td width="375" rowspan="2"><div align="center"><strong>Fluid Selection</strong></div></td>
	<td width="103" rowspan="2"><div align="center"><strong>Fluid Rating</strong></div></td>
	</tr>
	<tr>
	<th width="350" scope="row"><div align="right">
	<input type="reset" /> 
	<input type="submit" value="submit" name="submit" />
	</div></th> 
	</tr>
<?php 
	if (isset($_POST['test']) && !empty($_POST['test'])) 
		{
		foreach ($_POST['test'] as $t){
		echo "<tr valign='top'>" . "<td>" . "</td>";
		echo "<td align='center'>" . $t . "</td>";
		echo "<td align='center' bgcolor=" . $color_array[$rating] .">" . $rating . "</td>" . "</tr>";

 					}
								}

mysql_close($con);
?>
	</table>
</div></th>
	</form>

<td width="226" valign="top"><div align="center"></div></td>
</body>
</html>

Posted

Is the 'id' field unique for each entry? If it is, you continue using it ($t) as the selected value, and then as the index to the entry in the $chemical[$t] and $nitrile[$t] arrays.

Posted

Hi Phil,

 

Yes the id field is unique to each row.

 

I don't however understand how to construct the array. I've tried several variations. The best I have done is to get the second letter in the chemical name as the rating.

Posted

So you have one or more unique 'id' values (1, 2, 3, ...) coming back from the form. Have you thought about querying the database to get the associated 'chemical' and 'nitrile' fields:

SELECT id, chemical, nitrile FROM chemicals WHERE id IN (3, 6, 7)

Or, you can build arrays from the database contents:

$chemical = array();
$chemical[$id] = 'chem name from database';  // numeric index
or 
$chemical["$id"] = 'chem name from database';  // associative array with string index

Posted

So you have one or more unique 'id' values (1, 2, 3, ...) coming back from the form. Have you thought about querying the database to get the associated 'chemical' and 'nitrile' fields:

 

Yes each row has a unique id

here is exactly how the db looks

sample_db.jpg

 

The select box needs to hold all values pulled from column 'chemical'

 

Upon post I need to spit out for each selected 'chemical => value in one cell, and in another cell 'nitrile' => value

 

SELECT id, chemical, nitrile FROM chemicals WHERE id IN (3, 6, 7)

Doesn't Select * from chemicals grab all this data? Also I don't quite understand the IN (3,6,7) is this a reference to the Column (3,6,7)?

 

Or, you can build arrays from the database contents:

$chemical = array();
$chemical[$id] = 'chem name from database'; // numeric index
or 
$chemical["$id"] = 'chem name from database'; // associative array with string index

 

Sorry once again I'm a tad confused here, this has to be dynamic based off the selections so the array would have to hold the selected values from the select box

So something like? $chemical[$id] = '$row['chemical']';

and

$rating[$id] = '$row['nitrile']';

 

I apologize this array stuff is confusing the hell out of me, something just hasn't clicked for me yet.

 

I have tried just doing another SQL query and I have gotten it to work with a single selection, but when I select more then 1 item I can't get the all the ratings to display.

You can see that here:

 

The code for that is:

$con = mysql_connect("localhost","xxx");
		if (!$con)
	 {
			 die('Could not connect: ' . mysql_error());
		 }
		mysql_select_db("xxx", $con);

$sql="SELECT * FROM chemicals";
$result=mysql_query($sql); 
$options="";

while ($row=mysql_fetch_array($result)) {
$fluid=$row["chemical"];
$options.="<OPTION VALUE=\"$fluid\">".$fluid.'</option>';
$color_array = array("x" => "white", "1" => "green", "2" => "blue", "3" => "yellow", "4" => "red" );

}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
	<table width="850" border="1">
	<tr valign="top">
	<th width="350" scope="row"><div align="left">
	<select name="test[]" size="7" multiple="multiple" class="select1">
	</option>
	<?php echo $options?>
	</select>
	</div></th>
	<td width="375" rowspan="2"><div align="center"><strong>Fluid Selection</strong></div></td>
	<td width="103" rowspan="2"><div align="center"><strong>Fluid Rating</strong></div></td>
	</tr>
	<tr>
	<th width="350" scope="row"><div align="right">
	<input type="reset" /> 
	<input type="submit" value="submit" name="submit" />
	</div></th> 
	</tr>
<?php 

	if (isset($_POST['test']) && !empty($_POST['test'])) 
		{
		foreach ($_POST['test'] as $t){
		echo "<tr valign='top'>" . "<td>" . "</td>";
		echo "<td align='center'>" . $t . "</td>";

 					}
								}
//NEW SELECTION HERE
		$sql2="SELECT nitrile FROM chemicals Where chemical = '$t'";
		$result2=mysql_query($sql2); 

		while ($row2=mysql_fetch_array($result2)) {
		$rating=$row2["nitrile"];
		$color_array = array("x" => "white", "1" => "green", "2" => "blue", "3" => "yellow", "4" => "red" );
 	echo "<td align='center' bgcolor=" . $color_array[$rating] .">" . $rating . "</td>" . "</tr>";	

		 }
mysql_close($con);
?>
	</table>
</div></th>
	</form>

 

I really thank you for your input and I'm actively trying to get this to work right.

Posted

Thanks for all the help Phil.

 

I found the problem. It was associated with the while loop and declaring the variables outside of where I needed it to get displayed. By writing another query and using the $t variable for reference I got the results I wanted.

 

This code seems to work. as seen here.

$sql="SELECT * FROM chemicals";
$result=mysql_query($sql); 
$options="";

while ($row=mysql_fetch_array($result)) {
$fluid=$row["chemical"];
$options.="<OPTION VALUE=\"$fluid\">".$fluid.'</option>';


}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
	<table width="850" border="1">
	<tr valign="top">
	<th width="350" scope="row"><div align="left">
	<select name="test[]" size="7" multiple="multiple" class="select1">
	</option>
	<?php echo $options?>
	</select>
	</div></th>
	<td width="375" rowspan="2"><div align="center"><strong>Chemical Selected</strong></div></td>
	<td width="103" rowspan="2"><div align="center"><strong>Nitrile Rating</strong></div></td>
	</tr>
	<tr>
	<th width="350" scope="row"><div align="right">
	<input type="reset" /> 
	<input type="submit" value="submit" name="submit" />
	</div></th> 
	</tr>
<?php 
		$color_array = array("x" => "white", "1" => "green", "2" => "blue", "3" => "yellow", "4" => "red" );
	if (isset($_POST['test']) && !empty($_POST['test'])) 
		{
		foreach ($_POST['test'] as $t){
		echo "<tr valign='top'>" . "<td>" . "</td>";
		echo "<td align='center'>" . $t . "</td>";
		$sql2="SELECT nitrile FROM chemicals where chemical='$t'";
		$result2=mysql_query($sql2); 
		while ($row2=mysql_fetch_array($result2)) {
		$rating=$row2["nitrile"];
		echo "<td align='center' bgcolor=" . $color_array[$rating] .">" . $rating . "</td>" . "</tr>";

 					}
								}
									 }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...