Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pull items out of Array Merged Array

Status
Not open for further replies.

iteach2

Technical User
Sep 16, 2001
120
US
Gurus,

I have a merged array like so:
Code:
array(796) {
  [0]=>
  array(2) {
    [0]=>
    string(32) "(15) Mexican Spaghetti (Tuesday)"
    [1]=>
    string(5) "22265"
[1]=>
  array(2) {
    [0]=>
    string(13) "A BOWL OF RED"
    [1]=>
    string(5) "22266"
  }
  [2]=>
  array(2) {
    [0]=>
    string(21) "A Red Chili Nightmare"
    [1]=>
    string(5) "22267"
  }
  [3]=>
  array(2) {
    [0]=>
    string(25) "A TO Z OF SPICES (PART 1)"
    [1]=>
    string(5) "22268"
  }

Any help/advice/assistance is very much appreciated. Thank You.

I would like for the dynamic drop down to be as such, using the example above.

Code:
<option value=22265>Mexican Spaghetti (Tuesday)</option>;

Here is my full code:

Code:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "recipes";

mysql_connect($dbhost, $dbuser, $dbpass);

mysql_select_db($dbname) or die(mysql_error());

$cat = $_REQUEST['cat'];

	
$cat = mysql_real_escape_string($cat);
$name = mysql_real_escape_string($name);
	
$query = "SELECT auto, name FROM recipes_pro_new WHERE category = '$cat'";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
$items[] = array_merge($row['name'],$row['auto']);
}

echo"<pre>";
var_dump($items);


echo "<select name=name size=10 id=name>";

foreach($items as $item)
{
$filter = implode(",", $item);
echo "<option value=>".$filter."</option>";
}


?>



 
Is there any particular reason why you're putting all the values from the table into your array before outputting them?

If not, all of this:

Code:
while($row = mysql_fetch_array($result))
{
$items[] = array_merge($row['name'],$row['auto']);
}

echo"<pre>";
var_dump($items);


echo "<select name=name size=10 id=name>";

foreach($items as $item)
{
$filter = implode(",", $item);
echo "<option value=>".$filter."</option>";
}

can be simplified to:

Code:
echo '<select name="name" size="10" id="name">';

while($row = mysql_fetch_assoc($result))
{
	echo '<option value="' . $row['auto'] . '">' . $row['name'] . '</option>';
}



Want the best answers? Ask the best questions! TANSTAAFL!
 
i don't think you need to have merged arrays. the following code should do what you want.

Code:
<?php

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "recipes";

mysql_connect($dbhost, $dbuser, $dbpass);

mysql_select_db($dbname) or die(mysql_error());

$cat = $_REQUEST['cat'];

    
$cat = mysql_real_escape_string($cat);
$name = mysql_real_escape_string($name);
    
$query = "SELECT auto, name FROM recipes_pro_new WHERE category = '$cat'";

$result = mysql_query($query) or die(mysql_error());

$options = '';
while($row = mysql_fetch_assoc($result)){
	$options .= <<<HTML
		<option value="{$row['auto']}">{$row['name']}</option>

HTML;
}

echo <<<HTML
	<select name="name" size="10" id="name">
	$options
	</select>

HTML;
?>
 
Thank you very much sleipnir214[2thumbsup] and Thank you very much jpadie[2thumbsup]. You both have shown me faster and better ways to do my code. jpadie I did not even no about the <<<HTML feature of PHP. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top