cheeseus Posted July 1, 2010 Share Posted July 1, 2010 Hello, I don't seem to be able to get the hang of arrays, multidimensional ones in particular and I need some help printing this one here. First, I get the data from the DB by querying it as many times as projectIDs I send. foreach($projectIDs as $row => $value) { $query = mysql_query("SELECT * FROM ".PROJECT_TABLE." WHERE id = '$value' "); $row = mysql_fetch_array($query); $projectID[] = $row['id']; $projectname[] = $row['projectname']; $projecttype[] = $row['type']; $sourcelang[] = $row['sourcelang']; $targetlang[] = $row['targetlang']; $units[] = $row['units']; $unit[] = $row['unit']; $unitprice[] = $row['unitprice']; $currency[] = $row['currency']; $totalprice[] = $row['totalprice']; $clientname[] = $row['clientname']; $clientid[] = $row['clientid']; } $rows = array( 'projectID' => $projectID, 'projectname' => $projectname, 'projecttype' => $projecttype, 'sourcelang' => $sourcelang, 'targetlang' => $targetlang, 'units' => $units, 'unit' => $unit, 'unitprice' => $unitprice, 'currency' => $currency, 'totalprice' => $totalprice, 'clientname' => $clientname, 'clientid' => $clientid, ); Then I get the contents of the queries: $projectDataMulti = invoiceDataMulti($projectID); Now I need to get the contents grouped by projectID and here's where I got stuck. This method does print the results but ont in a way I can further manipulate them, which I need to do before I print them. foreach($projectDataMulti as $key => $value) { echo $key.": "; foreach($value as $iKey => $iValue) { echo $iKey ." - ".$iValue.", "; } echo "<br />"; } I need to get the results in the following manner: projectID0: projectname0 + projecttype0 + sourcelang0 + sourcelang0; projectID1: projectname1 + projecttype1 + sourcelang1 + sourcelang1; etc. so that I can print the relevant values in the relevant fields. Please explain how to do this and why, don't just give me the answer. Thank you! Quote Link to comment Share on other sites More sharing options...
miancu Posted July 1, 2010 Share Posted July 1, 2010 I think you got lost in so many arrays you're using. If I understand correctly your needs, you might be able to simplify your $rows array when you retrieve the data from database: $projects = array(); //this is instead of your $rows foreach($projectIDs as $row => $value) { $query = mysql_query("SELECT * FROM ".PROJECT_TABLE." WHERE id = '$value' "); $row = mysql_fetch_array($query); $projects[$row['id']] = array(); $projects[$row['id']]['projectname'] = $row['projectname']; $projects[$row['id']]['projecttype'] = $row['type']; $projects[$row['id']]['sourcelang'] = $row['sourcelang']; $projects[$row['id']]['targetlang'] = $row['targetlang']; $projects[$row['id']]['units'] = $row['units']; // continue here with rest of variables you have } You could then use $projects array for further display or manipulation. Let me know if this helps you! Quote Link to comment Share on other sites More sharing options...
cheeseus Posted July 1, 2010 Author Share Posted July 1, 2010 Worked right out of the box! Thank you very much! I think what I am missing on arrays is this part exactly - multidimensional arrays (still learning). I will try to comprehend it. Was there a way to get the same results out of the mess I'd done? Here is how I received my results before: <pre> projectID: 0 - 37, 1 - 35, projectname: 0 - XXXXX, 1 - YYYYY, projecttype: 0 - translation, 1 - translation, sourcelang: 0 - BG, 1 - BG, targetlang: 0 - ENG, 1 - ENG, units: 0 - 1267, 1 - 11000, unit: 0 - words, 1 - words </pre> Quote Link to comment Share on other sites More sharing options...
miancu Posted July 1, 2010 Share Posted July 1, 2010 you're welcome! glad to help! it would probably work somehow with your version too, but i didn't try it, since it seemed to complex and i didn't really have an example to see them. Quote Link to comment Share on other sites More sharing options...
cheeseus Posted July 1, 2010 Author Share Posted July 1, 2010 Oh, seems I haven't figured it out completely. Now ... how exactly do I print an individual value? I was misled by what I printed above as a preview result, using this: foreach($projectDataMulti as $key => $value) { echo $key.": "; foreach($value as $iKey => $iValue) { echo $iKey." - ".$iValue.", "; } } Here's the preview: 37: projectname - XXXXX, projecttype - translation, sourcelang - BG, targetlang - ENG, units - 1267... 35: projectname - YYYYY, projecttype - translation, sourcelang - BG, targetlang - ENG, units - 11000... But I can't access an individual item of the results, e.g. projectXXXXX's source and target languages. Quote Link to comment Share on other sites More sharing options...
cheeseus Posted July 1, 2010 Author Share Posted July 1, 2010 I don't feel particularly clever... It can't be that easy: echo $value['projectname']." - ".$value['sourcelang']." - ".$value['targetlang']."<br />"; Quote Link to comment Share on other sites More sharing options...
miancu Posted July 1, 2010 Share Posted July 1, 2010 Does this help? // I assume you know which project ID you want = $projectID echo "projectname: ". $projects[$projectID]['projectname'] . "<br />"; echo "targetlang: ". $projects[$projectID]['targetlang'] . "<br />"; //... more values to display here according to the key - targetlang , sourcelang , units If this is not what you need, please give more details, because I am a little confused on what you need here. Quote Link to comment Share on other sites More sharing options...
cheeseus Posted July 2, 2010 Author Share Posted July 2, 2010 Where I wrote "that easy" I mean it does give me the wanted data but not in the way I can further manipulate it. Here's where I am now: foreach($projectDataMulti as $key => $project) { $description = $project['projectname']." - ".$project['projecttype']." from ".$project['sourcelang']." into ".$project['targetlang']; $unit = $project['unit']; $units = $project['units']; $unitprice = $project['unitprice']; $currency = $project['currency']; $totalprice = $project['totalprice']; $clientname = $project['clientname']; $clientid = $project['clientid']; } But these variables ($description, $units...) they are arrays too, aren't they? In the HTML form I print below I need to be able to handle each variable separately, because I need to fill them in their relevant fields. This whole thing is for the part where I want to print an invoice for more than one project at the same time. And in the invoice HTML I have separate "description", "unit", "units", "line total" fields for each project (I calculate "line total" using JS). So my question is, from the above foreach cycle, how do I extract an individual value? Here's my HTML: <tr> <td class=\"input\">1.</td> <td class=\"input\"><input type=\"text\" name=\"description\" id=\"description\" size=\"40\" value=\"".$description."\" /></td> <td class=\"input\"><input size=\"5\" type=\"text\" name=\"unit\" id=\"unit\" value=\"".$unit."\" /></td> <td class=\"input\"> <input type=\"text\" name=\"unitprice\" id=\"unitprice\" size=\"4\" value=\"".$unitprice."\" /> <input type=\"text\" name=\"currency\" id=\"currency\" size=\"4\" value=\"".$currency."\" /> </td> <td class=\"input\"> <input type=\"text\" name=\"units\" id=\"units\" size=\"7\" value=\"".$units."\" /> </td> <td class=\"input\"> <input type=\"text\" name=\"linetotal\" id=\"linetotal\" size=\"7\" onfocus=\"calculate()\" value=\"".$linetotal."\" /> <script type=\"text/javascript\">calculate();</script> <input type=\"text\" name=\"currency\" id=\"currency\" size=\"4\" value=\"".$currency."\" /> </td> </tr> Do I need to put this HTML inside the foreach loop, or should I better 1. count how many times the foreach loop is iterated. 2. assign a value to each of the iterated variables, e.g. $description[0], $description[1], ... (which I am not too sure how) 3. print these values in the HTML. Hope my explanations aren't too obscure. Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.