lilmer Posted April 2, 2013 Share Posted April 2, 2013 I've been given a function that return all the list of data in an array. Only that function I can update nothing else specially on the database. From all the list of data, by using foreach I can print all the list of data. But my problem now is on the list of data e.g. (name, address, phone_number, zip_code). . How can I make a condition that will only show all the data by the same zip_code or name whatever. e.g. (John, John, John, John) for me it will be easy if i have access on the database, but unfortunately I don't have. Anyone can help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/ Share on other sites More sharing options...
awjudd Posted April 2, 2013 Share Posted April 2, 2013 Keep track of the 'previous' value (i.e. previous name) in a variable and then if the current value is the same as the previous value, don't emit it. $previous_name=NULL; foreach($array as $value) { if($value['name']!=$previous_name) { echo $value['name']; $previous_name=$value['name']; } // Display the rest } Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1422431 Share on other sites More sharing options...
lilmer Posted April 2, 2013 Author Share Posted April 2, 2013 (edited) Example is this image right here: On theTYPE, I only want to make a condition that will view only all the data where type is Deposit. . . condition from foreach. . is this possible? Edited April 2, 2013 by lilmer Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1422434 Share on other sites More sharing options...
trq Posted April 2, 2013 Share Posted April 2, 2013 What have you tried? Should be a pretty simple task even for a beginner. Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1422435 Share on other sites More sharing options...
lilmer Posted April 3, 2013 Author Share Posted April 3, 2013 Here's my code: foreach($bHistory as $row){ $result .= ' <tr class="internal-03 highlight"> <td class="internal border-bottom" align="center">'.$row['Id'].'</td> <td class="internal border-bottom" align="center">'.$data['wallet'].'</td> <td class="internal border-bottom" align="center">'.date('y-m-d h:i:s',$row['Date']).'</td> <td class="internal border-bottom" align="center"><strong>'.$row['Memo'].'</strong></td> <td class="internal border-bottom" align="center">N/A</td> <td class="internal border-bottom" align="center">'.$data['currency'].'</td> <td class="internal border-bottom" align="center">'.$row['Status'].'</td> <td class="internal border-bottom" align="center">'.$row['Balance'].'</td> <td class="internal border-bottom" align="center">N/A</td> </tr> '; }; to make a condition, I do this: foreach($bHistory as $row){ if($type == 'Deposit'){ if($row['Memo'] == 'Deposit'){ $result .= ' <tr class="internal-03 highlight"> <td class="internal border-bottom" align="center">'.$row['Id'].'</td> <td class="internal border-bottom" align="center">'.$data['wallet'].'</td> <td class="internal border-bottom" align="center">'.date('y-m-d h:i:s',$row['Date']).'</td> <td class="internal border-bottom" align="center"><strong>'.$row['Memo'].'</strong></td> <td class="internal border-bottom" align="center">N/A</td> <td class="internal border-bottom" align="center">'.$data['currency'].'</td> <td class="internal border-bottom" align="center">'.$row['Status'].'</td> <td class="internal border-bottom" align="center">'.$row['Balance'].'</td> <td class="internal border-bottom" align="center">N/A</td> </tr> '; } } elseif($type == 'AffiliateFee') { if($row['Memo'] == 'AffiliateFee'){ $result .= ' <tr class="internal-03 highlight"> <td class="internal border-bottom" align="center">'.$row['Id'].'</td> <td class="internal border-bottom" align="center">'.$data['wallet'].'</td> <td class="internal border-bottom" align="center">'.date('y-m-d h:i:s',$row['Date']).'</td> <td class="internal border-bottom" align="center"><strong>'.$row['Memo'].'</strong></td> <td class="internal border-bottom" align="center">N/A</td> <td class="internal border-bottom" align="center">'.$data['currency'].'</td> <td class="internal border-bottom" align="center">'.$row['Status'].'</td> <td class="internal border-bottom" align="center">'.$row['Balance'].'</td> <td class="internal border-bottom" align="center">N/A</td> </tr> '; } }else{ $result .= ' <tr class="internal-03 highlight"> <td class="internal border-bottom" align="center">'.$row['Id'].'</td> <td class="internal border-bottom" align="center">'.$data['wallet'].'</td> <td class="internal border-bottom" align="center">'.date('y-m-d h:i:s',$row['Date']).'</td> <td class="internal border-bottom" align="center"><strong>'.$row['Memo'].'</strong></td> <td class="internal border-bottom" align="center">N/A</td> <td class="internal border-bottom" align="center">'.$data['currency'].'</td> <td class="internal border-bottom" align="center">'.$row['Status'].'</td> <td class="internal border-bottom" align="center">'.$row['Balance'].'</td> <td class="internal border-bottom" align="center">N/A</td> </tr> '; } }; But no output is given back. . There's no error on my code . . I know it should be running correctly. . Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1422572 Share on other sites More sharing options...
trq Posted April 3, 2013 Share Posted April 3, 2013 There's no error on my code . . I know it should be running correctly. .You've already stated it's not working correctly so that disproves that theory. I don't see where $type is defined, nor do I see $result printed anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1422605 Share on other sites More sharing options...
lilmer Posted April 3, 2013 Author Share Posted April 3, 2013 Sorry for given incomplete information. the variable $type is from a $_POST['type'] given by ajax . . I just didn't include the print $result syntax but there is. . Yeah that is what I'm guessing, so any Idea how can I make a condition inside a foreach. Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1422620 Share on other sites More sharing options...
trq Posted April 3, 2013 Share Posted April 3, 2013 Have you tried any debugging? Some simple debugging would include printing type and $row['Memo'] to see what values they contain. Do you really need a condition? Looks like your trying to print the same thing regardless. Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1422629 Share on other sites More sharing options...
lilmer Posted April 6, 2013 Author Share Posted April 6, 2013 There's a problem retrieving the data['memo']. . it add additional space. Thanks Mr Admi and Mr guru. Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1423249 Share on other sites More sharing options...
jcbones Posted April 6, 2013 Share Posted April 6, 2013 You can trim the space out. But, you are still asking for the same display reguardless of what the condition is. To get only the 'type' that is sent via AJAX, you could always do: foreach($bHistory as $row){ if($row['Memo'] != $_GET['type']) continue; $result .= ' <tr class="internal-03 highlight"> <td class="internal border-bottom" align="center">'.$row['Id'].'</td> <td class="internal border-bottom" align="center">'.$data['wallet'].'</td> <td class="internal border-bottom" align="center">'.date('y-m-d h:i:s',$row['Date']).'</td> <td class="internal border-bottom" align="center"><strong>'.$row['Memo'].'</strong></td> <td class="internal border-bottom" align="center">N/A</td> <td class="internal border-bottom" align="center">'.$data['currency'].'</td> <td class="internal border-bottom" align="center">'.$row['Status'].'</td> <td class="internal border-bottom" align="center">'.$row['Balance'].'</td> <td class="internal border-bottom" align="center">N/A</td> </tr> '; }; Quote Link to comment https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/#findComment-1423292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.