I'm trying to create foreach statement using multidimensional array.
Controller:
function index()
{
$index1 = 0;
$index2 = 0;
$index3 = 0;
$index4 = 0;
$result1 = $this->data->get_test('kdprogram','kdprogram');
foreach($result1 as $row1){
$array_temp[$index1] = $row1;
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $row2){
$array_temp[$index1][$index2] = $row2;
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $row3){
$array_temp[$index1][$index2][$index3] = $row3;
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4){
$array_temp[$index1][$index2][$index3][$index4] = $row4;
$index4++;
}
$index3 ++;
}
$index2 ++;
}
$index1 ++;
}
//print_r($array_temp);
$data['damn'] = $array_temp;
$this->load->view('report/laporan_output', $data);
}
$data contains:
Array
(
[0] => Array
(
[kdprogram] => 06
[0] => Array
(
[kdgiat] => 3400
[0] => Array
(
[kdoutput] => 001
[0] => Array
(
[kdsoutput] => 001
)
[1] => Array
(
[kdsoutput] => 006
)
)
[1] => Array
(
[kdoutput] => 008
[2] => Array
(
[kdsoutput] => 001
)
)
)
)
)
How to echo each array (kdprogram, kdgiat, etc) on view especially with html table?
Am i doing it right?
Thanks
it looks kinda ugly and i would use some sort of recursive function but here is your way
in controller ( i assume the arrays have numeric index otherwise you have to use some sort of counter like you did in your code )
foreach($result1 as $a_counter=>$row1)
{
$array_temp[$a_counter] = array( 'parent'=>$row1 , 'child'=>array());
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $b_counter=> $row2)
{
$array_temp[$a_counter]['child'][$b_counter] = array( 'parent'=>$row2 , 'child'=>array());
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $c_counter=>$row3)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter] = array( 'parent'=>$row3 , 'child'=>array());
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter]['child'][] = $row;
}
}
}
}
in the view
foreach($result as $a )
{
// show a
foreach($a['child'] as $b )
{
// show b
foreach($b['child'] as $c )
{
// show c
foreach($c['child'] as $d )
{
// show d
}
}
}
}
Related
Assume that applicant id was passed from the other form.I have array variable coming from my database, here is the code :
$array_id_applicants = explode(";",stripslashes($applicant_id1));
$applicants_num = count($array_id_applicants);
$arr_app_num = array();
for($x=0;$x<=$applicants_num;$x++)
{
if($array_id_applicants[$x]){
$applicant_id = str_replace("'","",$array_id_applicants[$x]);
$applicants = getdata("select cellphone from personal where applicant_id='".$applicant_id."'");
$replace_array = array("-","(",")","+","_");
array_push($arr_app_num,str_replace($replace_array,"",$applicants[1][cellphone]));
}
}
$applicant_number = implode(";",$arr_app_num);
echo $applicant_number; exit;
Assume that this is the value of array :
$applicant_number = '639152478931 / 631687515455','631235497891'
I want the output to be like this :
$applicant_number = '639152478931','631687515455','631235497891'
See if this is what you are trying to do.:
<?php
$applicant_number[] = '639152478931 / 631687515455';
$applicant_number[] = '631235497891';
$applicant_number[] = '0294765388389 / 52525252525';
$applicant_number[] = '0012324252728';
$new = array();
foreach($applicant_number as $number) {
if(strpos($number,'/') !== false) {
$val = explode("/",str_replace(" ","",$number));
$new = array_merge($new,$val);
}
else
$new[] = $number;
}
print_r($new);
?>
Gives you:
Array
(
[0] => 639152478931
[1] => 631687515455
[2] => 631235497891
[3] => 0294765388389
[4] => 52525252525
[5] => 0012324252728
)
<?php
$applicant_number[] = '639152478931';
$applicant_number[] = '631235497891';
$applicant_number[] = '1111111110294765388389';
$applicant_number[] = '0012324252728';
$new = array();
foreach($applicant_number as $number) {
$count = strlen($number) - 10;
$b = substr($number,$count);
$new[] = "+63".$b;
}
print_r($new);
?>
Gives you :
Array ( [0] => +639152478931 [1] => +631235497891 [2] => +634765388389 [3] => +632324252728 )
Hi here are my query results
transsum
-19121111
-17432222
-19873333
-22404444
-21955555
-19716666
I need to place each one of the results into it's own variable
I have this but I don't think it's right
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
foreach ($row = odbc_fetch_array($arr_results) )
{
$price0 = $row[0];
$price1 = $row[1];
$price2 = $row[2];
$price3 = $row[3];
$price4 = $row[4];
$price5 = $row[5];
}
Updated code
$TD_DB_RESOURCE = open_teradata_resource();
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$rows = array();
$i=0;
while ($myRow = odbc_fetch_array($arr_results) )
{
$rows[$i] = $myRow;
$i++;
}
outputs
Array ( [0] => Array ( [TOTAL] => -19126241 ) [1] => Array ( [TOTAL] => -17439360 ) [2] => Array ( [TOTAL] => -19871999 ) [3] => Array ( [TOTAL] => -22409254 ) [4] => Array ( [TOTAL] => -21950605 ) [5] => Array ( [TOTAL] => -19710526 ) )
If what you want is an array with totals, you can use this:
$prices = [];
while ($myRow = odbc_fetch_array($arr_results)) {
$prices[] = $myRow['TOTAL'];
}
Afterwards, the contents of $prices should be:
[-19126241, -17439360, ...]
You can dynamically create variables with curly braces:
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$count = 0;
foreach ($row = odbc_fetch_array($arr_results)) {
$price{$count} = $row;
$count++;
}
You can do something like:
<?php
$arr_result ...
$index=0;
foreach($row = odbc_fetch_array($arr_results)){
${'price'.$index++} = $row;
}
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$i=0;
$prices = array(); //Make a dynamic array of elements
foreach ($row = odbc_fetch_array($arr_results) )
{
$prices[$i] = $row[$i];
$i++;
}
Then use the array elements anywhere in you calculations like
$c = $price[0]+$price[1];
I have a functon that is passed an array of url's. I am extracting data from each webpage and then assigning each piece of data to an array. Here's my function:
function getitems ($urls) {
$iteminfo = array();
foreach($urls as $link) {
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[][] = $itemtitle;
//$iteminfo[$itemtitle][] = $itemlink;
//$iteminfo[$itemtitle][] = $itemdesc;
}
return $iteminfo;
}
I want the array to look like this:
Array ( [0] => Array ( [0] => title [1] => link [2] => desc ) [1] => Array ( [0] => title [1] => link [2] => desc ) [2] => Array ( [0] => title [1] => link [2] => desc ) )
But I can't wrap my head around how to additional fields to the sub-arrays.
try something like this
function getitems ($urls) {
$iteminfo = array();
$i = 0;
foreach($urls as $link) {
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[$i][] = $itemtitle;
$iteminfo[$i][] = $itemlink;
$iteminfo[$i][] = $itemdesc;
$i++;
}
return $iteminfo;
}
Everything is ok, you just have to assign index to each of your rows.
If i understand you correctly...
$iteminfo[] = array($itemtitle, $itemlink, $itemdesc);
function getitems ($urls) {
$iteminfo = array();
foreach($urls as $link) {
$subInfo = array();
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[] = $subInfo;
}
return $iteminfo;
}
You could easily replace
$iteminfo[][] = $itemtitle;
//$iteminfo[$itemtitle][] = $itemlink;
//$iteminfo[$itemtitle][] = $itemdesc;
with
$iteminfo = array($itemtitle, $itemlink, $itemdesc);
You can do this because the syntax
$array = $element; // where $array = array();
is just another way to add element to an array in PHP and $element can be an array() as well.
How can I create an array like the following in PHP from a database result set using a loop:
Array
(
[T] => Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
)
[P] => Array
(
[0] => Array
(
[id] => 3
[name] => Paper
)
[1] => Array
(
[id] => 4
[name] => Puppy
)
)
)
You will notice that the array keys are a letter, which is taken from the 'name' value in the result set. The loop will be something like this:
while($result = $db->fetch($query) {
$key = $result['name']{0};
// your answer :-)
}
I think something like this should do it:
$sql = 'SELECT id, name FROM table';
$result = mysql_query( $sql);
$answer = array();
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = $row;
}
mysql_free_result( $result);
var_dump( $answer);
OR, to be more specific (if your query is returning more columns than just id and name):
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
$indexArray = array(); // Array from Example
while($result = $db->fetch($query) {
$key = $result['name']{0};
if(!isset($indexArray[$key])) {
$indexArray[$key] = array();
}
array_push($indexArray[$key], $result);
}
$results = array();
while($result = $db->fetch($query)) {
$key = strtoupper($result['name'][0]);
if(!isset($results[$key]))
$results[$key] = array();
$results[$key][] = $result;
}
I think the solution is somewhat easy, of course it is eluding me.
I have two tables and am Joining them on a field with identical values:
example records:
art0001,
art0001,
art0001,
art0002,
art0002,
art0003
What I want to do is to append a number to count every duplicate and echo it out like this:
art0001-1,
art0001-2,
art0001-3
art0002-1,
art0002-2,
art0003-1
I came up with this code, but the output just adds a number, but does not restart when a new duplicate is found.
$query = mysql_query( "SELECT * FROM product_image JOIN my_art ON product_folder = product_code" );
if( !$query ) {
die( mysql_error() );
}
$row = mysql_fetch_array($query);
$i = 0;
while($row = mysql_fetch_array($query)) {
if($row['COUNT(base_folder)'] < 1 && $row['image_type'] == 'B' && $row['view'] == 'FF') {
echo $row['base_folder']."-".$i++;
echo "<br />";
}
}
Can anyone please help me and tell me what I did wrong?
If you want to keep a counter for each 'product' individually, you'll have to check that the product is still the same in the loop, or if this another product. Mind you: the order by clause in the query is needed for this to work.
<?php
$query = mysql_query( "SELECT * FROM product_image JOIN my_art ON product_folder = product_code ORDER BY product_folder" );
if( !$query ) {
die( mysql_error() );
}
$row = mysql_fetch_array($query);
$i = 0;
$last = '';
while($row = mysql_fetch_array($query)) {
if( $row['COUNT(base_folder)'] < 1 && $row['image_type'] == 'B' && $row['view'] == 'FF' ) {
if( $last !== $row['basefolder'] ) {
$i = 0;
}
echo $row['base_folder']."-".$i++;
echo "<br />";
$last = $row['basefolder'];
}
}
You can use an array to keep track of the item count:
<?php
$test_data = array(
'art0001',
'art0001',
'art0001',
'art0002',
'art0002',
'art0003',
);
$item_count = array();
foreach($test_data as $item){
if( isset($item_count[$item]) ){
$item_count[$item]++;
}else{
$item_count[$item] = 1;
}
echo $item . '-' . $item_count[$item] . PHP_EOL;
}
Here is a simple function for taking an array with duplicate values and appending a number to only the duplicate names.
Array
(
[0] => samename
[1] => namesame
[2] => samename
[3] => namename
[4] => namename
[5] => notsame
[6] => samename
)
Array
(
[0] => samename
[1] => namesame
[2] => samename(2)
[3] => namename
[4] => namename(2)
[5] => notsame
[6] => samename(3)
)
function uniqueNames($array){
$alreadyoccured=array();
$newarray=array();
foreach($array as $arr){
if(array_key_exists($arr,$alreadyoccured)){
$alreadyoccured[$arr]=$alreadyoccured[$arr]+1;
$newarray[]=$arr.'('.$alreadyoccured[$arr].')';
}else{
$alreadyoccured[$arr]=1;
$newarray[]=$arr;
}
}
return $newarray;
}