how to get the results of an include() statement to display

A

Anonymous

Guest
I have the following script that I want to display in my main script with an include() statement. I need the results of the script to be placed into a variable that is later called in an echo statement. I can't seem to get it to fit into my main script. Any help?

Code:
<?php
//connect to the database
include ('mysql_connect.php');

//select all of the names of orders available
$get_orders = "select * from insect_order";
$order_names = mysql_query($get_orders, $conn) or die(mysql_error());

// start a select box
echo "<Strong>Select Insect Order</Srong><BR>
<select name=\"insect_orders\" size =\"1\">
<OPTION selected value=\"none\">None</OPTION>";

//display order_names as options in the select box
WHILE($newArray = mysql_fetch_array($order_names)) {
  $id = $newArray['orderID'];
	$name = $newArray['order_name'];
  $option = "<OPTION value = \"$id\">$name</OPTION>";
	echo $option;
}	

//stop the select box
echo "</SELECT>";
?>

I want to call the script in my main script like this...
Code:
<?php
$select_order_box = include ('select_orders.php');
$display_block = "<h1>Add Insect Family</h1>
<form method=\"POST\" enctype=\"multipart/form-data\" 
action=\"$_SERVER[PHP_SELF]\"><P><strong>Family Name:</strong><br>
<input type=\"text\" name=\"family_name\" size=30 maxlength=40>
$select_order_box <P><input type=\"hidden\" name=\"op\" value=\"add\">
<input type=\"submit\" name=\"submit\" 
value=\"Add Insect Family\">";

echo $display_block;
?>

I have tried many things that result in errors, or only a single insect_order appearing in the select box instead of all of the names available in the DB. Any suggestions?
 
The right way to use the include is in your first posted code/box.
If $select_order_box variable will receive a value that is somehow returned in the select_orders.php file, so you can do it in that same file.

Example (select_orders.php):
Code:
<?php

$select_order_box = 'whatever';
// Or
$select_order_box = whatever();
// Or so many diffferent kinds of assignment

function whatever() { return true; }

// Now you can include this file and just call for $select_order_box
// Following the 2ª assignment example you can include this file
// and than call for $select_order_box like: $select_order_box = whatever();

?>
 
I figured out a way on my own the other night. I discovered that I could pass a single variable from the included file if I used the following code...

Code:
<?php
//connect to the database
include ('mysql_connect.php');

//select all of the names of orders available
$get_orders = "select * from insect_order";
$order_names = mysql_query($get_orders, $conn) or die(mysql_error());

$options = "<Strong>Select Insect Order</Srong> 
<select name=\"orderID\" size =\"1\">
<OPTION selected value=\"none\">None</OPTION>";

//display order_names as options in the select box
WHILE($newArray = mysql_fetch_array($order_names)) {
  $id = $newArray['orderID'];
	$name = $newArray['order_name'];
	$options .= "<OPTION value = \"$id\">$name</OPTION>";

}	

$options .= "</SELECT>"; 

?>

By inserting appending each option onto the $options variable I was able to have a sinlge variable with all of the HTML encompased within it. Then all I had to do in my variable in the file that includes the above was to add "$options" where is it belonged in the rest of the code that was to be inserted. I had never seen the ".=" before since I am new, but once I saw it, it fixed my problem immediately.

The next issue that I have is that I want to create a checkbox that is populated with information based on the selection in a previous checkbox, but the catch is that I don't want to reload the box, so I am not sure I can do it in php. Is that a Javascript? For instance, I want the user to select an insect order in one box. Based on the Insect order that they select, I want to the next selectbox to be populated with the insect families contained within that order, then based on the family selected, I want a third selectbox that is populated with the genera contained in the selected family, and finally I want a 4th selectbox populated with the species contained within the selected genus. Any thoughts on how to do that? I have seen similar setups on ebay and such.

Cheers!

Justin
 
Back
Top