I need give some hyperlink to my items category in codeigniter.
$this->data['itemdata'] = array(
array(
'title' => 'Printers / Accessories',
'items' => ['Printers', 'Printer Cartridges', 'Compatible Toners'],
'brands' => ['hp', 'cannon', 'brother', 'toshiba', 'sharp']
),
how can I create it?
Updated
view file
<div class="items">
<ul class="floated">
<?php foreach ($item['items'] as $key => $value): ?>
<li><?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
</div>
You have to do it something like below:-
<li><?php echo $value;?></li>
Note:- I hope through URL you want to send user to some fruitful link. So change controller/function/uri to corresponding values.
Reference:-CodeIgniter - Correct way to link to another page in a view
Note:- if you want to remove link then do like below:-
<li><?php echo $value;?></li>
Related
So here is my code. The problem I am having is that I want the number from HP in my PHP code into my HP HTML code and the same thing with Cylinders. I have figured out the other stuff but when it comes to that part I am stuck
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$cars = array(
array(
"car" => "Ferrari",
"model" => "Testarossa",
"gearbox" => "Manual 5 Shift",
"designer" => "Battista Pininfarina",
"engine" =>
array(
"HP" => 390,
"Cylinders" => 12
),
),
);
?>
<?php foreach($cars as $cars_key => $car_val): ?>
<ul>
<div style="margin-bottom: 10px;">
<li><b>Car:</b> <?php echo $car_val["car"]; ?></li>
<li><b>Model:</b> <?php echo $car_val["model"]; ?></li>
<li><b>Gearbox:</b> <?php echo $car_val["gearbox"]; ?></li>
<li><b>Designer:</b> <?php echo $car_val["designer"]; ?></li>
<li><b>Engine</b></li>
<ul>
<li><b>HP:</b></li>
<li><b>Cylinders:</b></li>
</ul>
</div>
</ul>
<?php endforeach; ?>
I have a few concerns:
You lose the brilliance/utility of an associative array when you hardcode values into your script that you could otherwise just call from the array.
I don't like the look of the mid-list <div>. I can't think of any good reason to break up your unorder list flow with it.
I don't like the floating sub-list either. It logically belongs to Engine and good markup would dictate that the sub-list exist inside of its parent.
Here is what I would suggest considering my above points...
*Some Notes:
I'm not sure how you want to layout multiple lists as your array grows in size.
The echoing is just my personal preference. You can bounce in and out of php if you like.
ucfirst() allows you to avoid hardcoding the keys.
My snippet will make your task clean, DRY, and concise.
Code: (Demo)
$cars = array(
array(
"car" => "Ferrari",
"model" => "Testarossa",
"gearbox" => "Manual 5 Shift",
"designer" => "Battista Pininfarina",
"engine" => array(
"HP" => 390,
"Cylinders" => 12
)
)
);
foreach($cars as $details){
echo "<ul style=\"margin-bottom:10px;\">";
foreach($details as $key=>$item){
echo "<li><b>",ucfirst($key),":</b>";
if(!is_array($item)){
echo " $item</li>";
}else{
echo "<ul>";
foreach($item as $subkey=>$subval){
echo "<li><b>$subkey:</b> $subval</li>";
}
echo "</ul>";
echo "</li>";
}
}
echo "</ul>";
}
Source Code Output:
<ul style="margin-bottom:10px;">
<li><b>Car:</b> Ferrari</li>
<li><b>Model:</b> Testarossa</li>
<li><b>Gearbox:</b> Manual 5 Shift</li>
<li><b>Designer:</b> Battista Pininfarina</li>
<li><b>Engine:</b>
<ul>
<li><b>HP:</b> 390</li>
<li><b>Cylinders:</b> 12</li>
</ul>
</li>
</ul>
Rendered Output: (run my snippet # phptester.net to see this)
From you example, it seems to me that the list is static and consists of two elements, then you need not use forEach at all.
<?php foreach($cars as $cars_key => $car_val): ?>
<ul>
<div style="margin-bottom: 10px;">
<li><b>Car:</b> <?php echo $car_val["car"]; ?></li>
<li><b>Model:</b> <?php echo $car_val["model"]; ?></li>
<li><b>Gearbox:</b> <?php echo $car_val["gearbox"]; ?></li>
<li><b>Designer:</b> <?php echo $car_val["designer"]; ?>
</li>
<li><b>Engine</b></li>
<ul>
<li><b>HP:</b><?php echo $car_val["engine"]["HP"]; ?></li>
<li><b>Cylinders:</b><?php echo $car_val["engine"]["Cylinders"]; ?></li>
</ul>
</div>
</ul>
<?php endforeach; ?>
If you do need to use a nested forEach, here is how you would go about doing that:
foreach($cars as $cars_key => $car_val):
if($cars_key == "engine")
foreach($car_val["engine"] as $engine_key => $engine_val):
echo $engine_key.$engine_val;
endforeach;
endforeach;
i am updating a table's data from a form that is generating data for multiple tables. so when i run the update function its giving me an error: Undefined property: Package->dayplan
the controller code:
function update($id){
$package = Package::find($id);
$package->update_attributes($_POST);
redirect('admin/packages/');
}
the view code:
<div class="container">
<?= form_open('admin/packages/update/'.$package->id) ?>
<div class="row">
<div class="col-md-8 form-data">
<ul>
<li><?= form_input('title', set_value('title', isset($package->title)?$package->title:''))?></li>
<li><?= form_input('type', set_value('type', isset($package->type)?$package->type:''))?></li>
<li><?= $this->ckeditor->editor('description', isset($package->description)?$package->description:'') ?></li>
<li><?= form_input('price', set_value('price', isset($package->price)?$package->price:''))?></li>
<li><?= form_input('location', set_value('location', isset($package->location)?$package->location:''))?></li>
</ul>
<ul>
<?php
foreach($dayplan as $row)
{?>
<li><?= form_input('dayplan[day_no][]', set_value('day_no', isset($row->day_no)?$row->day_no:''))?></li>
<li><?= form_input('dayplan[description][]', set_value('description', isset($row->description)?$row->description:''))?></li>
<?php }?>
</ul>
<ul>
<?php
foreach($image as $row)
{?>
<label><input type="file" name="images[]" value=""><?php echo $row->image_location; ?></label>
<?php }?>
</ul>
<?= form_submit('', $btn, 'class="publish-btn"') ?>
</div>
<?= form_close() ?>
when i click the submit button, it posts all the data of the form to the package table and generates that error of undefined property.
its done we have to pass an array of attribute to specify the attributes for package:a $data array for package's attributes.
updated solution:
function update($id){
$package = Package::find($id);
$data = array(
'title' => $this->input->post('title'),
'type' => $this->input->post('type'),
'description' => $this->input->post('description'),
'price' => $this->input->post('price'),
'location' => $this->input->post('location')
);
$package->update_attributes($data);
redirect('admin/packages/');
}
i have problem that i googled allot to try and figure out, but all solutions are variations on mine.
So i have output like this
<div class="row">
<?php $cat= '';?>
<div class="blank">
#foreach($posts as $post)
#if($cat!= $post->cat_id)
</div><div class="col-sm-3">
<p class="alert alert-info"><strong>{{$post->category_name}}</strong></p>
<?php $cat= $post->cat_id; ?>
#endif
</ul><ul>
<li>
{{$post->title}}
</li>
#endforeach
So basically i wont output that category name is in and then all the posts of that category within that div in ul li, and then close the DIV.
But i managed to make it only with this blank div, and closing ul and div before open, but that gives me invalid html, but its sorted good.
Is there any smart way to get this done?
Thanks
here is a working example. I think the problem was the place where you were opening and closing the foreach.
...
(I replaced the blade statements for plain php for clarity, but you should be able to change it back.)
EDIT:
I was not that far..
<?php $posts = [
['cat_id' => 1, 'cat_name' => 'a name', 'title' => 'title'],
['cat_id' => 1, 'cat_name' => 'a name2', 'title' => 'title2'],
['cat_id' => 2, 'cat_name' => 'another name', 'title' => 'title3'],
['cat_id' => 2, 'cat_name' => 'another name2', 'title' => 'title4'],
]; ?>
<div class="col-sm-3">
<?php $cat= '';?>
<?php foreach($posts as $post): ?>
<?php if($post['cat_id'] != $cat): ?>
<p class="alert alert-info">
<strong><?php echo $post['cat_name'];?></strong>
</p>
<ul>
<?php endif; ?>
<li> <?=$post['cat_name'];?> </li>
<?php if($post['cat_id'] == $cat): ?>
</ul>
<?php endif; ?>
<?php $cat= $post['cat_id']; ?>
<?php endforeach; ?>
</div>
Here I edited my previous answer, a few recommendations tho:
The point is not to just write code that works, but also maintainable and readable. That will help you or whoever has to change some functionality in the future.
Try to be as clear as you can be when asking, and provide an example of your desired output if needed (just like in the comment).
You could probably format the data before passing it to the view, so you can print it easily, for example, transforming the array I used in the above code to something like:
$posts = [
['cat_id' => 1, 'cat_name' => 'a name', 'posts' => [
['title' => 'a title','content' => 'whatever'],
['title' => 'another title','content' => 'whatever2'],
]
];
That way it would be waaay easier to show it, and it'll give you ways to
write clearer code.
Best regards!
So after extensive trying, i got what i wanted. Hope this is correct way of doing it.
<?php
$cat= '';
foreach($posts as $post){
if($cat != $post['cat_id']){
echo $cat!= '' ? '</ul></div>' : '';
echo '<div class="col-sm-4 col-md-3">
<p class="alert alert-info"><strong>'.$post['cat_name'].'</strong></p>
<ul>';
$cat= $post['cat_id'];
}
echo '<li>'.$post['title'].'</li>';
}
echo '</ul>'; //close last opened ul
echo '</div>'; //close last opened div
?>
Please help with this code:
<ul class="activity-projects">
<?php foreach ($ownProjects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->id,
'date'=>$date)
); ?>"><?php echo $userProject->name; ?></a>
</li>
<?php endforeach; ?>
<?php foreach ($projects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->project_id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->project->id,
'date'=>$date)
); ?>"><?php echo $userProject->project->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
How to change it to dropdown list, using CHtml::dropDownList. Thanks for watching!
First, you need to define a key-value array like this:
$options = array();
<?php foreach ($ownProjects as $userProject)
array_push($options, array($userProject->id => $userProject->name));
?>
echo CHtml::dropDownList('seletName', '1', $options);
This will be produced an html <select> tag with "seletcName" name. And also option with value "1" will be selected option. You can use your desired value for first and second parameters.
Also you can use CActiveForm#dropDownList for this purpose.
In your form, use the form dropDownList() function.
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAll(),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
From your example, it looks like OwnProjects is not a model on its own, but a subset of a model. You can customise the query
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAllByAttributes(array('user_id'=> Yii:app()->user->id),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
This solution finally helps me:
<?php $options = array();
foreach ($projects as $userProject) :
$options[$userProject->id] = $userProject->project->name;
endforeach;
echo CHtml::dropDownList('selectName', '1', $options);
?>
Hello I just started working with CI ( codeigniter) and everything went well till now, except my navigation part. I've used the url class for now with the anchor method to create the urls but I also want the current url to have a class="current" for example so that I can style it.
Can someone show me how to do this?
my link is created as follows:
$this->load->helper('url');
$menu_item = array(
'/home' => 'Home',
'/schiphol' => 'Schiphol Service',
'/tarieven' => 'Tarieven en Acties',
'/kwaliteit' => 'Kwaliteit',
//'/news' => 'news'
'/contact' => 'Contact'
);
and in my view
<nav role="navigation" class="mainnav">
<ul>
<?php foreach ($menu_item as $menu => $key): ?>
<li> <?php echo anchor($menu, $key) ?> </li>
<?php endforeach ?>
</ul>
</nav>
but in the anchor method I can give a 3rd method with the class but how can I do this only for the current url?
You can give a 3rd attribute, not a method.
You have to use the uri class to compare the current url with the one in the loop:
http://ellislab.com/codeigniter/user-guide/libraries/uri.html
<?php foreach ($menu_item as $menu => $key): ?>
<li>
<?php echo anchor($menu, $key, $this->uri->segment(1) == $menu ? 'class="active"' : '') ?>
</li>
<?php endforeach ?>