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

Link to comment
Share on other sites

Example is this image right here:

 

des.png

 

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 by lilmer
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

'; 



};
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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