0

With the code below I have the plant information printing out first and then all of the availability printing out last. Is there any way I can get both printing together, but not have duplicate pictures and information for the numerous sizes that sometimes list per plant. Ex. Buxus Green Velvet may have quantities for 3 gallon, 5 gallon, 24-30 inch, etc. I do not need the plant info and pictures printing out for each of these sizes. I just want the size and availability listed together under the ONE picture and info. Thanks for any assistance you can provide me.

<?php 


$aLight = array( 
    'Part Sun Shade' => 'Part Sun Shade', 
    'Full Sun' => 'Full Sun', 
    'Full Shade' => 'Full Shade', 
   
	
	
  ); 

$aColours = array( 
    'bronze fall color' => 'bronze fall color', 
    'burgundy fall color' => 'burgundy fall color', 
    'orange fall color' => 'orange fall color', 
    'purple fall color' => 'purple fall color', 
    'red fall color' => 'red fall color', 
    'yellow fall color' => 'yellow fall color',
	
    'blue flower' => 'blue flower', 
    'burgundy flower' => 'burgundy flower', 
    'lavender flower' => 'lavender flower', 
    'lilac flower' => 'lilac flower',
    'magenta flower' => 'magenta flower', 
    'mixed flower' => 'mixed flower', 
    'orange flower' => 'orange flower', 
    'pink flower' => 'pink flower',
    'purple flower' => 'purple flower', 
    'red flower' => 'red flower', 
    'rose flower' => 'rose flower', 
    'white flower' => 'white flower',
    'yellow flower' => 'yellow flower',
		
    'blue green foliage' => 'blue green foliage',
    'green foliage' => 'green foliage', 
    'purple foliage' => 'purple foliage', 
    'red foliage' => 'red foliage', 
    'variegated foliage' => 'variegated foliage',
    'yellow foliage' => 'yellow foliage',
	
    'green fruit' => 'green fruit',
    'orange fruit' => 'orange fruit',
    'purple fruit' => 'purple fruit',
    'red fruit' => 'red fruit'


    
); 

$aType = array( 
    'DECIDUOUS VINE' => 'DECIDUOUS VINE', 
    'EVERGREEN SHRUB' => 'EVERGREEN SHRUB', 
    'DECIDUOUS SHRUB' => 'DECIDUOUS SHRUB', 
    'EVERGREEN TREE' => 'EVERGREEN TREE', 
    'DECIDUOUS TREE' => 'DECIDUOUS TREE', 
    'BROADLEAF EVERGREEN SHRUBS' => 'BROADLEAF EVERGREEN SHRUBS' 
    
); 



asort($aLight); 
asort($aType); 

?> 


<form name="plantsearch" id="myform" method="post"> 
    <fieldset> 
        <legend>LIGHT REQUIREMENTS</legend> 
      
        
<?php 
// You could write a function to handle this since it'll be reused for colour.  I won't bother 
foreach ($aLight AS $key => $light) 
{ 
    $checked = ''; 
    if (isset($_POST['light']) && in_array($light, $_POST['light'])) 
    { 
        $checked = ' checked="checked"'; 

    } 
    printf('        <input type="checkbox" id="light_%s" name="light[]" value="%s"%s />' . PHP_EOL, str_replace(' ', '_', $key), $key, $checked); 
    printf('        <label for="light_%s">%s  l  </label>' . PHP_EOL, str_replace(' ', '_', $key), $light); 
} 
?> 
    </fieldset>
    
    <p>
     
<fieldset> 
        <p align="center">&nbsp;</p> <legend>COLOR ATTRIBUTES - each plant is listed by only one attribute</legend> 
<?php 

foreach ($aColours AS $key => $colour) 
{ 
    $checked = ''; 
    if (isset($_POST['colour']) && in_array($colour, $_POST['colour'])) 
    { 
        $checked = ' checked="checked"'; 
    } 
    printf('        <input type="checkbox" id="colour_%s" name="colour[]" value="%s"%s />' . PHP_EOL, str_replace(' ', '_', $key), $key, $checked); 
    printf('        <label for="colour_%s">%s  l  </label>' . PHP_EOL, str_replace(' ', '_', $key), $colour); 
} 

?> 
   



</fieldset> 
   
    <p> 
    <fieldset> 
    <p align="center">&nbsp;</p><legend>PLANT TYPE</legend> 
           


<?php 

foreach ($aType AS $key => $type) 
{ 
    $checked = ''; 
    if (isset($_POST['type']) && in_array($type, $_POST['type'])) 
    { 
        $checked = ' checked="checked"'; 

    } 
    printf('        <input type="checkbox" id="type_%s" name="type[]" value="%s"%s />' . PHP_EOL, str_replace(' ', '_', $key), $key, $checked); 
    printf('        <label for="type_%s">%s  l  </label>' . PHP_EOL, str_replace(' ', '_', $key), $type); 
} 

?> 
    </fieldset>
   
  <p>
  <p>
  <p align="center">&nbsp;</p><input type="submit" name="submit" value="SEARCH PLANTS" />
 </p>
</form> 

(must choose at least one Light, Fall Color and Type)


<p align="center">&nbsp;</p>
<div align="center"><hr width="300" size="1" noshade="noshade" /></div>

<div align="center"><b>SUMMARY OF RESULTS</b>
</div>
<p align="center">&nbsp;</p>


<div align="center">



<?php 

// Lets filter out the junk 
function notEmpty($x) 
{ 
    return !empty($x); 
} 


// Make your connections here 

   mysql_connect("", "", "") or die(mysql_error()); 
   mysql_select_db("") or die(mysql_error()); 
	
				
	
 foreach($_POST['light'] As $sLight) { 
    printf("%s<br>", $sLight);  
 }
	 
  foreach($_POST['colour'] As  $sColours) { 
   printf("%s<br>",  $sColours);  
 }  

	
  foreach($_POST['type'] As  $sType) { 
   printf("%s<br>",  $sType);  
 }  
	
	

if (isset($_POST['submit'])) 

{ 
    $aWhere = array( 
         
	   	  
        &$sLight, 
        &$sColours,
        &$sType
    ); 
   
  
  
  $sWhere = ''; 
    if (isset($_POST['light'])) 
    { 
        $sLight = 'findplantsdb.Light IN (\'' . implode("', '", $_POST['light']) . '\')'; 
    } 
    if (isset($_POST['colour'])) 
    { 
        $sColours = 'findplantsdb.Color IN (\'' . implode("', '", $_POST['colour']) . '\')'; 
    } 
	
	 if (isset($_POST['type'])) 
    { 
        $sType = 'findplantsdb.Type IN (\'' . implode("', '", $_POST['type']) . '\')'; 
    } 
	
	
	
    	$aWhereFilter = array_filter($aWhere, 'notEmpty'); 
		
	
  
      if (count($aWhereFilter) > 0) 
    { 
        $sWhere = ' WHERE ' . implode(' AND ', $aWhere); 
    } 


  
    $sQry = 'SELECT findplantsdb.Name, findplantsdb.Patent, findplantsdb.Common, findplantsdb.Hname, findplantsdb.SName, findplantsdb.Color, findplantsdb.Light, findplantsdb.Zone, findplantsdb.Picname, findplantsdb.Notes, plantsdb.type, plantsdb.size, plantsdb.yard, plantsdb.field, plantsdb.total FROM findplantsdb LEFT JOIN plantsdb ON (plantsdb.name = findplantsdb.Name)' . $sWhere; 
	
	

    $qry = mysql_query($sQry) or die('check to see if you have selected at least one from each category'); 
    if (mysql_num_rows($qry) > 0) 
    { 
        		
				
        while ($row = mysql_fetch_assoc($qry)) 
        { 
	
	

echo "<h4> ", $row['Name'], " &nbsp; ", $row['Patent'], "</h4> ",$row['Common'], "<p> Height:  ", $row['Hname'], "<br> Spread:  ", $row['Sname'], "<br> Color:  ", $row['Color'], "<br> Light:  ", $row['Light'], "<br> Zone:  ", $row['Zone'], "<p> <img src=http://www.domain.com/mobile/",$row['Picname'],  " /> <p><p>", $row['Notes'], "<p><p> <hr width='50%' size='1' color='#A3A3A3'><p>";
} 



mysql_data_seek($qry, 0);

while ($row = mysql_fetch_assoc($qry)) {
	

Print "<table border=0 cellspacing=2 cellpadding=0 align='center'>";

Print "<font size=2 face='Arial'>";

while($row = mysql_fetch_array( $qry )) { Print "<tr>";

Print "<td>".$row['Name'] . "</td>";
Print "<td>-Size: ".$row['size'] . "</td>";
Print "<td>-Type: ".$row['type'] . "</td>";
Print "<td>-Yd: ".$row['yard'] . "</td>";
Print "<td>-Fld: ".$row['field'] . "</td></tr>";  } Print "</table>";

	}



      
    } 
    else 
    { 
        print '<p><div>No matching records</div></p>'; 
    } 





}
?>
2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by diafol
0

Is there any reason that you're using associative arrays as opposed to simple ones? Your keys and values are identical.

Code looks pretty convoluted to me. Will have a look later on if nobody else posts.

Edited by diafol: n/a

0

Sorry, if it looks bad. I'm a beginner and have had much help on this from your message board as well as others. It may be a mess, but it is working and doing what I need except for what I have stated above. Any help at would be greatly appreciated. And the reason for associative arrays is because it was recommended to me by someone more knowledgeable than I on php.

1

OK I started having a look, but ran out of time tonight:

<?php

mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("diafol") or die(mysql_error()); 
//HELPER VARIABLES
$r = array('light','colour','type'); //labels
$f = array('Light','Color','Type');  //fields

//ARRAY DATA 
$aLight = array( 
    'Part Sun Shade', 'Full Sun', 'Full Shade', 
); 
 
$aColour = array( 
    'bronze fall color', 'burgundy fall color', 'orange fall color', 'purple fall color', 'red fall color', 'yellow fall color',	
	'blue flower', 'burgundy flower', 'lavender flower', 'lilac flower', 'magenta flower', 'mixed flower', 'orange flower', 'pink flower',    'purple flower', 'red flower', 'rose flower', 'white flower', 'yellow flower', 
    'blue green foliage', 'green foliage', 'purple foliage', 'red foliage', 'variegated foliage', 'yellow foliage', 'green fruit', 'orange fruit', 'purple fruit', 'red fruit'
); 
 
$aType = array( 
    'DECIDUOUS VINE', 'EVERGREEN SHRUB', 'DECIDUOUS SHRUB', 'EVERGREEN TREE', 'DECIDUOUS TREE', 'BROADLEAF EVERGREEN SHRUBS' 
); 
//sort arrays to alphabeticals 
sort($aLight); 
sort($aType); 

//FUNCTIONS
//junk filter
function notEmpty($x){ 
    return !empty($x); 
} 
//checkbox generator 
function createChecks($label,$r){
	$list = "";
	foreach ($r as $item){ 
		$checked = (isset($_POST[$label]) && in_array($item,(array)$_POST[$label]))? ' checked="checked"' : ''; 
		$list .= '        <input type="checkbox" id="' . $label .  '_' . str_replace(' ', '_', $item) . '" name="' .$label. '[]" value="' . $item .'"' .$checked . ' />' . PHP_EOL; 
		$list .= '        <label for="' . $label . '_' . str_replace(' ', '_', $item) .'">' . $item . '  l  </label>' . PHP_EOL; 
	} 
	return $list;	
}
//list generator 
function createList($r){
	$list = "";
	$where = "";
	foreach($r as $item) { 
    	if(isset($_POST[$item])){
			foreach($_POST[$item] as $p){
				$list .= addslashes($p) . "<br />"; 	
			}
		}
 	}
	return $list;
}
//where clause geneerator
function createWhere($r,$f){
	$where="";
	for($i=0;$i<count($r);$i++){
		if(isset($_POST[$r[$i]])){
			$p = (array)$_POST[$r[$i]];
			$pr = array_map('mysql_real_escape_string',$p);
			$lstring[$r[$i]] = 	"findplantsdb.{$f[$i]} IN ('" . implode("', '", $pr) . "')";
		}
	}
	if(isset($lstring))$where = ' WHERE ' . implode(' AND ', $lstring); 
	return $where;
	//needs work to avoid false items being passed - but works as is for now
}
?> 
 
 
<form name="plantsearch" id="myform" method="post"> 
    <fieldset> 
        <legend>LIGHT REQUIREMENTS</legend> 
<?php 
	echo createChecks('light',$aLight);
?> 
    </fieldset>
	<fieldset> 
       <legend>COLOR ATTRIBUTES - each plant is listed by only one attribute</legend> 
<?php 
	echo createChecks('colour',$aColour); 
?> 
	</fieldset> 
	<fieldset> 
 		<legend>PLANT TYPE</legend> 
<?php 
	echo createChecks('type',$aType); 
?> 
    </fieldset>
 
  <br />
  <br />
  <input type="submit" name="submit" value="SEARCH PLANTS" />
</form> 
 
(must choose at least one Light, Fall Color and Type)
 
<p align="center">&nbsp;</p>
<div align="center"><hr width="300" size="1" noshade="noshade" /></div>
<div align="center"><b>SUMMARY OF RESULTS</b>
</div>
<p align="center">&nbsp;</p>
<div align="center">
  
<?php 
	if(isset($_POST['submit'])){
		echo createList($r);
		$sQry = 'SELECT findplantsdb.Name, findplantsdb.Patent, findplantsdb.Common, findplantsdb.Hname, findplantsdb.SName, findplantsdb.Color, findplantsdb.Light, findplantsdb.Zone, findplantsdb.Picname, findplantsdb.Notes, plantsdb.type, plantsdb.size, plantsdb.yard, plantsdb.field, plantsdb.total FROM findplantsdb LEFT JOIN plantsdb ON (plantsdb.name = findplantsdb.Name)' . createWhere($r,$f);
	}
	$qry = mysql_query($sQry) or die('check to see if you have selected at least one from each category'); 

//I GO TO HERE !!!!!!!!

	if (mysql_num_rows($qry) > 0) { 
		while ($row = mysql_fetch_assoc($qry)){ 
			echo "<h4> ", $row['Name'], " &nbsp; ", $row['Patent'], "</h4> ",$row['Common'], "<p> Height:  ", $row['Hname'], "<br> Spread:  ", $row['Sname'], "<br> Color:  ", $row['Color'], "<br> Light:  ", $row['Light'], "<br> Zone:  ", $row['Zone'], "<p> <img src=http://www.domain.com/mobile/",$row['Picname'],  " /> <p><p>", $row['Notes'], "<p><p> <hr width='50%' size='1' color='#A3A3A3'><p>";
		} 
		mysql_data_seek($qry, 0);
		while ($row = mysql_fetch_assoc($qry)) {
			Print "<table border=0 cellspacing=2 cellpadding=0 align='center'>";
			Print "<font size=2 face='Arial'>";
			while($row = mysql_fetch_array( $qry )) { 
				Print "<tr>";
				Print "<td>".$row['Name'] . "</td>";
				Print "<td>-Size: ".$row['size'] . "</td>";
				Print "<td>-Type: ".$row['type'] . "</td>";
				Print "<td>-Yd: ".$row['yard'] . "</td>";
				Print "<td>-Fld: ".$row['field'] . "</td></tr>";  
			} 
			Print "</table>";
		}
	}else{ 
		print '<p><div>No matching records</div></p>'; 
	} 
?>

Didn't get to the nitty gritty of the SQL - sorry had to reformat to my way of thinking to get my head around it. Think I'm almost there. Will come back tomorrow unless somebody else contributes.

1

OK, you can try this - couldn't try it as I don't have your DB tables:

<?php

mysql_connect("", "", "") or die(mysql_error()); 
mysql_select_db("") or die(mysql_error()); 
//HELPER VARIABLES
$r = array('light','colour','type'); //labels
$f = array('Light','Color','Type');  //fields

//ARRAY DATA 
$aLight = array( 
    'Part Sun Shade', 'Full Sun', 'Full Shade', 
); 
 
$aColour = array( 
    'bronze fall color', 'burgundy fall color', 'orange fall color', 'purple fall color', 'red fall color', 'yellow fall color',	
	'blue flower', 'burgundy flower', 'lavender flower', 'lilac flower', 'magenta flower', 'mixed flower', 'orange flower', 'pink flower',    'purple flower', 'red flower', 'rose flower', 'white flower', 'yellow flower', 
    'blue green foliage', 'green foliage', 'purple foliage', 'red foliage', 'variegated foliage', 'yellow foliage', 'green fruit', 'orange fruit', 'purple fruit', 'red fruit'
); 
 
$aType = array( 
    'DECIDUOUS VINE', 'EVERGREEN SHRUB', 'DECIDUOUS SHRUB', 'EVERGREEN TREE', 'DECIDUOUS TREE', 'BROADLEAF EVERGREEN SHRUBS' 
); 
//sort arrays to alphabeticals 
sort($aLight); 
sort($aType); 

//FUNCTIONS

//checkbox generator 
function createChecks($label,$r){
	$list = "";
	foreach ($r as $item){ 
		$checked = (isset($_POST[$label]) && in_array($item,(array)$_POST[$label]))? ' checked="checked"' : ''; 
		$list .= '        <input type="checkbox" id="' . $label .  '_' . str_replace(' ', '_', $item) . '" name="' .$label. '[]" value="' . $item .'"' .$checked . ' />' . PHP_EOL; 
		$list .= '        <label for="' . $label . '_' . str_replace(' ', '_', $item) .'">' . $item . '  l  </label>' . PHP_EOL; 
	} 
	return $list;	
}
//list generator 
function createList($r){
	$list = "";
	$where = "";
	foreach($r as $item) { 
    	if(isset($_POST[$item])){
			foreach($_POST[$item] as $p){
				$list .= addslashes($p) . "<br />"; 	
			}
		}
 	}
	return $list;
}
//where clause geneerator
function createWhere($r,$f){
	$where="";
	for($i=0;$i<count($r);$i++){
		if(isset($_POST[$r[$i]])){
			$p = (array)$_POST[$r[$i]];
			$pr = array_map('mysql_real_escape_string',$p);
			$lstring[$r[$i]] = 	"findplantsdb.{$f[$i]} IN ('" . implode("', '", $pr) . "')";
		}
	}
	if(isset($lstring))$where = ' WHERE ' . implode(' AND ', $lstring); 
	return $where;
	//needs work to avoid false items being passed - but works as is for now
}
?> 
 
 
<form name="plantsearch" id="myform" method="post"> 
    <fieldset> 
        <legend>LIGHT REQUIREMENTS</legend> 
<?php 
	echo createChecks('light',$aLight);
?> 
    </fieldset>
	<fieldset> 
       <legend>COLOR ATTRIBUTES - each plant is listed by only one attribute</legend> 
<?php 
	echo createChecks('colour',$aColour); 
?> 
	</fieldset> 
	<fieldset> 
 		<legend>PLANT TYPE</legend> 
<?php 
	echo createChecks('type',$aType); 
?> 
    </fieldset>
 
  <br />
  <br />
  <input type="submit" name="submit" value="SEARCH PLANTS" />
</form> 
 
(must choose at least one Light, Fall Color and Type)
 
<p align="center">&nbsp;</p>
<div align="center"><hr width="300" size="1" noshade="noshade" /></div>
<div align="center"><b>SUMMARY OF RESULTS</b>
</div>
<p align="center">&nbsp;</p>
<div align="center">
  
<?php 
if(isset($_POST['submit'])){
	echo createList($r);
	$sQry = 'SELECT findplantsdb.Name, findplantsdb.Patent, findplantsdb.Common, findplantsdb.Hname, findplantsdb.SName, findplantsdb.Color, findplantsdb.Light, findplantsdb.Zone, findplantsdb.Picname, findplantsdb.Notes, plantsdb.type, plantsdb.size, plantsdb.yard, plantsdb.field, plantsdb.total FROM findplantsdb LEFT JOIN plantsdb ON (plantsdb.name = findplantsdb.Name)' . createWhere($r,$f);
	
	$qry = mysql_query($sQry) or die('check to see if you have selected at least one from each category'); 
	if (mysql_num_rows($qry) > 0) {
		$name = ""; 
		while ($row = mysql_fetch_assoc($qry)){ 
			if($name != $row['Name'] && $name != "")echo '</table>';
			if($name != $row['Name'])echo "<h4> ", $row['Name'], " &nbsp; ", $row['Patent'], "</h4> ",$row['Common'], "<p> Height:  ", $row['Hname'], "<br> Spread:  ", $row['Sname'], "<br> Color:  ", $row['Color'], "<br> Light:  ", $row['Light'], "<br> Zone:  ", $row['Zone'], "<p> <img src=http://www.domain.com/mobile/",$row['Picname'],  " /> <p><p>", $row['Notes'], "<p><p> <hr width='50%' size='1' color='#A3A3A3'><p><table border=0 cellspacing=2 cellpadding=0 align='center'><font size=2 face='Arial'>";
			echo "<tr><td>".$row['Name'] . "</td><td>-Size: ".$row['size'] . "</td><td>-Type: ".$row['type'] . "</td><td>-Yd: ".$row['yard'] . "</td><td>-Fld: ".$row['field'] . "</td></tr>";
			$name = $row['Name'];
		}
		echo '</table>';
	}else{ 
		print '<p><div>No matching records</div></p>'; 
	} 
}
?>

Hope it works.

Edited by diafol: n/a

0

OHMYGOD YOU DID IT!! You seriously did it!! It is now listing out exactly the way I have wanted it to list for months! I cannot thank you enough for helping me with this. I'm new and have struggled for a very long time with this. You are truly a kind and brilliant man. Thank you very much!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.