How To load the array values as Dynamic? - php

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
}
?>

Related

How to change link for each item in foreach loop in PHP?

Totally no experience with PHP and I need to change some little thing by my self, the developer has made a generic html component with a generic link ("href=#"). This should be different for each item in foreach loop. Pretty simple but totally difficult who doesnt know the syntax.
Anyone a quick fix?
<?php foreach ($pricing['price_box'] as $box) :?>
<div class="col-lg-6 mb-4 mb-lg-0">
<div class="p-4 pl-5 pr-5 pb-5">
<h3 class="text-center pt-4 pb-5"><?php echo $box['title'] ?></h3>
<div class="d-md-flex justify-content-md-between mb-5">
<?php echo $box['detail'] ?>
<div class="price-unit align-self-center">
<?php
$priceArray = explode('.', $box['price']);
echo $box['price'] ? $priceArray[0] . ",<span>$priceArray[1]</span>" : ''
?>
<div><?php echo $box['duration'] ?></div>
</div>
</div>
<div class="text-center">
Registreren
</div>
</div>
</div>
<?php endforeach; ?>
Since the anchor tag is inside the foreach loop, you can use the $box variable in the anchor tag.
Also, you can use the php variable inside the html by echo it.
For example, if your link is something like some_php_script.php?id=XXX and you want to replace the xxx with a unique id, you can use it like:
Registreren
assumes that the $box array contains an index called id.
Read more about php echo function.
Read more about php arrays here.
Assuming you need to change the link here :
<div class="text-center">
Registreren
</div>
As it is already in the loop, and you have $box array, you need to find out on which index the link href and text(the one you need to display instead of Registreren ) is available. Suppose you have the link at index link and text is at text, then your code will be like
<div class="text-center">
<?php echo $box['text'];?>
</div>
You can check the array structure using print_r()
Reference
Arrays
foreach

Why my images do not appears in a modal? Codeigniter

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.

how to load view in codeigniter with variable?

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.

Data attribute value from MySQL

I'm new to PHP and MySQL so i'm not quite sure what i am doing wrong here. I am using the jQuery plugin quicksand to create a filterable portfolio. The plugin uses custom data attribute data-tag inside the li item to populate the filter nav.
What I am trying to do is use a foreach loop to populate the contents of the ul. The problem is that the filter nav won't auto populate when I use php to get the value of the data attribute from the gallery_tag column in my mySQL table.
<?php
$pagetitle = "Red Penguin - Our Work";
$navcurrent = "work";
$headTitle = "RECENT WORK";
$headsubTitle = "SOME OF OUR RECENT WORK";
include_once('includes/headersub.php');
include_once('includes/connection.php');
include_once('includes/project.php');
$project = new Project;
$projects = $project->fetch_all();
?>
<div class="row">
<nav id="filter"></nav>
<section id="container">
<ul id="stage" class="three-up">
<?php foreach($projects as $project) { ?>
<li class="gallerylist" data-tag="<?php echo $project['gallery_tag']; ?>">
<a href="project.php?id=<?php echo $project['gallery_id']; ?>">
<img src="<?php echo $project['gallery_thumb']; ?> " alt="<?php echo $project['gallery_proj']; ?>" />
<?php echo $project['gallery_title']; ?>
</a>
</li>
<?php } ?>
</ul>
</section>
The error that comes up in the log is in the jquery line:
tags = elem.data('tags').split(',');
The log comes back with this error: "Uncaught TypeError: Cannot call method 'split' of undefined" for the above line.
I'm not quite sure why this is a conflict that causes the jquery to be unable to read the value of the data-attribute as taken from the gallery_tag column of my table. Any help would be appreciated.
I guess there is a spelling mistake in your html you have data-tag and you are trying to get the elem.data('tags') it should be elem.data('tag')
your elem.data('tags') returns undefined. So split() would not work on it.
Maybe I'm wrong, but I think you need: tags = elem.data('tags').split(' ');

Attribute assign image to each multi option

I have created a multi option attribute so that I show an image for each option but i can not get it to work.
I have used this code from another post on here to get a list of the options to show.
The code I used is:
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
So this now shows a list of the options, one of on top of each other.
As I said I would like an image to be shown for each option.
I thought the best way would be to have divs and assign an image to each div.
I was hoping that I could get the output to be:
<div class="option1"></div>
<div class="option2"></div>
<div class="option3"></div>
<div class="option3"></div>
instead of the output that the code above has:
<ul>
<li>option1</li>
<li>option2</li>
<li>option3</li>
<li>option4</li>
</ul>
Change your code to
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<div class="<?php
$_comma = ",";
$_list = "\"></div><div class=\"";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</div>
<?php endif; ?>

Categories