Jump to content

Foreach then conditioning


lilmer

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/276423-foreach-then-conditioning/
Share on other sites

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
}
 

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. . 

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.

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>

'; 



};

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.