Filtering results depending on form results

If you var_dump($mfgs) after the loop, what does it show?

Minor thing in your code has the column named “manufaturer” (without the “c”), is that your typo or is it correct and I’ve corrected it when I should not have done?

Please post your code.

code.php.zip (3.5 KB)

Thank you so much for helping me with this.

You’re right that was a typo, thank you for correcting it.

The var_dump outputs this: array(0) { }

I’ve also attached to whole (real) code as I didn’t know which parts would help.

The var_dump needs to be after the foreach loop - you’ve put it right after it’s created, so it’s showing empty correctly.

Sorry I’ve taken a long time to get back to you - as you know the other issue was causing so many problems and crashing the server. Now that that’s getting better I can think about this again.

The var_dump now shows this:

string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name” string(43) “select * from product_dietary order by name”

Are you sure you’re using var_dump on the correct array, the $mfgs array? I can’t see how that could contain the strings you describe. Add the code for the loop, if you would.

I’m using var_dump($query); to get the results, is that wrong?

Yes, I wanted to see what was in the $mfgs array, not the query.

What I was hoping to do is build $mfgs as an indexed array, so each time you get a new row as part of your loop, you could check whether the manufacturer for that item has already appeared in the array, and if it has not, then you add it. You can then decide whether or not to display the item based on how many times an item from that manufacturer has already been displayed. After that, you increment the array total for that manufacturer and go for the next row.

So I was expecting to see something like

$mfgs[] array {
  'vauxhall' : 1
  'ford' : 2
  'bmw': 8
}

or however var_dump shows it after the end of your loop. That might give us an idea as to why it’s continuing to display stuff even though you’re checking the count. From post #24 it seems that you have the var_dump inside the loop rather than after it - laying out the code differently might make it a little clearer, as I recall you have several } on the same line and so on, which doesn’t help indenting to identify ends of loops.

Sorry to be a pain but my boss how now decided that she doesn’t want it like this so I don’t need to worry about it. Although what she now wants is causing me a different header.

Is it possible to have an array (foreach) within another on? When I try I stop getting any results after the second foreach.

This is what I’ve got at the moment:

<?php if( is_array( $product_results ) ) { ?>
		<?php foreach( $product_results as $key=>$value ) {
?>
  <tr valign="middle" align="left">
    <td><div class="zoom_img" ><img src="<?php echo htmldisplay( $value['image_url'] ) ?>"/></div></td>
    <td><span style="font-weight: bold; color: #000;"><?php echo htmldisplay( $value['product_name'] ) ?></span></td>
    <td class="extra" style="color: #000;"><?php echo htmldisplay( $value['brand_name'] ) ?></td>
    <td align="center" class="extra"><?php echo htmldisplay( $value['weight'] ) ?>kg</td>
    <td align="center" class="extra"><?php echo htmldisplay( $value['serving'] ) ?></td>
    <!-- Ideally I want this but to show one results but if there are multiple results just say many -->
    <td class="extra"><?php $query="select name as source_name from product_source, source  where source.source_id=product_source.id and source.product_id=".$value['product_feed_id']." order by name";
      $source_name=dbselect( $query,"dbLinkInt" );
      if( is_array( $source_name ) ) {
        foreach( $source_name as $key=>$value ) {
	   echo htmldisplay( $value['source_name'] ); 
		} } ?></td>
        
       <!-- stops working here -->
    <td align="center"><?php if( !empty( $value['promo_text'] ) ) { ?><div style="border: 1px solid #d7d7d7; background: #f6f6f6; color: #fd491b; text-align: center; padding: 5px;"><?php echo htmldisplay( $value['promo_text'] ) ?></div><?php } else { ?> - <?php } ?></td>
    <td class="results_price extra"><?php if($value['weight']==='N/A') { ?>N/A<?php } else { ?>&pound;<?php echo number_format( $value['price']/$value['weight'],2 ); } ?></td>
    <td class="results_price extra"><?php if($value['serving']==='N/A') { ?>N/A<?php } else { ?>&pound;<?php echo number_format( $value['price']/$value['serving'],2 ); } ?></td>
    <td class="results_price">&pound;<?php echo htmldisplay( $value['price'] ) ?></td>
    <td class="results_price extra">&pound;<?php echo htmldisplay( $value['cost'] ) ?></td>
    <td class="results_price">&pound;<?php echo htmldisplay( $value['price']+$value['cost'],2 ) ?></td>
  </tr>
  <?php
 } } ?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.