I have a for each loop like this
$sn_count = 1;
$prodfilter = "";
foreach($prods as $prod){
$prodfilter .= "<div class=\"product\">".$prod['product'][1]."</div>";
$sn_count++;
}
echo $prodfilter;
Now my problem my "product" class displaying border even if the $prod['product'][1] not available. So i would like to test it using if statement.
If (product value available) {
$prodfilter .= "<div class=\"product\">".$prod['product'][1]."</div>";
}
I tried like this.
if(!empty($prod['product'][1])) {
$prodfilter .= "<div class=\"product\">".$prod['product'][1]."</div>";
}
But its not working.
3 Answers 3
you can try couple of things
try this for a start
if(strlen(trim($prod['product'][$sn_count]))>0) {
$prodfilter .= "<div class=\"product\">".$prod['product'][$sn_count]."</div>";
}
or
if(isset($prod['product'][$sn_count])) {
$prodfilter .= "<div class=\"product\">".$prod['product'][$sn_count]."</div>";
}
2 Comments
The right thing in my opinion would be to check how many rows returned. I'll assume you are using MySQL since you did not specify. Please ask for additional help if you are not using it.
http://www.php.net/manual/en/function.mysql-num-rows.php
if (mysql_num_rows($prods)!=0) {
//Do your code
}
This should check if your query returned more than 0 rows (so it needs to be drawn). Does it fix it?
Comments
The right thing to do is to check if it's empty as you tried, but since it fails there obviously is some data there. You could do a var_dump and figure out what and why it is there which probably leads you to the source of the problem.
If by available you mean declared then isset is the right function to use by the way.
$stepsbut you are testing and outputting$prod... ?? You'll get the same thing out for each loop iteration.