Codeigniter: how can I send a foreach statement to the view? - php

Controller:
$categorys = array(
'1234' => array('Car Audio','Car Subwoofers'),
'12' => array('Car Stereos')
)
$category_id = $this->input->get('category_id');
$product_id = $this->input->get('modelNumber');
if (array_key_exists($category_id,$categorys))
{
foreach($categorys[$category_id] as $key => $value)
{
echo $value;
}
}
How can I echo the $value outputted from the foreach statement in my view file?

You can pass the whole array to the view and run the foreach directly in the view, e.g.:
$data['array'] = array(/* etc. */);
$this->load->view('view', $data);
And in the view:
<?php foreach($array as $key => $value): ?>
<p>The key is <?php echo $key; ?><br>
The value is <?php echo $value; ?></p>
<?php endforeach; ?>

Controller
if (array_key_exists($category_id,$categorys))
{
$query['cats'] = $categorys[$category_id];
}
View
foreach($cats as $key => $value)
{
echo $value;
}

Related

i run this code but it donot type the array

$studentArray = array(
array("ahmed2",15,3.5),
array("ahmed1",15,2.4),
array("ahmed3",29,3.9),
array("ahmed4",22,3),
array("ahmed5",23,2.8)
);
foreach($studentArray as $key => $value ){
echo $key. '-'.$value.'<br>';
}
Since you have student detail array in $studentArray, you need an extra foreach to loop through inner array.
Try below code in your structure.
$studentArray = array(
array("ahmed2",15,3.5),
array("ahmed1",15,2.4),
array("ahmed3",29,3.9),
array("ahmed4",22,3),
array("ahmed5",23,2.8)
);
foreach($studentArray as $studentDetail ){
foreach($studentDetail as $key => $value ){
echo $key. '-'.$value.'<br>';
}
}
For more better understanding, have a try with below code.
$studentArray = array(
array("val1"=>"ahmed2","val2"=>15,"val3"=>3.5),
array("val1"=>"ahmed1","val2"=>15,"val3"=>2.4),
array("val1"=>"ahmed3","val2"=>29,"val3"=>3.9),
array("val1"=>"ahmed4","val2"=>22,"val3"=>3),
array("val1"=>"ahmed5","val2"=>23,"val3"=>2.8)
);
foreach($studentArray as $student){
foreach($student as $key => $value) {
echo $key. '-'.$value.'<br>';
}
echo '<br>';
}

PHP read value form multidimensional array

its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO

Whats the best method to create this list with PHP?

I'm new to the world of PHP and looking to see if there is a better way of writing this little code snippet that I have. Basically, it gives variables for some social media links, checks if they are empty, creates an array in a function and I call the array in a UL. I know its simple but I'm just looking if what I have is best or if I can improve on it any.
<?php
$facebook = of_get_option('facebook');
$twitter = of_get_option('twitter');
$youtube = of_get_option('youtube');
$linkedIn = of_get_option('linkedin');
$instagram = of_get_option('instagram');
$socialMediaLinks = array(
);
if (!empty($facebook)){
$socialMediaLinks[facebook] = $facebook;
}
if (!empty($twitter)){
$socialMediaLinks[twitter] = $twitter;
}
if (!empty($youtube)){
$socialMediaLinks[youtube] = $youtube;
}
if (!empty($linkedIn)){
$socialMediaLinks[linkedIn] = $linkedIn;
}
if (!empty($instagram)){
$socialMediaLinks[instagram] = $instagram;
}
function socialMediaList($value, $key){
echo '<li class="'.$key.'">'.$key.'</li>';
}
?>
<?php if (!empty($socialMediaLinks)){ ?>
<ul class="social-media">
<?php array_walk($socialMediaLinks, 'socialMediaList'); ?>
</ul>
<?php } ?>
You could shorten the code by simply doing:
$socialMediaLinks = array_filter(array(
'facebook' => of_get_option('facebook'),
'twitter' => of_get_option('twitter'),
'youtube' => of_get_option('youtube'),
'linkedIn' => of_get_option('linkedIn'),
'instagram' => of_get_option('instagram'),
));
This will automatically remove all empty entries, so you won't need that whole bunch of if-statements afterwards.
Try this:
<?php
$socialMediaItems = array(
'facebook' => of_get_option('facebook'),
'twitter' => of_get_option('twitter'),
'youtube' => of_get_option('youtube'),
'linkedin' => of_get_option('linkedin'),
'instagram' => of_get_option('instagram')
);
$socialMediaLinks = array();
foreach ($socialMediaItems as $key => $item) {
if ($item) {
$socialMediaLinks[$key] = $item;
}
}
function socialMediaList($value, $key){
echo '<li class="'.$key.'">'.$key.'</li>';
}
?>
<?php if (!empty($socialMediaLinks)){ ?>
<ul class="social-media">
<?php array_walk($socialMediaLinks, 'socialMediaList'); ?>
</ul>
<?php } ?>
Using array
<?php
$facebook = of_get_option('facebook');
$twitter = of_get_option('twitter');
$youtube = of_get_option('youtube');
$linkedIn = of_get_option('linkedin');
$instagram = of_get_option('instagram');
$socialMediaLinks = array();
$madeArray = array('facebook' => $facebook,'twitter' => $twitter,'youtube' => $youtube,'linkedin' => $linkedIn,'instagram' => $instagram);
foreach ($madeArray as $key => $value) {
if (!empty($value)) {
$socialMediaLinks[$key] = $value;
}
}
function socialMediaList($value, $key){
echo '<li class="'.$key.'">'.$key.'</li>';
}
?>
<?php if (!empty($socialMediaLinks)){ ?>
<ul class="social-media">
<?php array_walk($socialMediaLinks, 'socialMediaList'); ?>
</ul>
<?php } ?>
Better can be:
<?php
$socialmedia = array('facebook', 'twitter', 'youtube', 'linkedin' 'instagram');
$socialMediaLinks = array();
function GetOptions($name)
{
$value = of_get_option($name);
if (!empty($value))
$socialMediaLinks[$name] = $value;
}
foreach ($socialmedia as $smname)
GetOptions($smname);
function socialMediaList($value, $key){
echo '<li class="'.$key.'">'.$key.'</li>';
}
if (!empty($socialMediaLinks)){
?>
<ul class="social-media">
<?php array_walk($socialMediaLinks, 'socialMediaList'); ?>
</ul>
<?php
}
?>
I'd made something like this:
$social_networks = ['facebook', 'twitter', 'youtube', 'linkedin', 'instagram'];
$media_links = [];
foreach ($social_networks as $network) {
if ($option = of_get_option($network)) {
$media_links[$network] = $option;
}
}
// your code to output...
If you write simple application with short support time - your displaying style its normal. But if it something complex, you should look to MVC pattern.

Adding a value to a PHP associative array

I'm passing an array inside a GET(url) call like this:
&item[][element]=value
Then I do my stuff in PHP:
$item = $_GET['item'];
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value';
The problem I'm facing is that I need to have(echo) a third 'value':
echo '$key $value $thirdValue';
Do I have to change my the URL I'm passing or the foreach? And how do I do that? I Googled but I can't make heads nor tails out of it.
$item = $_GET['item'];
$item_temp=array_values($item);
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value'.$item_temp[2];
}
}
<?php
$item = $_GET['item'];
$r=array();
foreach($item as $rt){
array_push($r,array(key($rt)=> $rt));
}
foreach($r as $rt){
foreach($rt as $rt2){
$k = key($rt2);
echo $k.'__'.$rt2[$k] ;
echo "<br>";
}
}
?>
it's Work .

foreach($_SESSION AS $value){...} -> get array key of session

I fill a session like this:
$_SESSION[$id]=$value;
And I'm reading out the array with this:
foreach($_SESSION AS $value){...}
But how can I read out the $id of the session too? Array key?
Thanks!
foreach($_SESSION as $key => $value){
}
http://br2.php.net/manual/en/control-structures.foreach.php
You need something like the following:
foreach($_SESSION AS $key => $value) {
echo "$key -> $value";
}
foreach ($_SESSION as $key => $value)
foreach ($_SESSION as $key => $value) {
print $key . '<br>';
print $value;
}
foreach ($_SESSION as $key => $value) {
echo $key ;
echo $value;
}
session_start();
$dataArray = [];
//foreach(.......) can you passed your array value
foreach ($posts as $post) {
$myData = [];
//can you change ['name']
$myData["name"] = $post->name;
$myData["email"] = $post->email;
$dataArray[] = $myData;
}
$_SESSION["getAllarrydata"] = $dataArray;
//can you call other page
echo "<pre>";
print_r($_SESSION);
$array = array();
/*$array = array(0 => "Value");*/
$array[0] = "Value";
$_SESSION["array"] = $array;
/*$_SESSION["array"][0]= "Value 2";*/
foreach($_SESSION["array"] as $key => $value){
print $key;
print $value;
}

Categories