DeepakJ Posted August 1, 2007 Share Posted August 1, 2007 How could you define a multi dimensional array in another multidimensional array. $pidcounter = 0; foreach ($invoicenumlist as $key => $value){ foreach ($invoicenumlist[$key] as $key1 => $value1){ $query3 = "SELECT * FROM productid where invoicenum = '$value1'"; $Result3 = mysql_query($query3); while ($row3=mysql_fetch_array($Result3)){ $productidlist[$key][]=$row3['productid']; $pidcounter = $pidcounter+1; } } } Hopefully this code will illustrate what I am trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/ Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 Well, i would imagine what you are after is: foreach ($invoicenumlist as $key => $value){ foreach ($value as $key1 => $value1){ $query3 = "SELECT * FROM productid where invoicenum = '$value1'"; $Result3 = mysql_query($query3); while ($row3=mysql_fetch_array($Result3)){ $productidlist[$key][]=$row3['productid']; $pidcounter = $pidcounter+1; } } } It is the value of each element of your array that is an array also, not the key. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312920 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 what is the $invoicenumlist structure? does the key mean anything, or is it just an array of invoice numbers with irrelevant keys? Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312928 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 the keys correspond with the customerkey Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312941 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 This is my full code. <?php header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="export.csv"'); $hostname = "localhost"; $username = "xxxx"; $password = "xxxx"; $dbname = "xxxx"; mysql_connect($hostname, $username, $password) or DIE("Unable to connect to MySQL server $hostname"); $selected = mysql_select_db($dbname) or DIE("Could not select requested db $dbname"); $query = "SELECT * FROM customerid"; $Result = mysql_query($query) or DIE("Could not query"); while ($row=mysql_fetch_array($Result)){ $customeridlist[]= $row['customerid']; } foreach ($customeridlist as $key => $customer){ $query2 = "SELECT * FROM invoiceid WHERE customerid ='$customer'"; $Result2 = mysql_query($query2) or DIE("Could not query2"); $keykeeper[] = $key; while ($row2=mysql_fetch_array($Result2)){ $invoicenumlist[$key][]= $row['invoicenum']; $numoflicenses[$key][] = $row['numoflicenses']; } } $pidcounter = 0; foreach ($invoicenumlist as $key => $value){ foreach ($value as $key1 => $value1){ $query3 = "SELECT * FROM productid where invoicenum = '$value1'"; $Result3 = mysql_query($query3); while ($row3=mysql_fetch_array($Result3)){ $productidlist[$key][]=$row3['productid']; $pidcounter[$key] = $pidcounter[$key]+1; } } } $counter=0; $str=""; $size = count($invoicenumlist); foreach ($invoicenumlist as $key => $value){ $size = count($invoicenumlist[$key][$value]); while ($size<3){ $invoicenumlist[$key][] = ""; $size=$size + 1; } } foreach ($numoflicenses as $key => $value){ $counter=0; foreach ($value as $key1 => $value1){ $counter = $counter + $value1; } $counter[$key]= $counter; } foreach ($counter as $key => $value){ $total[$key] = $counter[$key] - $pidcounter[$key]; } foreach ($keykeeper as $num => $key){ $str.= $customeridlist[$key].", ".$counter[$key].", ".$total[$key].", ".$invoicenumlist[$key][0].", ".$invoicenumlist[$key][1].", ".$invoicenumlist[$key][2]; foreach ($productidlist[$key] as $key1 => $value1){ $str.=", ".$value1; } $str.= "\n"; } // Open file export.csv. $f = fopen ('export.csv','w'); // Put all values from $out to export.csv. fputs($f, $str); fclose($f); ?> These are my errors: Notice: Undefined index: customerid in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line 16 Undefined variable: invoicenumlist in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line 28 Invalid argument supplied for foreach() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line 28 Undefined variable: invoicenumlist in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php on line 41 Undefined variable: invoicenumlist in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php on line 42 Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php on line 42 Undefined variable: numoflicenses in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>50</b><br /> Invalid argument supplied for foreach() in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>50</b><br /> <br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>58</b><br /> <br /> <b>Notice</b>: Undefined variable: total in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>62</b><br /> <br /> <b>Notice</b>: Undefined variable: invoicenumlist in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>62</b><br /> <br /> <b>Notice</b>: Undefined variable: invoicenumlist in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>62</b><br /> <br /> <b>Notice</b>: Undefined variable: invoicenumlist in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>62</b><br /> <br /> <b>Notice</b>: Undefined variable: productidlist in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>63</b><br /> <br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>63</b><br /> Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312951 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 I would guess that most of your problems stem from this one: Notice: Undefined index: customerid in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php on line 16 It seems that cutomerid is not a field in your database, as there is no key called customerid in the array returned by mysql_fetch_array() Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312959 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 i'd think you'd want to correspond the products to the invoice then; you can actually lump all the products into the same array, rather than having multiple. it's usually easier to deal with your code when you name your variables something meaningful, rather than just "key" and "value": $pidcounter = 0; foreach ($invoicenumlist as $customerKey => $invoiceNumArray){ // go through each invoice for this customer foreach ($invoiceNumArray AS $uselessKey => $invoiceNum){ // create an array of products, with customerKey as its primary key // and invoiceNum as its subkey // we use array_merge() to play nice with notices $invoicenumlist[$customerKey] = array_merge($invoicenumlist[$customerKey], array($invoiceNum => array())); // make an easy reference to this new sub-array $ref = &$invoicenumlist[$customerKey][$invoiceNum]; // grab all the products for this invoice $query3 = "SELECT productid FROM productid WHERE invoicenum = '$invoiceNum'"; $Result3 = mysql_query($query3); while ($row3=mysql_fetch_assoc($Result3)){ // put all these products into our array $ref[]=$row3['productid']; $pidcounter = $pidcounter+1; } // unset the empty array so that we don't get the array of invoice numbers stacked within the array of products ids unset($invoicenumlist[$customerKey][$uselessKey]); } } before running this, your array would look like (i assume): $invoicenumlist[customer one][0] => invoice number one $invoicenumlist[customer one][1] => invoice number two $invoicenumlist[customer one][2] => invoice number three $invoicenumlist[customer two][0] => invoice number one ... after running this, it SHOULD return an array that looks like: $invoicenumlist[customer one][invoice number one][0] => product one $invoicenumlist[customer one][invoice number one][1] => product two ... is that what you're after? EDIT: i missed the previous two while typing, but i've spent a while typing this and refuse to let it go to waste. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312962 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 Thanks both of you. It help alot. I have one more error popping up now. foreach ($invoicenumlist as $key => $value){ $size = count($invoicenumlist[$key][$value]); while ($size<3){ $invoicenumlist[$key][] = ""; $size=$size + 1; } } It says an illegal offset number on the count line. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312975 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 it's because $value is actually an array, not a key. if you want the size, run the count() function on that array itself: foreach ($invoicenumlist as $key => $value){ $size = count($value); while ($size<3){ $invoicenumlist[$key][] = ''; $size=$size + 1; } } Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312982 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 Now it says <b>Warning</b>: Cannot use a scalar value as an array in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>55</b><br /> <br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>58</b><br /> <br /> <b>Notice</b>: Undefined variable: total in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>62</b><br /> <br /> <b>Notice</b>: Undefined variable: productidlist in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>63</b><br /> <br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\InputMaintainance\reportdata1.php</b> on line <b>63</b><br /> Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312992 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 alright, i guess this is where a little more information is needed. wherever you see arrays not acting the way you expect, or getting foreach() or count() errors, use var_dump() and print_r() on the "array" in question. it will greatly help in debugging. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312996 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 I think it impacts this code specifically foreach ($numoflicenses as $key => $value){ $counter=0; foreach ($value as $key1 => $value1){ $counter = $counter + $value1; } $counter[$key]= $counter; } Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-312997 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 How do the above functions you named work Oops typo. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-313001 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 you can't freely interconvert arrays and scalars. you'll have to use a temporary variable to store that counted value, then add it to the $counter array. keep in mind this means you have to initialize $counter as an array, not a scalar (0). to find out how a function works, check the PHP manual at http://www.php.net. that's what it's there for. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-313002 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 you can't freely interconvert arrays and scalars. you'll have to use a temporary variable to store that counted value, then add it to the $counter array. keep in mind this means you have to initialize $counter as an array, not a scalar (0). to find out how a function works, check the PHP manual at http://www.php.net. that's what it's there for. I just started PHP I have no idea what interconverting arrays and scalars mean. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-313013 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 it means converting from an array to a scalar. if you don't know what these mean, have a look at the starter tutorial on the PHP website (http://www.php.net) and take a look at the initial topics of the language reference (variable types, control structures, etc.) Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-313020 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 <?php header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="export.csv"'); $hostname = "localhost"; $username = "root"; $password = "98989lol"; $dbname = "licensinginformation"; mysql_connect($hostname, $username, $password) or DIE("Unable to connect to MySQL server $hostname"); $selected = mysql_select_db($dbname) or DIE("Could not select requested db $dbname"); $query = "SELECT * FROM invoiceid"; $Result = mysql_query($query) or DIE("Could not query"); while ($row=mysql_fetch_array($Result)){ $customeridlist[]= $row['customerid']; } foreach ($customeridlist as $key => $customer){ $query2 = "SELECT * FROM invoiceid WHERE customerid ='$customer'"; $Result2 = mysql_query($query2) or DIE("Could not query2"); $pidcounter=0; while ($row2=mysql_fetch_array($Result2)){ $invoicenumlist[]= $row2['invoicenum']; $numoflicenses[] = $row2['numoflicenses']; } foreach ($invoicenumlist as $key1 => $value){ $query3 = "SELECT * FROM productid WHERE invoicenum='$value'"; $Result3=mysql_query($query3) or DIE("Could not query3"); while ($row3=mysql_fetch_array($Result3)){ $productidlist[]= $row3['productid']; if ($row3['productid']!= ""){ $pidcounter=$pidcounter+1; } } } $counter=0; $str=""; $size = count($invoicenumlist); while ($size<3){ $invoicenumlist[] = ""; $size=$size + 1; } foreach ($numoflicenses as $key2 => $value2){ $counter = $counter + $value; } $total = $counter - $pidcounter; $str.= $customer.", ".$counter.", ".$total.", ".$invoicenumlist[0].", ".$invoicenumlist[1].", ".$invoicenumlist[2]; foreach ($productidlist as $key3 => $value3){ $str.=", ".$value; } $str.= "\n"; unset($invoicenumlist); unset($productidlist); unset($numoflicenses); echo $str; } // Open file export.csv. $f = fopen ('export.csv','w'); // Put all values from $out to export.csv. fputs($f, $str); fclose($f); ?> Why doesn't this script work. I am really lost here. Someone please help. Quote Link to comment https://forums.phpfreaks.com/topic/62864-solved-how-would-you-define-a-multidimensional-array-in-a-loop/#findComment-313072 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.