New to PHP, need a little guidance |
[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() { Any advice would be wonderful, and thanks in advance for your patience with me
[eluser]davedriesmans[/eluser]
use result_array() instead of row_array() you can find more here: http://ellislab.com/codeigniter/user-gui...sults.html
[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() {
[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() { Can anyone show me my mistake?
[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']; 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!
[eluser]Chris Davies[/eluser]
Hi Aken. Thanks for your reply. Code: foreach ($query->result_array() as $data) 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(); Thank you
[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.
[eluser]Chris Davies[/eluser]
Could you possibly show me an example as I am getting myself mighty confused.
[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();
[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? |
Welcome Guest, Not a member yet? Register Sign In |