• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to PHP, need a little guidance

#1
[eluser]Chris Davies[/eluser]
Hi all. I have been using Expression Engine for a year now. I have some basic PHP knowledge, but have never really used it as such.

I have a little bit of spare time, and have started looking into learning some to make a module for EE. I have managed to get quite far but have got myself a little confused and was hoping I could find some help here as I know EE is build on codeigniter.

Basically, I have a function that I want to query the database and find all 'follow_id' that match the user id that is entered in the tagdata.

The issue I have is I am only outputting one row, and I dont really understand how to output multiple rows...

Code:
public function summary() {
    
     $tagdata = $this->EE->TMPL->tagdata;
    
     $user_id = $this->EE->TMPL->fetch_param('user_id');
     if( $user_id === FALSE ) {
         return "";
     }
    
     $this->EE->db->select( "follow_id, COUNT(*) as total" );
     $this->EE->db->where( "member_id", $user_id );
     $this->EE->db->group_by( "follow_id" );
     $query = $this->EE->db->get( "follow" );

     if( $query->num_rows() == 0 ) {
         $data = array(
             "follow_id" => 0
         );
     } else {
         $data = $query->row_array();            
     }

     $variables = array();
     $variables[] = $data;

     return $this->EE->TMPL->parse_variables( $tagdata, $variables );
}

Any advice would be wonderful, and thanks in advance for your patience with me Smile

#2
[eluser]davedriesmans[/eluser]
use result_array() instead of row_array()

you can find more here: http://ellislab.com/codeigniter/user-gui...sults.html

#3
[eluser]Chris Davies[/eluser]
Thanks davedriesmans.

I have given the manual a read, but am not entirely sure how I would use this in my function.

I tried how I thought, and have failed epically, see below...

Could you possibly give me an example?

Code:
public function summary() {
    
     $tagdata = $this->EE->TMPL->tagdata;
    
     $user_id = $this->EE->TMPL->fetch_param('user_id');
     if( $user_id === FALSE ) {
         return "";
     }
    
     $this->EE->db->select( "follow_id, COUNT(*) as total" );
     $this->EE->db->where( "member_id", $user_id );
     $this->EE->db->group_by( "follow_id" );
     $query = $this->EE->db->get( "follow" );
    
     $data = foreach ($query->result_array() as $row)
  {
     echo $row['follow_id'];
  }    

     $variables = array();
     $variables[] = $data;

     return $this->EE->TMPL->parse_variables( $tagdata, $variables );
}

#4
[eluser]Chris Davies[/eluser]
Okay, so I fixed the issue above, then realised I don't want to echo out the results. I want them to output through my variables.

I have managed to get my results to output through my variables, but each result is printed on a new row. I want the 'follow_id's' to be returned with a '|' between, and the overall total, not the total of each row.

Code:
public function summary() {
    
     $tagdata = $this->EE->TMPL->tagdata;
    
     $user_id = $this->EE->TMPL->fetch_param('user_id');
     if( $user_id === FALSE ) {
         return "";
     }
    
     $this->EE->db->select( "follow_id, COUNT(*) as total" );
     $this->EE->db->where( "member_id", $user_id );
     $this->EE->db->group_by( "follow_id" );
     $query = $this->EE->db->get( "follow" );
    
     foreach ($query->result_array() as $data)
     {
      $variables[] = array(
             'follow_id' => $data['follow_id'],
             'total' => $data['total']
         );
     }
  
   return $this->EE->TMPL->parse_variables( $tagdata, $variables );
}

Can anyone show me my mistake?

#5
[eluser]Aken[/eluser]
Your mistake is that you aren't doing anything to turn your IDs and totals into the format you want.

Put all your follow_id's into an array using something like

Code:
$follow_ids[] = $data['follow_id'];

Then use implode() to turn them into a single string. For your totals, use math to increase a single variables total.

Code:
$total += $data['total'];
// Same as:
$total = $total + $data['total'];

Note I didn't just write out the whole solution for you. You should understand it more if you code most of it yourself, so there's a start!

#6
[eluser]Chris Davies[/eluser]
Hi Aken. Thanks for your reply.

Code:
foreach ($query->result_array() as $data)
     {
      $variables[] = array(
             'follow_id' => $data['follow_id'],
         );
     }

I have been looking at the code above. This takes each row, and prints out the information, which is my case current prints two rows of follow_id and total.

I want just one row how ever many follow ids are returned, and to have them returned all together like so: 1|2|3 etc. Currently I have a foreach loop which is definitely not going to do this for me right?

So my question is, instead of a foreach loop what can I use?

I tried this, but it doesnt work:

Code:
$data = $query->result_array();

$variables[] = array(
    'follow_id' => $data['follow_id'],
);

Thank you Smile


#7
[eluser]Aken[/eluser]
1) Define two variables: an empty array for follow_id's, and one for total (starts at zero).

2) In the foreach(), add each follow_id to the array, and add the total to your total variable.

3) After the foreach, create your $variables item using your two variables.

#8
[eluser]Chris Davies[/eluser]
Could you possibly show me an example as I am getting myself mighty confused.

#9
[eluser]Aken[/eluser]
I'd suggest getting more familiar with the basics of PHP if this has been confusing you. It seems you aren't too keen on how arrays and foreach() work quite yet.

Code:
$follow_ids = array();
$total = 0;

foreach ($query->result_array() as $row)
{
$follow_ids[] = $row['follow_id'];
$total += $row['total'];
}

$variables = array(
array(
  'follow_ids' => implode('|', $follow_ids),
  'total'   => $total
)
);

#10
[eluser]Chris Davies[/eluser]
Hi Aken.

Yes, I really think I need to.

Looking at your example I understand everything but the variables section. Could you possibly explain why their are two arrays, one inside the other?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.