Jump to content
Larry Ullman's Book Forums

bevc

Members
  • Posts

    8
  • Joined

  • Last visited

bevc's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. This is related to another question I asked that I haven't managed to resolve, so I'm trying something else! I have an associative array fetched from mysql, and I need to sum values based on the value in a different column. I have created an associative array, and printed to screen and it looks okay. It returns what I need, and looks like [1] => 779700.00 ) Array ( [4] => 868.00 ) Array ( [2] => 102000.00 ) Array ( [2] => 775808.00 My problem is that I need to be able to sum the results for each individual key value pair (so eg '2' would sum to 877808.00) - there are only eight key values, and I need a variable to hold each one. I have read lots of similar questions and have tried the solutions, but have just got myself hopelessly confused. I assume I need a foreach loop, please could somebody point me in the right direction for what I need to do? I'm not sure if I'm barking up the wrong tree or not. Thanks. while($row_outputs=mysqli_fetch_array($run_projects)) { $Id = $row_outputs['id']; $impact = $row_outputs['impact']; $cost = $row_outputs['total_cost']; $summing_array = array($impact=>$cost);
  2. It wasn't that, but thank you very much for the reply. I also realised that I put the wrong array in the foreach, but that didn't fix it either! I can't figure it out, so I've gone back to the drawing board to try something else. Cheers.
  3. I'm having a problem with a foreach loop that isn't doing what I need it to. I want to take an array of id numbers, fetch the figures related to that id number from my mysql db and sum them. Initially the problem was that my loop was 'overwriting itself' so that the only summing it did was of the last id number. Then after I fixed that, although I have used a 'unique array', it seems that the loop is still summing everything, ie if the array contains the same id number 8 times, it loops through and multiplies that figure x8 (at least I think that's what it's doing). I'm not sure why while($row_outputs=mysqli_fetch_array($run_projects)){ $prog_name = $row_outputs['prog_name']; $proj_name = $row_outputs['name']; $projectId = $row_outputs['id']; $proj_array = array($projectId); $proj_array_uni = array_unique($proj_array); $projecty = ''; foreach($proj_array as $projectile){ $projectile = trim($projectile); $projecty .= $projectile; $get_outputs_active = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='1'"; $run_outputs_active = mysqli_query($conn, $get_outputs_active); while ($row_outputs_active =mysqli_fetch_array($run_outputs_active)){ $total_outputs_active += $row_outputs_active['total_cost']; } $ftotal_outputs_active = number_format($total_outputs_active,2); $get_outputs_run = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='2'"; $run_outputs_run = mysqli_query($conn, $get_outputs_run); while ($row_outputs_run =mysqli_fetch_array($run_outputs_run)){ $total_outputs_run += $row_outputs_run['total_cost']; } } ?> Can anybody see what my problem is? Thanks.
  4. That worked, thank you very much indeed, I should have been concentrating on the SQL statement(s) and checking them first in phpmyadmin! Once I had got that right I felt much more confident in what I was doing
  5. I'm having a bit of a problem which I think I might know the solution, but I'm really struggling to know the correct syntax. If somebody could point me in the right direction I'd be very grateful. I've created an application where users can create projects, and within the projects there are 'outputs' and 'inputs'. All of which is stored in separate MySql tables. It works fine. I have now been asked to create an addition layer, a 'programme' layer, which sits above projects (ie there can be multiple projects within one programme). I think I have got my table normalisation correct, I created 2 tables, 'programmes' which stores programme id and name etc, and 'programmes_projects' which stores only the programme id and the project id. That seems to work ok. What I'm having trouble with, is displaying the results correctly. I need to retrieve information from 4 (actually 5) related MySql tables. Phew. I think I need to create an associative array and a foreach loop, but because of the number of tables and the relationships, I'm getting very confused. I'm not sure if I should be focussing on the sql statements, the php, or what. I tried using while loops, because that's what I know, but wasn't surprised that it didn't work. progId = $_GET['prog']; //get the individual projects from the linking db table $get_projects = "SELECT * FROM programmes_projects WHERE prog_id=$progId"; $run_projects = mysqli_query($conn, $get_projects); $i = 0; while($row_projects=mysqli_fetch_array($run_projects)){ $prog_proj_id = $row_projects['prog_proj_id']; $prog_id = $row_projects['prog_id']; $projectId = $row_projects['proj_id']; $i++; //attempting to collect project details from the outputs table $get_proj_details = "SELECT * FROM projects WHERE project_id=$projectId"; $fetch_projects = mysqli_query($conn, $get_proj_details); $i = 0; while($row_details=mysqli_fetch_array($fetch_projects)){ $proj_name = $row_details['project_name']; $proj_whatever = $row_details['date']; $i++; // project outputs $fetch_outputs = "SELECT * FROM projects_outputs WHERE project_id=$projectId"; $get_outputs = mysqli_query($conn, $fetch_outputs); $i = 0; while($row_outputs=mysqli_fetch_array($get_outputs)){ $out_id = $row_outputs['output_id']; $output_desc = $row_outputs['output_desc']; $i++; }}}?> Please could someone help?? Even if it's just a nudge in the right direction. Thank you very much.
  6. Thank you very much for the response, your book was EXTREMELY helpful, and was what got me through the project (my first php experience), I’m looking forward to reading your other books on php and Ruby too! I used something called 'html2canvas' and some cribbed javascript which created a pdf but the file was huge, and the content didn’t render property at all. I have been able to use ‘html2pdf’ and ‘dompdf’ to create ‘Hello World’-type pdfs, but I can't figure out how to take the dynamically created content and create a report that looks like what appears in the browser. I know very little about libraries, if you could point me in the right direction and tell me what library or functions I should be concentrating on, or any advice at all, it would be very much appreciated. Thank you!
  7. I apologise in advance if I'm committing a faux pas posting here, I couldn't see any forum rules! I read Mr Ullman's PHP for the web book, and it was the best I've read, really clear and easy to follow, it helped me from start to finish on what was (for me) a very big project, but this topic doesn't relate to it. I have what I thought would be small problem, but is causing me endless headaches. The users on my project have requested that the final report (that is created using php and mysql) be downloadable as pdf. The best I have managed is a fuzzy blurry pdf that's hugely bloated and illegible and the colours don't render properly! All the tutorials and code I've seen online seem to deal with creating either a 'new' pdf (eg an invoice) or they just render static html, rather than reproducing exactly what's in the browser from php. Safari's 'Export as pdf' command does basically exactly what I want, but I can't ask users to make sure they use Safari! if anybody could help me or point me in the right direction I'd be extremely grateful, it's driving me a little bit doolally. Thank you very much.
×
×
  • Create New...