PHP - Foreach And $data[$var][$i] Arrays
Hello ,
I have a table like $data[$var][$i] $data is an string, $var is a string and $i is an index int. I am tired to write for($i=1;,$i<=count($$data[$var],$i++) { echo $data['id'][$i] ... }oouuuff How i can make it with foreach ? I tried foreach ($data -> $field as $value) echo $value; but i doesnt work. Any advise ? Similar TutorialsHow do i ouput it like this Code: [Select] Array ( [Griffin] => Array ( [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) ) rather than this Code: [Select] Array ( [field_name] => first_name [field_type] => 1 [max_length] => 20 ) Array ( [field_name] => last_name [field_type] => 1 [max_length] => 40 ) Array ( [field_name] => email [field_type] => 1 [max_length] => 80 ) Code: [Select] $fields = array(); foreach ($_POST['use'] as $field){ $fields = array( "field_name"=>$field, "field_type"=>$match_types[$i], "max_length"=>$match_length[$i]); print_r($fields); echo $fields[0]['field_name']; //echo $field.' - Type: '.$match_types[$i].' - Lenght: '.$match_length[$i]."<br />"; $i++; } Hi everyone, If you take a look at my code below I am having trouble echoing the key/ value pair of the 'sex' array which is a sub array of 'pens'. I have tried so many different ways but to no avail, thanks Chris! Code: [Select] <?php $products = array( 'paper' => array( 'copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"), 'pens' => array( 'ball' => "Ball Point", 'hilite' => "Highlighters", 'marker' => "Markers", 'sex' => array ( 'condom' => "Protection")), 'misc' => array( 'tape' => "Sticky Tape", 'glue' => "Adhesives", 'clips' => "Paperclips") ); echo "<pre>"; foreach ($products as $section => $items ) foreach ($items as $key => $value) echo "$section:\t$key\t($value)<br>"; echo "</pre>"; ?> I have two arrays as Code: [Select] $array1=array("word1","word2","word3"); $array2=array("term1","term2","term3"); How can I have a foreach loop to echo "word1-term1", "word2-term2", .... I mean having Code: [Select] echo "$array1[0]-$array2[0]";but in a foreach loop Hey forum, I am a new php user and am having some issues (probably pretty basic to the likes of more experienced users), I am trying to make a script that has 4 checkboxes and a submit button, the values of the checkboxes should be passed through the array using a foreach loop, I got the basic template setup but when trying to pass the array I ran into a few problems. If anyone would take a peek at my code and point me in the right direction it would be much appreciated. Thanks forum. [attachment deleted by admin] Let me explain my problem. I have an array with dates and numbers in format ($cronograma) Ex: Array ( [2020-09-21] => Array ( [0] => 2020-09-21 [1] => 2 [2] => 2 [3] => 2 ) [2020-09-28] => Array ( [0] => 2020-09-28 [1] => 2 [2] => 2 [3] => 4 ) Then i have another array with 2 ids (in this case 58,60) ($id) Finally i have a third array with numbers only (in this case 34,34) $tot So what i want is cross information beween them, for example for id 58 I must get dates (first element and last element when $tot = 34) for id 60 I must get dates (first element after $tot =34 and last element of array) Whath i have so far is this foreach ($id as $idPlan) { foreach ($cronograma as $c) { $t1 = 0; foreach ($tot as $d) { $t1 += (int)$d['tempos']; if ($c[3] == $t1) { $newAr[] =$idPlan; $newAr[] = $c[0]; } } } } My response array(8) { [0]=> string(2) "58" [1]=> string(10) "2021-02-01" [2]=> string(2) "58" [3]=> string(10) "2021-06-14" [4]=> string(2) "60" [5]=> string(10) "2021-02-01" [6]=> string(2) "60" [7]=> string(10) "2021-06-14" } null So it's clear that i have all repeated I should have a line like: 58 - 2020-09-21 -2021-02-01 Any help? Hi, after banging my head against the wall for a while thinking this would be a simple task, I'm discovering that this is more complicated than I thought. Basically what I have is a link table linking together source_id and subject_id. For each subject there are multiple sources associated with each. I had created a basic listing of sources by subject... no problem. I now need a way of having a form to create an ordered list in a user-specified way. In other words, I can currently order by id or alphabetically (subject name lives on a different table), but I need the option of choosing the order in which they display. I added another row to this table called order_by. No problem again, and I can manage all of this in the database, however I want to create a basic form where I can view sources by subject and then enter a number that I can use for sorting. I started off looping through each of the entries and the database (with a where), and creating a foreach like so (with the subject_id being grabbed via GET from the URL on a previous script) Code: [Select] while($row = mysqli_fetch_array($rs)) { //update row order if (isset($_POST['submit'])) { //get variables, and assign order $subject_id = $_GET['subject_id']; $order_by = $_POST['order_by']; $source_id = $row['source_id']; //echo 'Order by entered as ' . $order_by . '<br />'; foreach ($_POST['order_by'] as $order_by) { $qorder = "UPDATE source_subject set order_by = '$order_by' WHERE source_id = '$source_id' AND subject_id = '$subject_id'"; mysqli_query($dbc, $qorder) or die ('could not insert order'); // echo $subject_id . ', ' . $order_by . ', ' . $source_id; // echo '<br />'; } } else { $subject_id = $_GET['subject_id']; $order_by = $row['order_by']; $source_id = $row['source_id']; } And have the line in the form like so: Code: [Select] echo '<input type="text" id="order_by" name="order_by[]" size="1" value="'. $order_by .'"/> (yes I know I didn't escape the input field... it's all stored in an htaccess protected directory; I will clean it up later once I get it to work) This, of course, results in every source_id getting the same "order_by" no matter what I put into each field. I'm thinking that I need to do some sort of foreach where I go through foreach source_id and have it update the "order_by" field for each one, but I must admit I'm not sure how to go about this (the flaws of being self-taught I suppose; I don't have anyone to go to on this). I'm hoping someone here can help? Thanks a ton in advance Greetings to all! I am very new to this so I may not be on the right track. I am providing some code I have because I think this is the best way to explain what I am trying to do. I have a database and php pages that generate a pedigree when one selects any particular dog from the search page. If anyway has a tip on how to simplify this, I would sure appreciate it!! First, I request an array for the dog selected: Code: [Select] $sql = "SELECT RegName, SireID, DamID FROM pedigrees WHERE ID="; $thisDogQ = $sql . $values["ID"]; $thisDogR = db_query($thisDogQ,$conn); $thisDogRow_value = mysql_fetch_assoc($thisDogR); $thisDogRow_value = array ( 'RegName' => $thisDogRow_value['RegName'], 'SireID' => $thisDogRow_value['SireID'], 'DamID' => $thisDogRow_value['DamID']); </code> //Next I request the same information for his sire <code> if (is_numeric($thisDogRow_value['SireID'])) { $query_SireQ = $sql . $thisDogRow_value['SireID']; $SireR = mysql_query($query_SireQ, $conn) or die(mysql_error()); $SireRow_value = mysql_fetch_assoc ($SireR); $SireRow_value = array ( 'RegName' => $SireRow_value['RegName'], 'SireID' => $SireRow_value['SireID'], 'DamID' => $SireRow_value['DamID']); </code> //and then his dam <code> if (is_numeric($thisDogRow_value['DamID'])) { $query_DamQ = $sql . $thisDogRow_value['DamID']; $DamR = mysql_query($query_DamQ, $conn) or die(mysql_error()); $DamRow_value = mysql_fetch_assoc ($DamR); $DamRow_value = array ( 'RegName' => $DamRow_value['RegName'], 'SireID' => $DamRow_value['SireID'], 'DamID' => $DamRow_value['DamID']); </code> Now, here's the rub: Now I need the sire and the dam for those 2 [described above] so this time through, I will need the same information for FOUR dogs. The next time will be for EIGHT, then SIXTEEN - and so on until there are a total of 256 dogs. <code> //Get PatGrandSire 2 if (is_numeric($SireRow_value['SireID'])) { $PatGrandSireQ = $sql . $SireRow_value['SireID']; $PatGrandSireR = mysql_query ($PatGrandSireQ, $conn) or die(mysql_error()); $PatGrandSireRow_value = mysql_fetch_assoc ($PatGrandSireR); $PatGrandSireRow_value = array ( 'RegName' => $PatGrandSireRow_value['RegName'], 'SireID' => $PatGrandSireRow_value['SireID'], 'DamID' => $PatGrandSireRow_value['DamID']); //GetPatGrandDam 17 if (is_numeric($SireRow_value['DamID'])) { $PatGrandDamQ = $sql . $SireRow_value['DamID']; $PatGrandDamR = mysql_query ($PatGrandDamQ, $conn) or die(mysql_error()); $PatGrandDamRow_value = mysql_fetch_assoc ($PatGrandDamR); $PatGrandDamRow_value = array ( 'RegName' => $PatGrandDamRow_value['RegName'], 'SireID' => $PatGrandDamRow_value['SireID'], 'DamID' => $PatGrandDamRow_value['DamID']); //Get Pat1GGrandSire-3 if (is_numeric($PatGrandSireRow_value['SireID'])) { $Pat1GGrandSireQ = $sql . $PatGrandSireRow_value['SireID']; $Pat1GGrandSireR = mysql_query ($Pat1GGrandSireQ, $conn) or die(mysql_error()); $Pat1GGrandSireRow_value = mysql_fetch_assoc ($Pat1GGrandSireR); $Pat1GGrandSireRow_value = array ( 'RegName' => $Pat1GGrandSireRow_value['RegName'], 'SireID' => $Pat1GGrandSireRow_value['SireID'], 'DamID' => $Pat1GGrandSireRow_value['DamID']); if (is_numeric($PatGrandSireRow_value['DamID'])) { $Pat1GGrandDamQ = $sql . $PatGrandSireRow_value['DamID']; $Pat1GGrandDamR = mysql_query ($Pat1GGrandDamQ, $conn) or die(mysql_error()); $Pat1GGrandDamRow_value = mysql_fetch_assoc ($Pat1GGrandDamR); $Pat1GGrandDamRow_value = array ( 'RegName' => $Pat1GGrandDamRow_value['RegName'], 'SireID' => $Pat1GGrandDamRow_value['SireID'], 'DamID' => $Pat1GGrandDamRow_value['DamID'], 'Title' => $Pat1GGrandDamRow_value['Title'], 'Title2' => $Pat1GGrandDamRow_value['Title2'], 'Title3' => $Pat1GGrandDamRow_value['Title3'], 'Title4' =>$Pat1GGrandDamRow_value['Title4'], 'Title5' => $Pat1GGrandDamRow_value['Title5'], 'Title6' =>$Pat1GGrandDamRow_value['Title6'], 'Title7' => $Pat1GGrandDamRow_value['Title7'], 'Title8' =>$Pat1GGrandDamRow_value['Title8'], 'Title9' => $Pat1GGrandDamRow_value['Title9'], 'Addl_Titles' => $Pat1GGrandDamRow_value['Addl_Titles']); //Get Pat2GGrandSire-18 if (is_numeric($PatGrandDamRow_value['SireID'])) { $Pat2GGrandSireQ = $sql . $PatGrandDamRow_value['SireID']; $Pat2GGrandSireR = mysql_query ($Pat2GGrandSireQ, $conn) or die(mysql_error()); $Pat2GGrandSireRow_value = mysql_fetch_assoc ($Pat2GGrandSireR); $Pat2GGrandSireRow_value = array ( 'RegName' => $Pat2GGrandSireRow_value['RegName'], 'SireID' => $Pat2GGrandSireRow_value['SireID'], 'DamID' => $Pat2GGrandSireRow_value['DamID'], 'Title' => $Pat2GGrandSireRow_value['Title'], 'Title2' => $Pat2GGrandSireRow_value['Title2'], 'Title3' => $Pat2GGrandSireRow_value['Title3'], 'Title4' => $Pat2GGrandSireRow_value['Title4'], 'Title5' => $Pat2GGrandSireRow_value['Title5'], 'Title6' => $Pat2GGrandSireRow_value['Title6'], 'Title7' => $Pat2GGrandSireRow_value['Title7'], 'Title8' => $Pat2GGrandSireRow_value['Title8'], 'Title9' => $Pat2GGrandSireRow_value['Title9'], 'Addl_Titles' => $Pat2GGrandSireRow_value['Addl_Titles']); //Get Pat2GGrandDaminfo-25 if (is_numeric($PatGrandDamRow_value['DamID'])) { $Pat2GGrandDamQ = $sql . $PatGrandDamRow_value['DamID']; $Pat2GGrandDamR = mysql_query ($Pat2GGrandDamQ, $conn) or die(mysql_error()); $Pat2GGrandDamRow_value = mysql_fetch_assoc ($Pat2GGrandDamR); $Pat2GGrandDamRow_value = array ( 'RegName' => $Pat2GGrandDamRow_value['RegName'], 'SireID' => $Pat2GGrandDamRow_value['SireID'], 'DamID' => $Pat2GGrandDamRow_value['DamID']); I am trying in vain to access the numbers in the "sims" array using the code below, I have tried various numbers of nested foreach loops but get errors every time, can anyone help point me in the correct direction please.
Thanks
Gordon
$stock = (array(1) { ["stock"]=> array(2) { [0]=> array(2) { ["operator"]=> string(3) "ECL" ["sims"]=> array(51) { [0]=> int(8944122650438410000) [1]=> int(8944122650438409000) [2]=> int(8944122650438409000) } } [1]=> array(2) { ["operator"]=> string(2) "JT" ["sims"]=> NULL } } } ) foreach ($stock as $key1 => $item1) { foreach($item1 as $key2 => $item2) { foreach($item2 as $key => $value) { echo $value[sims] . "</br>"; } } } Hi Guys I have never reall worked with xml and im stuck. I am gathering data from a class. include "example/class.php"; $data = new Class; $request = $data->List_Accounts(); $res = $data->run_xml($request); echo "<pre>"; print_r($res); echo "</pre>"; Ok so this is the output i get from the print_r(); <pre><!--?xml version="1.0" encoding="utf-8"?--> <rpc request_id="43535"> <response code="0" text="OK"> <account datatype="list"> <name datatype="username">arf34</name> <type datatype="account_type">3</type> <descr datatype="string">Linux Virtual Hosting</descr> <server datatype="int">35</server> </account> <account datatype="list"> <name datatype="username">awecerfewve</name> <type datatype="account_type">3</type> <descr datatype="string">Linux Virtual Hosting</descr> <server datatype="int">34</server> </account> </response></rpc></pre> How could i them loop through this data and pull out certain types like username and account_type using a foreach loop???? Can anyone please help with this. Thanks I have this query which query the parent.name and the node.name as well. Code: [Select] "SELECT parent.name FROM categories AS node, categories AS parent WHERE node.left_node BETWEEN parent.left_node AND parent.right_node AND node.name = '{$node_name}' ORDER BY parent.right_node AND node.left_node" below in the foreach loop is displaying both the node and parent, I only need the parent. I have done a little if statement saying that if node.name not to display but didn't work because both parent.name and node.name are displaying the he same field name. Code: [Select] <?php foreach($iterator as $key=>$value) { if($value <> "node.name") echo $value.'<br />'; } ?> Hi, I am trying to split a string into an array and then seeing what sort of data it is. eg; I get the following string Quote api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse I need to be able to put this into an array so in this case Code: [Select] myarray[type]=somethingelse myarray[api]=somerandomkeyhere myarray[user]=someuser myarray[password]=somehashvaluehere but it could include many more/less different value's separated by the & symbol. Thanks, -mme Ok so I'm trying to use jpgraph which allows you to make graphs with php (you dont need to know anything about jpgraph to help me) anyway how it works is this line data = array(1,3,4,5); the numbers here are what the graph plots. now i'm trying to figure out a way to place values from my database in there. the problem is the amount of values is constantly changing. I tried a lot of things but nothing is working here is my last try: $result = mysql_query("SELECT thismonth FROM list"); while ($row = mysql_fetch_array($result)) { $data[] = $row[0]; } $number = count($data); for ($i = 0; $i < $number; $i++) { $wtf .= $data[$i] . ',' ; } now when i echo $wtf i get values such as 6,3,2,0,1 now i tried this $data = array($wtf); but it doesnt work any ideas? Hi there, I am wanting to insert post data into my DB, this is guest information if i do a $safePost = filter_input_array(INPUT_POST); print_r($safePost); i get the following output. Array ( [action] => addGuestInfo [guestno] => 2 [bookingid] => 151 [customerid] => 22 [Guestname-0] => Jamie [Guestname-1] => Joe [GuestAge-0] => 4-3 [GuestInfo-0] => celiac [GuestAge-1] => 18 [GuestInfo-1] => wheat ) normally the guestname-0, guestage-0, guestinfo-0 would be in correct order but we have to ensure the just incase it isns't (see output above) we cater for that aswell, we can have upto 6 guest details coming in at once. so I need to loop through the data and insert into my DB. I have tried various for / foreach loops but cant seem to get it to group the data together I have tried things like below, but it inserts each record multiple times. $safePost = filter_input_array(INPUT_POST); foreach($safePost as $key => $value){ $exp_key = explode('-', $key); // for ($i =0; $i <= $guestcount; $i++) { switch ($exp_key[0]) { case 'Guestname': $booking->guestno = $exp_key[1]; $booking->guestname = $val; break; case 'GuestAge': $booking->guestno = $exp_key[1]; $booking->guestage = $val; break; case 'GuestInfo': $booking->guestno = $exp_key[1]; $booking->guestinfo = $val; break; default: break; } //CALLS FUNCTION TO INSERT TO DB result = lastInsertId(); $result= $booking->insertGuestDetails(); //} }
Hi There all, having an issue with simple xml... hopefully someone will be able to shed some light on this. I have this data in an xml feed from an API: <ID>job no 1 </ID> <Name>job name</Name> <State>job status</State> <Tasks> <Task> <Name>task name 1</Name> </Task> <Task> <Name>task name 2</Name> </Task> <ID>job no 2</ID> <Name>job name</Name> <State>job status</State> <Tasks> <Task> <Name>task name 1</Name> </Task> <Task> <Name>task name 2</Name> </Task> I would like to display these in a table pretty much as it is shown above. Job no 1 job name job status job 1 task name 1 job 1 task name 2 Job no 2 job name job status job 2 task name 1 job 2 task name 2 etc my code looks like this : $required is narrowing down specific states not shown for clarity $xml_current=simplexml_load_string($jobs_task_response) or die("Error: Cannot create object"); foreach($xml_current->Jobs->Job as $item_current) { if (in_array((string)$item_current->State, $required)) { $projects_current[] = array( 'job_no' => (string)$item_current->ID, 'job_name' => (string)$item_current->Name, 'job_status' => (string)$item_current->State, ); foreach($item_current->Tasks->Task as $current_tasks){ $projects_task[] = array( 'job_tasks' => (string)$current_tasks->Name, ); } } } foreach ($projects_current as $proj_current) { $job_no =$proj_current['job_no']; $job_name =$proj_current['job_name']; $job_status =$proj_current['job_status']; $clr_current = $colors[$job_status]; $project_id = $job_no; $tdata_1 .= "<tr id='current' class='card-body collapse-show'>"; $tdata_1 .= "<td class='th-sm-1 bg-white '><a href='../details/index.php?pid=$job_no' class='text-left ml-1'>$job_no</a></td>"; $tdata_1 .= "<td data-target='#" . $job_no . "' data-toggle='collapse' class='th-sm-2 bg-white text-left ml-1'>$job_name</td>"; $tdata_1 .= "<td class='th-sm-2 " . $clr_current . " text-left ml-1 '>$job_status</td>"; foreach ($projects_task as $proj_tasks){ $job_tasks =$proj_tasks['job_tasks']; $tdata_1 .= "</tr>"; $tdata_1 .= "<tr id='" . $job_no . "' class='card-body collapse'> "; $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>"; $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>"; $tdata_1 .= "<td class='th-sm " . $clr_current . " text-left ml-1'>$job_tasks</td>"; $tdata_1 .= "</tr>"; } } what I get with this code however is; Job no 1 job name job status job 1 task name 1 job 1 task name 2 job 2 task name 1 job 2 task name 2
Job no 2 job name job status job 1 task name 1 job 1 task name 2 job 2 task name 1 job 2 task name 2
I'm sure there is a simple answer to this, I just can't seem to put my finger on it. Can anyone help please? Much appreciated thanks in advance! i have a text file that has spaces between each column. eg 12 12 13 22 34.5 10 13 18 88 32.5 12 33 17 23 22.3 (the actual data is weather data that accumulates a new line every 15 mins and is seen at www.maidenerleghweather.com/clientraw.txt ) each column represents a weather variable. i would like to use php to find the maximum values in each column and minimum values etc etc. how can i get the text data into arrays, so that i can then easily perform some simple stats on it? HOWEVER each line would NOT be an array. the first value in each line makes up array number one, second makes up two etc any ideas for a newbie beginner would be welcomed. Hi wondering if there's a simple way to merge or update the data from one array to another . I've tried merge data but it just adds additional records rather than merging them off one common key.
The array is just simply adding a name to another array the format of both is as follows
Array 1
[0] => stdClass Object ( [name] => [market] => Football [selection] => 7051575 ) ) Array 2 Array ( [0] => stdClass Object ( [name] => Something [market] => [selection] => 7051575 ) ) There are other fields but I'm just trying to merge the name from Array two into Array one using the selection as a common key. Is there an easy command like merge or do I have to loop thru the arrays to add the data. Thanks Now I am stumped this is far any help would be awesome This is what I have gotten so far, this is what it does right now is display the first column of the array as a header for each foreach loop then i've got the data displayed under each header in the loop but what i am trying to understand here is i am trying to get under each header it to have 4 columns or a number of my choosing. Heres an example of what i am trying to do. A airlie beach Andergrove Alexandra Armstrong Beach Alligator Creek B Bucasia Blacks Beach Beaconsfield Bakers Creek Balberra Bloomsbury Breadalbane Ball Bay Belmunda This is an example of how it is being displayed right now A Airlie Beach Andergrove Alexandra Armstrong Beach Alligator Creek B Bucasia Blacks Beach Beaconsfield Bakers Creek Balberra Bloomsbury Breadalbane Ball Bay Belmunda C Cannonvale Calen Crystal Brook Cremorne Chelona Campwin Beach Cape Hillsborough Conway Heres The Code i have so far. Code: [Select] <?php $file = "widget.csv"; @$fp = fopen($file, "r") or die("Could not open file for reading"); $outputArray = array(); while(($shop = fgetcsv($fp, 1000, ",")) !== FALSE) { $outputArray[array_shift($shop)] = array_filter($shop); } print_r($outputArray); echo "\n<br />"; //display echo "<table>"; foreach ($outputArray as $alphabetical=>$value){ echo "<th><b>" . $alphabetical ."</b></th>"; echo "<tr>"; foreach ($value as $key){ echo "<td>" . $key . "</td>"; } echo "</tr>"; } echo "</table>"; echo "\n<br />"; ?> Heres my Csv File which i am using as an array in the code Quote A,Airlie Beach,Andergrove,Alexandra,Armstrong Beach,Alligator Creek,,,,,,,,,,,,,, B,Bucasia,Blacks Beach,Beaconsfield,Bakers Creek,Balberra,Bloomsbury,Breadalbane,Ball Bay,Belmunda,,,,,,,,,, C,Cannonvale,Calen,Crystal Brook,Cremorne,Chelona,Campwin Beach,Cape Hillsborough,Conway,,,,,,,,,,, D,Dows Creek,Dumbleton,Dolphin Heads,,,,,,,,,,,,,,,, E,Eimeo,Eton,Erakala,,,,,,,,,,,,,,,, F,Foulden,Foxdale,Flametree,Farleigh,Freshwater Point,,,,,,,,,,,,,, G,Glen Isla,Glenella,,,,,,,,,,,,,,,,, H,Homebush,Hampden,,,,,,,,,,,,,,,,, I,,,,,,,,,,,,,,,,,,, J,Jubilee Pocket,,,,,,,,,,,,,,,,,, K,Kinchant Dam,Kolijo,Koumala,Kuttabul,,,,,,,,,,,,,,, L,Laguna Quays,,,,,,,,,,,,,,,,,, M,McEwens Beach,Mackay,Mackay Harbour,Mandalay,Marian,Mia Mia,Middlemount,Midge Point,Mirani,Moranbah,Mount Charlton,Mount Jukes,Mount Julian,Mount Marlow,Mount Martin,Mount Ossa,Mount Pelion,Mount Pleasant,Mount Rooper N,Narpi,Nebo,Nindaroo,North Eton,,,,,,,,,,,,,,, O,Oakenden,Ooralea,,,,,,,,,,,,,,,,, P,Palmyra,Paget,Pindi Pindi,Pinevale,Pioneer Valley,Pleystowe,Preston,Proserpine,,,,,,,,,,, Q,,,,,,,,,,,,,,,,,,, R,Racecourse,Richmond,Riordanvale,Rosella,Rural View,,,,,,,,,,,,,, S,St Helens Beach,Sandiford,Sarina,Seaforth,Slade Point,Shoal Point,Shute Harbour,Shutehaven,Strathdickie,Sugarloaf,Sunnyside,,,,,,,, T,Te Kowai,The Leap,,,,,,,,,,,,,,,,, U,,,,,,,,,,,,,,,,,,, V,Victoria Plains,,,,,,,,,,,,,,,,,, W,Wagoora,Walkerston,Woodwark,,,,,,,,,,,,,,,, X,,,,,,,,,,,,,,,,,,, Y,Yalboroo,,,,,,,,,,,,,,,,,, Z,,,,,,,,,,,,,,,,,,, Hi... I tried to use foreach in displaying my table header, but I encountered problem when I tried to display data on the first row , my query only display the last Sum for the last Comp. here is my code: <html> <head> <title>Half Shell</title> <link rel="stylesheet" type="text/css" href="kanban.css" /> <?php error_reporting(E_ALL ^ E_NOTICE); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <body> <form name="param" action="" method="post" onSubmit="return false"> <div id="fieldset_PS"> <?php echo "<table>"; $sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp"; $res_comp = mysql_query($sql, $con); while($row_comp = mysql_fetch_assoc($res_comp)){ $Comp[] = $row_comp['Comp']; } echo "<th> </th>"; foreach($Comp AS $Comp){ echo "<th>$Comp</th>"; } echo "<tr> <td>Total Kg/Compound</td>"; $sql_sec = "SELECT SUM(TotalKg) AS TotalKg FROM sales_order WHERE Comp = '$Comp' ORDER BY Comp"; $res_sec = mysql_query($sql_sec, $con); while($row_sec = mysql_fetch_assoc($res_sec)){ $TotalKg[] = $row_sec['TotalKg']; } foreach($TotalKg AS $TotalKg){ echo "<td>$TotalKg</td> </tr>"; } ?> I also attach the correct output that should be and the result from my code. Thank you Now I am stumped this is far any help would be awesome This is what I have gotten so far, this is what it does right now is display each Header above each bit of data and spans across 5 columns for each foreach loop but what i am trying to understand here is i am trying to get the header then the data then another header and data and so on but only about 4 or number of my choosing grouped header/data but select how many are in each column up to 5 columns. Heres an example of what i am trying to do. A D K N S AlexandraHeadlands DickyBeach KingsBeach Nambour SandstonePoint Aroona Diddillibah KielMountain Ninderry ShellyBeach Doonan KundaPark NoosaHeads SippyDowns B Dulong Kuluin Ningi SunriseBeach Beachmere DeceptionBay Kilcoy NorthArm SunshineBeach BanksiaBeach Noosaville Scarborough Beerburrum E L Beerwah EerwahVale Landsborough O T Bellara Elimbah Tanawha Bellmere Eudlo M P TowenMountain Birtinya Eumundi Maleny Petrie Tewantin Bongaree Mapleton Palmview TwinWaters Bokarina F Marcoola Palmwoods BribieIslandArea Flaxton MarcusBeach Parklands U Buddina ForestGlen MaroochyRiver Parrearra UpperCaboolture Burnside Maroochydore PeregianBeach Buderim G Minyama Pinbarren V Burpengary GlassHouseMountains MoffatBeach PointArkwright Valdora BliBli Mons PelicanWaters H Montville PacificParadise W C Highworth Mooloolaba WeybaDowns CoolumBeach Hunchy Mooloolah Q Warana Caboolture MountainCreek WestWoombye CabooltureSouth I MountCoolum R Woombye Caloundra ImageFlat Morayfield Rosemount Woorim CastawaysBeach Mudjimba Redcliffe WamuranBasin Chevallum J Woodford CoesCreek WoodyPoint Cooroy Wamuran Currimundi Wurtulla X Y YandinaCreek Yandina Z This is an example of how it is being displayed right now A Airlie Beach Andergrove Alexandra Armstrong Beach Alligator Creek B Bucasia Blacks Beach Beaconsfield Bakers Creek Balberra Bloomsbury Breadalbane Ball Bay Belmunda C Cannonvale Calen Crystal Brook Cremorne Chelona Campwin Beach Cape Hillsborough Conway Heres The Code i have so far. Code: [Select] <?php $file = "widget.csv"; @$fp = fopen($file, "r") or die("Could not open file for reading"); $outputArray = array(); $count = 0; while(($shop = fgetcsv($fp, 1000, ",")) !== FALSE) { $outputArray[array_shift($shop)] = array_filter($shop); } echo "<table>"; foreach ($outputArray as $alphabetical=>$value){ echo "<th colspan='4'><b>" . $alphabetical ."</b></th>"; echo "<tr>"; foreach ($value as $key){ $afterkey=preg_replace('/\s+/','-',$key); if ($count == 4){ echo '</tr><tr><td><a href="map.php?mapaddress='.$afterkey.'-qld&mapname='.$key.'">'.$key.'</a></td>'; $count = 0; }else{ echo '<td><a href="map.php?mapaddress='.$afterkey.'-qld&mapname='.$key.'">'.$key.'</a></td>'; } $count++; } echo "</tr>"; $count = 0; } echo "</table>"; echo "\n<br />"; ?> Heres my CSV File: Quote A,Airlie Beach,Andergrove,Alexandra,Armstrong Beach,Alligator Creek,,,,,,,,,,,,,, B,Bucasia,Blacks Beach,Beaconsfield,Bakers Creek,Balberra,Bloomsbury,Breadalbane,Ball Bay,Belmunda,,,,,,,,,, C,Cannonvale,Calen,Crystal Brook,Cremorne,Chelona,Campwin Beach,Cape Hillsborough,Conway,,,,,,,,,,, D,Dows Creek,Dumbleton,Dolphin Heads,,,,,,,,,,,,,,,, E,Eimeo,Eton,Erakala,,,,,,,,,,,,,,,, F,Foulden,Foxdale,Flametree,Farleigh,Freshwater Point,,,,,,,,,,,,,, G,Glen Isla,Glenella,,,,,,,,,,,,,,,,, H,Homebush,Hampden,,,,,,,,,,,,,,,,, I,,,,,,,,,,,,,,,,,,, J,Jubilee Pocket,,,,,,,,,,,,,,,,,, K,Kinchant Dam,Kolijo,Koumala,Kuttabul,,,,,,,,,,,,,,, L,Laguna Quays,,,,,,,,,,,,,,,,,, M,McEwens Beach,Mackay,Mackay Harbour,Mandalay,Marian,Mia Mia,Middlemount,Midge Point,Mirani,Moranbah,Mount Charlton,Mount Jukes,Mount Julian,Mount Marlow,Mount Martin,Mount Ossa,Mount Pelion,Mount Pleasant,Mount Rooper N,Narpi,Nebo,Nindaroo,North Eton,,,,,,,,,,,,,,, O,Oakenden,Ooralea,,,,,,,,,,,,,,,,, P,Palmyra,Paget,Pindi Pindi,Pinevale,Pioneer Valley,Pleystowe,Preston,Proserpine,,,,,,,,,,, Q,,,,,,,,,,,,,,,,,,, R,Racecourse,Richmond,Riordanvale,Rosella,Rural View,,,,,,,,,,,,,, S,St Helens Beach,Sandiford,Sarina,Seaforth,Slade Point,Shoal Point,Shute Harbour,Shutehaven,Strathdickie,Sugarloaf,Sunnyside,,,,,,,, T,Te Kowai,The Leap,,,,,,,,,,,,,,,,, U,,,,,,,,,,,,,,,,,,, V,Victoria Plains,,,,,,,,,,,,,,,,,, W,Wagoora,Walkerston,Woodwark,,,,,,,,,,,,,,,, X,,,,,,,,,,,,,,,,,,, Y,Yalboroo,,,,,,,,,,,,,,,,,, Z,,,,,,,,,,,,,,,,,,, |