hi i want to just load the view page using variable not by direct name
here is my code:-
function load_views()
{
$this->load->model('test_model');
$data=$this->test_model->getMenu();
foreach($data as $data_menu) {
echo $data_menu->views;
$this->load->view($data_menu->views);
}
}
output :
test_view
An Error Was Encountered Unable to load the requested file: .php
actually it takes the value from db but it did not call the view file which is present in the view folder.
this is my view page :
<div class="panel-heading">
<h3 class="panel-title">Dashboard</h3>
</div>
<br/>
<div class="row">
<?php
$count = 0;
foreach($m as $data_menu){
if(($count% 4== 0))
{
?>
<div class = "row"></div>
<?php
}
?>
<div class="col-md-3">
<!--<a href="<?/*php echo base_url()*/?>index.php/<?php/* echo $data_menu->views*/?>">-->
<a href="<?php echo base_url()?>index.php/dn_login_controller/load_views">
<img class="img-rounded" src="" height="80" width="80" />
<div class="caption">
<h3><?php echo $data_menu->function_name; ?></h3>
</div>
</a>
</div>
<?php
$count++;
}
?>
</div>
</div>
Now in the view i want load views dynamically but i cant...
please help me.....
If you var_dump($data) you will see that it has an empty value somewhere in the array.
As kumar_v said in a comment, try to check for empty values
foreach($data as $data_menu) {
if(!empty(trim($data_menu->views)))
$this->load->view($data_menu->views);
}
This issue might be because, you cannot just directly load multiple views from single function/controller, when you just load view CI will send it to browser.
There are two other workaround for this :
Pass 2nd parameter NULL & 3rd parameter TRUE while loading your menu views, which will create your html as data instead of sending it to browser. Refer this link : codeigniter: loading multiple views
Create a view file, pass your data to that view & on that view execute for loop & load all your views.
Related
As you See the Above picture is where i want to load the Counts from the array dynamically from the db.In this code iam using it as static how to show the count Dynamically ?
This is the code i am currently using
<h3 class="card-title fw-l"><?php echo $dispcnt[0]->cnt;?> Counts</h3>```
I work in CodeIgniter
Try this solution:
<?php
foreach($dispcnt as $object)
{
?>
<h3 class="card-title fw-l"><?php echo $object->cnt;?> Counts</h3>
<?php
}
?>
I'm trying to do an if condiction in my HTML file in codeigniter that checks the URL and if it contains part of the URL it does one thing if doesnt it does something else
For example:
My URL is
localhost/index.php/cart/galery1
and when I click a photo it applies a filter with this URL
localhost/index.php/cart/galery1/2
the thing is that when I click in a photo after the filter it goes to something like
localhost/index.php/cart/galery1/2/2
There's a way I can do an if condiction checking the URL in my controler or my html ?
My code is something like that right now
HTML
<div id="top-industries" class="gallery">
<div class="container">
<h3 class="tittle">As Nossas Casas</h3>
<div class="gallery-bottom">
{foreach $products as $product}
<div class="view view-ninth">
<a href="galery1/{$product.cat_id}" class="b-link-stripe b-animate-go swipebox" title="Image Title"><img src="{$product.image}" alt="" class="img-responsive" width="330" height="219" target="_blank">
<div class="mask mask-1"></div>
<div class="mask mask-2"></div>
<div class="content">
<h2>CASA 1</h2>
<!--<p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>-->
</div></a>
</div>
{/foreach}
<div class="clearfix"> </div>
</div>
</div>
</div>
Controler
if($cat_id){
$data['catID'] = $cat_id;
$data['products'] = $this->Cart_model->get_casa($cat_id);
}
else{
$data['products'] = $this->Cart_model->get_products();
}
you have to set base_url(); here
application\config\config.php
$config['base_url'] = 'www.yoursite url.com';
and then use this base_url() in link like this
<a href="<?php echo base_url()?>galery1/{$product.cat_id}" class="b-link-stripe b-animate-go swipebox" title="Image Title"><img src="{$product.image}" alt="" class="img-responsive" width="330" height="219" target="_blank">
i think it will solve your problem
To get the segment from URL you should use CI's built-in library URI Class.
$this->uri->segment(n);
// your url - localhost/index.php/cart/galery1/2/3
$this->uri->segment(1); // returns conrtoller ie cart
$this->uri->segment(2); // returns function ie gallery1
$this->uri->segment(3); // returns 1st segment ie 2
$this->uri->segment(4); // returns 2nd segment ie 3
// you can also return default value when a uri segment is not available
$this->uri->segment(5, 0); // returns 0 because there is no 5th segment. Comes handy in condition -- will definitely help you here
// you can also use
uri_string(); // returns complete uri as a string ie cart/galery1/2/3
For more info read here.
Hope it helps you. :)
Create a new .tpl igual as the other one only changing what you want to change and then change the controller to
if($cat_id){
$data['catID'] = $cat_id;
$data['products'] = $this->Cart_model->get_casa($cat_id);
$this->smarty->view('fotos_galeria.tpl', $data);
}
else{
$data['products'] = $this->Cart_model->get_products();
$this->smarty->view('galeria.tpl', $data);
}
I am using codeigniter to develop a web system and I have to add a carousel slide on a modal, but the images does not appears when I'm using a foreach to select an image one by one
I'm running this web system with codeigniter 3.2 and php 7. I'm using a theme from themeforest called 'Pages ADM' which uses owl-carousel plugin.
<div class="container">
<div class="owl-carousel owl-theme">
<?php foreach ($produto['midias'] AS $midia) :?>
<img src="<?=base_url('uploads/midias/').$midia;?>"/>
<?php endforeach; ?>
</div>
</div>
and in my controller
$data['produto'] = $produto;
$data['produto']['midias'] = $this->produto_midia_class->listar('*');
where the function 'listar' was defined in my model
When Im using local images they appears, but when I'm using foreach, these images do not appear.
It would be easier to concatenate the string afterwards. Within "" the variables will be output.
<img src="<?php echo base_url()."uploads/midias/$midia";?>">
You could also do something like this:
<div class="container">
<div class="owl-carousel owl-theme">
<?php
foreach ($produto['midias'] as $midia){
$baseurl = base_url();
echo "<img src='$baseurl"."uploads/midias/$midia'>";
}
?>
</div>
</div>
Also just to note that usually the $midia will need an identifier like the column name if pulled from a database such as
<div class="container">
<div class="owl-carousel owl-theme">
<?php
foreach ($produto['midias'] as $midia){
$baseurl = base_url();
$imageurl = $midia->url;
echo "<img src='$baseurl"."uploads/midias/$imageurl'>";
}
?>
</div>
</div>
Hope this helps.
I have two view one for menu and other for page content.
header
cart
In header cartpopup
is an array key which contains cart data which which will only popup when user clicks cart icon, but here when the page is rendered this cartpopup contents get printed before html is generated.
This code gets printed before html generated on the page:
<li>
<img src="1550847861_Hydrangeas.jpg class="cart-thumb" alt="">
<div class="cart-item-desc">
<h6>Item1</h6>
<p>1x - <span class="price">475</span></p>
</div>
<span class="dropdown-product-remove"><i class="icon-cross"></i></span>
</li>
then html and body contents is generated
header.php
<div class="cart">
<i class="ti-bag"></i><span class="cart_quantity"><?php echo $tot_cart;?></span>
<ul class="cart-list">
<?php echo $cartpopup; ?>
</ul>
</div>
Controller:
public function index() {
$data['output_cart']= $this->show_cart();
$tot_cart=$this->cart->total_items();
$data_menu = $this->category_menu();
$output_cart_popup= $this->load_cart_header_pop();
$this->load->view('header',['menudata'=>$data_menu,'tot_cart'=>$tot_cart,'cartpopup'=>$output_cart_popup]);
$this->load->view('cart',$data);
}
Screenshots:
You just have to use the third param as "true", as in:
$subView = $this->load->view(YOUR_VIEW, YOUR_DATA_ARRAY, true);
$params = array(
YOUR_DATA_ARRAY,
'subView' => $subView
);
$this->load->view(YOUR_VIEW, YOUR_DATA, true);
That way you will have your view preloaded in memory only, then you just have to pass the view in the params to the main view and finally echo it inside your main view, as in:
echo $subView;
I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>