Jump to content

Help printing a multidimensional array


cheeseus

Recommended Posts

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!

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

8) 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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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.