i have an array like that, i want to print out it in view file, but it's empty, array is like that
array(
(int) 0 => array(
'ProductsUserNode' => array(
'product_user_node_id' => '155',
'user_node_id' => '53',
'product_id' => '1',
'is_active' => '1',
'expiry' => '0000-00-00',
'created' => '2013-01-10 10:27:22',
'modified' => '2013-01-10 10:27:22',
'created_view' => '10:27 AM, Jan 10,2013',
'modified_view' => '10:27 AM, Jan 10,2013'
),
'UserNode' => array(
'user_node_id' => '53',
'division_id' => '28',
'role_id' => '4',
'user_id' => '56',
'created' => '2013-01-10 10:27:20',
'created_view' => '10:27 AM, Jan 10,2013'
),
'Product' => array(
'product_id' => '1',
'name' => 'Manager',
)
),
i am using this code in view file
foreach ($products as $products)
{
?>
<tr>
<td> <?php $products['ProductsUserNode']['product_id']?> </td>
<td> <?php $products['Product']['name']?> </td>
</tr>
<?php }?>
i have also set the variable in controllerlike that,
$this->set('products',$products);
but it is not working, what's the problem? Thanks in advance
Use the echo keyword to print your variables.
foreach ($products as $product)
{
?>
<tr>
<td> <?php echo $product['ProductsUserNode']['product_id']?> </td>
<td> <?php echo $product['Product']['name']?> </td>
</tr>
<?php }?>
foreach ($products as $products) // wrong
You need to iterate over it properly (basic php!):
<?php foreach ($products as $product) { ?>
<tr>
<td> <?php echo h($product['ProductsUserNode']['product_id']); ?> </td>
<td> <?php echo h($product['Product']['name']); ?> </td>
</tr>
<?php } ?>
Also note the h() to secure the view.
PS: You should "bake" your code. This way you would learn from it how do to it properly.
Related
i found something problem with looping data in multidimension array. i got error. undefined $t in my view.
this my controller :
$dp[] = array(
'id' => $ft->idpr_order,
'nor_order_pro' => $ft->no_order_pro,
'product_name' => $ft->nama_produk,
'sku' => $ft->artikel,
'brand' => $ft->merk,
'discount' => $discft,
'size' => $ukr,
'color' => $wrn,
'qty' => $ft->qty,
'price_before' => $hg_before,
'price_fix' => $ft->harga_fix,
);
}
$exp = explode('|', $resex);
$data_cs = array(
'inv' => $invoice,
'tglOrdercs' => date('d F Y H:i:s'),
'tglExp' => $date_maju,
'nmlkp' => $nmlkpi,
'almtkp' => $address,
'note' => $note_ol,
'kota' => $city,
'prov' => $prov,
'layanan' => $exp[0],
'etd' => $exp[1],
'tarif' => $exp[2],
'noTelp' => $notelp271,
'methode' => $method,
'bnk_option' => $banking_select,
'cabang' => $banking_inf_cab,
'no_rek' => $banking_inf_no,
'an_bnk' => $banking_inf_an,
'subtotal' => $subt,
'kode_pembayaran' => $code_unik,
'total_belanja' => $tot_bel,
'berat_total' => $tot_ber,
'data_order' => $dp
);
$body = $this->load->view('em_info_notification_group/mail_order_for_admin',$data_cs,TRUE);
and this my view
<?php
foreach ($data_order as $data):
if (is_array($data)):
foreach ($data as $value):
if (is_array($value)):
foreach ($value as $t):
?>
<tr>
<td class="yut" align="left" style="border-bottom: 1px solid #ccc;">
<h4 style="margin-top:10px;margin-bottom: 10px;"><b><?php echo $t['product_name'];?></b></h4>
<ul class="list-unstyled" style="font-size: 12px; padding-left: 15px;margin-top: 0;">
<li class="gf">SKU : <?php echo $t['sku'];?></li>
<?php echo $t['color'];?>
<?php echo $t['size'];?>
</ul>
</td>
<td class="yut" style="font-size: 14px;border-bottom: 1px solid #ccc;" align="center">
<?php echo $t['qty'];?> pcs
</td>
<td class="yut" style="border-bottom: 1px solid #ccc;" align="right">
<span style="font-size: 12px;"><?php echo $t['price_before'];?><br><harga>Rp. <?php echo $t['price_fix'];?></harga><br><?php echo $t['discount'];?></span>
</td>
</tr>
<?php
endforeach;
endif;
endforeach;
endif;
endforeach;
?>
in my view i got error. undefined $t in my view. actually the data I want to parse it to the admin template html. I have done various ways but nothing works
Change your looping structure to:
foreach ($data_order as $data):
if (is_array($data)):
foreach ($data as $t):
Regards,
I have two arrays. I want to display the tr tag based on the count of $daterange and inside this, I need to check the date value with the second array date value.
First array :
$daterange = ['01/10/2017','02/10/2017','03/10/2017','04/10/2017','05/10/2017'];
Second array :
$job = [0 => ['id' =>1,'date' => '03/10/2017'],
1 => ['id' =>2,'date' => '12/10/2017'],
2 => ['id' =>3,'date' => '14/10/2017'],
3 => ['id' =>4,'date' => '13/10/2017'],
4 => ['id' =>5,'date' => '03/10/2017'],
5 => ['id' =>6,'date' => '04/10/2017'],
6 => ['id' =>7,'date' => '05/10/2017'],
7 => ['id' =>8,'date' => '01/10/2017']
];
Html code :
<table>
<?php foreach($daterange as $key=>$day)
{
?>
<tr>
<td>
<?php foreach($job as $jdata){
if(($day->format('Y-m-d') == ($jdata->date)) {
?>
<input type="radio" checked class="radio-check" name="date" value="">
<?php
} else {
?>
<input type="radio" class="radio-check" name="date" value="">
<?php
}
?>
</td>
</tr>
<?php
}
?>
</table>
But tr tag is displayed 8 times based on second array count.
How do I display tr 5 times, which is the count of the first array, and compare the date inside with the second array?
You only need one loop for this task and you shouldn't repeat whole lines when you are only modifying a single attribute in the line (as a matter of DRYness).
array_column() will sufficiently prepare the $job data.
This is what I recommend: (Demo)
$daterange = ['01/10/2017','02/10/2017','03/10/2017','04/10/2017','05/10/2017'];
$job = [0 => ['id' =>1,'date' => '03/10/2017'],
1 => ['id' =>2,'date' => '12/10/2017'],
2 => ['id' =>3,'date' => '14/10/2017'],
3 => ['id' =>4,'date' => '13/10/2017'],
4 => ['id' =>5,'date' => '03/10/2017'],
5 => ['id' =>6,'date' => '04/10/2017'],
6 => ['id' =>7,'date' => '05/10/2017'],
7 => ['id' =>8,'date' => '01/10/2017']
];
$job_dates=array_column($job,'date'); // generate 1-dimensional array of dates
echo '<table>';
foreach($daterange as $date){
echo "<tr><td><input type=\"radio\" class=\"radio-check\" name=\"date\" value=\"$date\"",(in_array($date,$job_dates)?' checked':''),'></td></tr>';
}
echo "</table>";
/* or write it over several lines like this:
echo '<table>';
foreach($daterange as $date){
echo '<tr>';
echo '<td>';
echo "<input type=\"radio\" class=\"radio-check\" name=\"date\" value=\"$date\"";
if (in_array($date,$job_dates)){
echo ' checked';
}
echo '>';
echo '</td>';
echo '</tr>';
}
echo "</table>";
*/
Output:
<table>
<tr><td><input type="radio" class="radio-check" name="date" value="01/10/2017" checked></td></tr>
<tr><td><input type="radio" class="radio-check" name="date" value="02/10/2017"></td></tr>
<tr><td><input type="radio" class="radio-check" name="date" value="03/10/2017" checked></td></tr>
<tr><td><input type="radio" class="radio-check" name="date" value="04/10/2017" checked></td></tr>
<tr><td><input type="radio" class="radio-check" name="date" value="05/10/2017" checked></td></tr>
</table>
Here you have my solution, very similar to yours.
I tried it and it works ok.
<?php
$daterange = ['01/10/2017', '02/10/2017', '03/10/2017', '04/10/2017', '05/10/2017'];
$job = [0 => ['id' => 1, 'date' => '03/10/2017'],
1 => ['id' => 2, 'date' => '12/10/2017'],
2 => ['id' => 3, 'date' => '14/10/2017'],
3 => ['id' => 4, 'date' => '13/10/2017'],
4 => ['id' => 5, 'date' => '03/10/2017'],
5 => ['id' => 6, 'date' => '04/10/2017'],
6 => ['id' => 7, 'date' => '05/10/2017'],
7 => ['id' => 8, 'date' => '01/10/2017']
]
?>
<table>
<?php
foreach ($daterange as $day) {
?>
<tr>
<td>
<?php
$i = 0;
$numJobs = count($job);
$dateFound = 0;
while ($i < $numJobs && !$dateFound) {
if ($job[$i]['date'] == $day) {
$dateFound = 1;
}
$i++;
}
if ($dateFound) {
?>
<input type="radio" checked class="radio-check" name="date" value="">
<?php
} else {
?>
<input type="radio" class="radio-check" name="date" value="">
<?php
}
?>
</td>
</tr>
<?php
}
?>
</table>
Inside each element of $daterange I look for that date inside $job array with a while loop. If I found it, it stops searching and displays the checked input. Else, if it goes through all the array and doesn't find that date, it displays the non checked input.
(You can just copy and paste it in your code)
So I created a function searchForDate that would check if the date exist in the second array or not, try the below code and here is the demo:
<?php
$daterange = ['01/10/2017','02/10/2017','03/10/2017','04/10/2017','05/10/2017'];
<table>
<?php foreach($daterange as $key=>$day)
{
?>
<tr>
<td>
<?php foreach($daterange as $key=>$day){
if(searchForId($day)) {
?>
<input type="radio" checked class="radio-check" name="date" value="">
<?php } else {
?>
<input type="radio" class="radio-check" name="date" value="">
<?php } ?>
</td>
</tr>
<?php } ?>
</table>
<?php
function searchForId($day) {
$job = [
0 => ['id' =>1,'date' => '03/10/2017' ],
1 => ['id' =>2,'date' => '12/10/2017'],
2 => ['id' =>3,'date' => '14/10/2017'],
3 => ['id' =>4,'date' => '13/10/2017'],
4 => ['id' =>5,'date' => '03/10/2017'],
5 => ['id' =>6,'date' => '04/10/2017'],
6 => ['id' =>7,'date' => '05/10/2017'],
7 => ['id' =>8,'date' => '01/10/2017']
];
foreach ($job as $key => $val) {
if ($val['date'] === $day) {
return $key;
}
}
return null;
}
?>
$job = [0 => ['id' =>1,'date' => '03/10/2017'/* ' here*/ ],
1 => ['id' =>2,'date' => '12/10/2017'],
2 => ['id' =>3,'date' => '14/10/2017'],
3 => ['id' =>4,'date' => '13/10/2017'],
4 => ['id' =>5,'date' => '03/10/2017'],
5 => ['id' =>6,'date' => '04/10/2017'],
6 => ['id' =>7,'date' => '05/10/2017'],
7 => ['id' =>8,'date' => '01/10/2017']
]
Try this as your for loop.
<table>
<?php
foreach ($job as $key => $jdata) {
if (in_array($jdata['date'], $daterange)) {
?>
<tr>
<td>
<input type="radio" checked class="radio-check" name="date" value="">
</td>
</tr>
<?php
}
}
?>
</table>
I am writing a word press plugin using short code.
I have a array like this:
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
.......................
);
How can I use foreach loop or any ways to return (not echo) a table like this:
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>
<tr>
<td>Show $data[0]['name']</td>
<td>Show $data[0]['birth']</td>
<td>Show $data[0]['number']</td>
</tr>
<tr>
Show all keys and all values of $data same above into each <tr> tags
</tr>
</table>
Thanks a lot!
You can do something like this
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
);
$str="<table><tr>";
$str.="<th>Name</th>";
$str.="<th>Birth</th>";
$str.="<th>Number</th></tr>";
foreach ($data as $key => $value) {
# code...
$str.="<tr>";
$str.="<td>Show $data['name']</td>";
$str.="<td>Show $data['birth']</td>";
$str.="<td>Show $data['number']</td></tr>";
}
return $str;
For me the best way is to put your table inside var and create it row by row, like this :
`
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10)
);
$myTable = "
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>";
foreach($data as $key => $value)
{
$myTable .= "
<td>Show ".$value['name']."</td>
<td>Show ".$value['birth']."</td>
<td>Show ".$value['number']."</td>
</tr>";
}
$myTable.="</table>";
echo $myTable; //You can also return it
`
I am trying to Iterate over an Associative Array which has the following structure
Output from var_export($data)
<?php
Harvest_Project::__set_state(array(
'_root' => 'project',
'_tasks' => array() ,
'_convert' => true,
'_values' => array(
'id' => '10122036',
'client-id' => '4861417',
'name' => 'ABC',
'code' => '',
'active' => 'true',
'billable' => 'true',
'bill-by' => 'Project',
'hourly-rate' => '145.0',
'budget' => '70.0',
'budget-by' => 'project',
'notify-when-over-budget' => 'true',
'over-budget-notification-percentage' => '80.0',
'over-budget-notified-at' => '2016-09-24',
'show-budget-to-all' => 'false',
'created-at' => '2016-03-15T21:38:40Z',
'updated-at' => '2016-05-31T23:19:58Z',
'starts-on' => '',
'ends-on' => '',
'estimate' => '70.0',
'estimate-by' => 'project',
'hint-earliest-record-at' => '2016-03-16',
'hint-latest-record-at' => '2016-08-11',
'notes' => '',
'cost-budget' => '',
'cost-budget-include-expenses' => 'false',
) ,
))
This is the code which I wrote to Iterate over the Array
<?php
$project_id=10122036;
$result=$api->getProject($project_id);
$data = $result->get( "data" );
echo "<table border='1'>
<tr><td>Project Name</td>
<td>Hourly Rate</td>
</tr>";
foreach($data as $key=>$fruit) {
?>
<tr><td><?php echo $fruit->name;?></td>
<td><?php echo $fruit->{'hourly-rate'};?></td></tr>
<?php }
echo "</table>";
?>
This code only creates the columns and for some reason does not generate the entries for each row in the table. Hence the resulting table is an Empty table.
Kindly suggest where I am going wrong.
Update
This code is for querying Harvest using the Harvest API. This is the PHP Wrapper Library http://mdbitz.com/docs/harvest-api/ which contains relevant classes and methods. getProject($project id) is a method to retrieve project details based on the project ID.
Added html table code.
$project_id='10122036';
$result=$api->getProject($project_id);
echo "<table border='1'>
<tr><th>Project Name</th>
<th>Hourly Rate</th></tr>";
if( $result->isSuccess() ) {
$project = $result->data;?>
<tr><td><?php echo $project->get( "name" );?></td>
<td><?php echo $project->hourly-rate;?></td></tr>
<?php }else{?>
<tr><td colspan="2">No data from API</td></tr>
<?php } ?>
</table>
Since you are trying to get a single project using api, Use the below code
$project_id='10122036';
$result=$api->getProject($project_id);
if( $result->isSuccess() ) {
$project = $result->data;
echo $project->get( "name" );
echo $project->hourly-rate;
}else{
echo "No data from API";
}
If you see the out put "No data from API", then there is not data returned from API
I have a multidimensional array like this:-
$data = array (
'SalaryHeadName' =>
array (
0 => 'Basic',
1 => 'PF',
),
'SalaryHeadType' =>
array (
0 => 'CR',
1 => 'DR',
),
'Amount' =>
array (
0 => 6000,
1 => 400,
),
)
how to change it into html table report like by using foreach loop or for loop thank in advance.
This is not general, its specific to your problem.
$data = array('SalaryHeadName'=>array(0=>'Basic',1=>'PF'),'SalaryHeadType'=>array(0=>'CR',1=>'DR'),'Amount'=>array(0=>6000,1=>400));
$array_keys = array_keys($data);
<table border="1">
<thead>
<?php foreach ($array_keys as $key) {?>
<th><?php echo $key ?></th>
<?php } ?>
</thead>
<tbody>
<?php for ($i=0; $i<count($data['SalaryHeadName']);$i++) {?>
<tr>
<td><?php echo $data['SalaryHeadName'][$i] ?></td>
<td><?php echo $data['SalaryHeadType'][$i] ?></td>
<td><?php echo $data['Amount'][$i] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
I hope this is what you want,
<?php
$data = array(
'SalaryHeadName' => array(
0 => 'Basic',
1 => 'PF'
) ,
'SalaryHeadType' => array(
0 => 'CR',
1 => 'DR'
) ,
'Amount' => array(
0 => 6000,
1 => 400
)
);
?>
<table border="1" cellpadding="10">
<?php
foreach($data as $key => $value)
{
echo "<tr><td>$key</td>";
foreach($value as $value1)
{
echo "<td>$value1</td>";
}
echo "</tr>";
}
?>
</table>