please help me, i've problem for get a value of database
in one page i called result() like code below :
<li><i class="fa fa-twitter"></i> Your Bird
<ul>
<?php foreach ($burung->result() as $burung) {
echo "<li>$burung->burung</li>";
}?>
</ul>
</li>
and next code :
<div class="row">
<?php foreach ($burung.result() as $burung) {
echo "
<div class=\"col-md-3\">
<div class=\"panel panel-default\">
<div class=\"panel-body bk-primary text-light\">
<div class=\"stat-panel text-center\">
<div class=\"stat-panel-number h1 \">24</div>
<div class=\"stat-panel-title text-uppercase\">$burung->burung</div>
</div>
</div>
Full Detail <i class=\"fa fa-arrow-right\"></i>
</div>
</div>";
}?>
</div>
but the when i run this page doesn't work with message :
Call to undefined function result()
Change this
<?php foreach ($burung.result() as $burung) {
to this
<?php foreach ($burung->result_array() as $row) {
and inside code $row['burung']
Use/Change result_array() instead of result(). Read this Result Arrays in Codeigniter.com
Related
This question already has answers here:
Find the last element of an array while using a foreach loop in PHP
(35 answers)
Closed 2 years ago.
Sorry for posting like this but I am learning
I want all the results to have a hr or any html based line except the last one
I put a line break but it won't look good
help me
foreach ($newsqry->result() as $n) {
$nr=$n->numrows();
$nlink = array_keys($routesAll, "news/index/" . $n->n_id);
?>
<div class="row">
<div class="col-md-6 imgpost">
<a class="post-imgn" href="<?=site_url($nlink)?>">
<img class=""src="<?=base_url("assets/uploads/news/thmb/$n->img")?>"alt="<?=character_limiter($n->title,'50')?>">
</a>
</div>
<div class="col-md-6 descpost">
<div class="post-category com" >
<?php
$t = $n->tags;
$s = explode(',', $t);
foreach ($s as $a) {
?>
<a class="tags" href="<?=site_url("news/view?tag=".strtolower(str_replace(' ','-',$a)))?>"><?=ucwords($a)?></a>
<?php } ?>
</div>
<h3 class="post-title news">
<?=character_limiter($n->title,'100')?>"
</h3>
<ul class="post-meta">
<li><?=$n->author?></li>
<li><i class="fa fa-clock-o" aria-hidden="true"></i> <?=date("h:i A",strtotime($n->time))?> <?=date("F-d-y",strtotime($n->date))?></li>
</ul>
<p class="descfornews"><?=character_limiter(strip_tags($n->desc),'190')?></p>
</div>
</div>
<?php if($nr<1){echo "<hr>";}else{ echo "<h3>End of Page</h3>";}?>
<?php
}
?>
I think you can do something like this.
it will print before the data printed, but there is $data variable to prevent it to print before the first data.
<?php
$data = 0;
$t = $n->tags;
$s = explode(',', $t);
foreach ($s as $a) {
if($data!=0){echo "<hr>";}
?>
<a class="tags" href="<?=site_url("news/view?tag=".strtolower(str_replace(' ','-',$a)))?>"><?=ucwords($a)?></a>
</div>
<h3 class="post-title news"><?=character_limiter($n->title,'100')?>"</h3>
<ul class="post-meta">
<li>
<?=$n->author?>
</li>
<li>
<i class="fa fa-clock-o" aria-hidden="true"></i>
<?=date("h:i A",strtotime($n->time))?>
<?=date("F-d-y",strtotime($n->date))?></li>
</ul>
<p class="descfornews"><?=character_limiter(strip_tags($n->desc),'190')?></p>
</div>
</div>
<?php
}
echo("<h3>End of Page</h3>");
?>
I'm inner joining two databases together here, when using the separate views individually they work fine, so do the databases, I think the cause of this is the inner join, but putting the query directly into phpmyadmin and replacing the $categoryID with a number works.
Some rows load, such as the images, but joined variables are undefined, What is causing the variables to be undefined?
Model:
function DisplayBySpecificCategory($categoryID)
{
$query = $this->db->query("SELECT * FROM CIUploads
INNER JOIN CIUsers ON CIUploads.UserID = CIUsers.UserID
WHERE CIUploads.UploadCategoryID = $categoryID
ORDER BY CIUploads.UploadTime DESC");
return $query->result();
}
Controller:
function SpecificCategory()
{
$categoryID=$this->uri->segment(3);
$this->load->model('Previews_model');
$this->load->model('Category_model');
$this->load->view('../includes/header.php');
$data['category'] = $this->Category_model->GenerateCategoryHeader($categoryID);
$data['allwork'] = $this->Previews_model->DisplayBySpecificCategory($categoryID);
$this->load->view('headers/Specific_category_header', $data);
$this->load->view('previews/Previews_view', $data);
$this->load->view('../includes/footer.php');
}
Header view:
<?php foreach ($category as $value2){ ?>
<header style="background-color:#<?php echo $value2->CategoryColor ?>">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="page-intro">
<h1 class="inverted text-center"><?php echo $value2->CategoryTitle ?></h1>
<p class="inverted text-center"><?php echo $value2->CategoryDescription ?></p>
</div>
</div>
</div>
</div>
</header>
<?php } ?>
Previews view:
<?php
if($value->UploadCategoryID == 2){
echo '<video muted class="video-responsive fit-centered">';
echo '<source src="'.base_url("/public/uploads/works")."/".$value->UploadedAt.'" type="video/mp4">Your browser does not support the video tag.';
echo '</video>';
}
else if($value->UploadCategoryID == 3){
echo '<iframe class="fit-centered" src="http://'.$value->UploadedAt.'" scrolling="no"></iframe>';
}
else{
echo '<img class="img-responsive fit-centered" src="'.base_url("/public/uploads/works")."/".$value->UploadedAt.'" alt="'.$value->UploadTitle.'">';
} ?>
<div class="overlay">
<p>
<i class="fa fa-eye fa-3x ease-color"></i>
</p>
</div>
</div>
<div class="hovereffect-information no-bounding">
<span class="category-tab pull-right text-center inverted" style="background-color:#<?php echo $value->CategoryColor ?>"><i class="fa <?php echo $value->CategoryIcon ?>"></i></span>
<p class="pull-left no-bounding text-uppercase preview-info"><?php echo $value->UploadTitle ?></p>
<img class="img-responsive small-profile-img pull-right hover-dim fit-centered" src="<?php echo base_url("/public/uploads/profile-images/")."/".$value->ProfileImg ?>" alt="<?php echo $value->Username ?>"/>
<p class="pull-left no-bounding text-uppercase preview-info">
<span class="preview-span"> <i class="fa fa-eye"></i> <?php echo $value->UploadViews ?> </span> |
<span class="preview-span"> <i class="fa fa-heart"></i> <?php echo $value->UploadLikes ?> </span> |
<span class="preview-span"> <i class="fa fa-comments"></i> <?php echo $value->UploadComments ?> </span>
</p>
</div>
</div>
<?php } ?>
Error:
Severity: Notice
Message: Undefined property: stdClass::$CategoryIcon
Filename: previews/Previews_view.php
Line Number: 31
Backtrace:
File:
/web/stud/u1263522/cmw/application/views/previews/Previews_view.php
Line: 31 Function: _error_handler
File:
/web/stud/u1263522/cmw/application/controllers/Views_controller.php
Line: 64 Function: view
File: /web/stud/u1263522/cmw/index.php Line: 292 Function:
require_once
In the question you have mentioned : Undefined property: stdClass::$CategoryID
whereas the error shows : Undefined property: stdClass::$CategoryIcon
If $CategoryIcon is undefined it means that you do not have a column called CategoryIcon in any of your tables.
Can you mention the details of the tables?
It is always a good practice to mention the column names in the query instead of *.
add your loading model before called in Controller , Check name is correct , Make sure that some name in (model = Class)
$this->load->model('model_name'); //
Or in Confige.php
<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav">
<?php
$this->load->model('cms_model');
if (isset($article_data)) {
} else {
$article_data = $this->cms_model->get_end_user_cms_menu(); (this line gives error)
}
foreach ($article_data as $row) { ?>
<li class="dropdown"> <a href='<?php echo site_url('endusers/end_cms_controller/cms_type_selected?type_id=' . $row->type_id) ?>'>
<?php echo $row->type_name; ?>
</a> </li>
<?php } ?>
</ul>
<div class="nav-search-wrap">
<input class="txt-search" type="text" name="search" placeholder="Search">
</div>
</div><!-- /.navbar-collapse -->
This is the code from my view file it givs the error on line number i highlighted. The error is " Fatal error: Call to a member function get_end_user_cms_menu() on a non-object in E:\xampp\htdocs\Santulan\application\views\endusers\header.php on line 158"
About the other problem: The issue was inside your controller. I post the corrected version with explanatory comments:
public function index(){
$str=$this->input->post('search');
$search_id=$this->cms_model->search_tag_id($str);
//echo $id= $search_id; -> you cannot echo $search_id becouse it's an array: $query->result() from search_tag_id() model function.
//$query->result() is allways an array
//FOR DEBUGGING: when you want to see inside $query->result(), use: echo '<pre>'.print_r($query->result(), true).'</pre>';
// instead you use:
$id = $search_id->article_id;
$search_data['article']=$this->cms_model->search_article($id);// function for search article
if($search_data){
$this->load->view('endusers/cms_view', $search_data); // this is ok, if you call for $article and $article_image inside the view
}else{
echo "No result found";
}
}
First, you must call $this->load->model('cms_model'); in your controller, not in your view.
Then, in the controller also, not in your view, you could do:
if (!isset($article_data)) {
$article_data = $this->cms_model->get_end_user_cms_menu();
}
$data['list'] = '';
foreach ($article_data as $row){
$data['list'] .= "<li class=\"dropdown\"> $row->type_name </li>";
}
Then, also in your controller, when you load the view, add $data:
$this->load->view('your_viwe', $data);
And in your view:
<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav">
<?php echo $list;?>
</ul>
<div class="nav-search-wrap">
<input class="txt-search" type="text" name="search" placeholder="Search">
</div>
</div>
And your-re good to go.
I have a controller pages.php:
class Pages extends CI_Controller {
public function index($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$this->lang->load('common/menu.php');
$this->lang->load('pages/'.$page.'.php');
$data = array();
$this->load->helper('slideshow');
$data['slideshow'] = get_image_array();
$this->load->view('templates/common/header', $data);
$this->load->view('modules/slideshow', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/common/footer', $data);
}
}
My slideshow_helper file:
function get_image_array(){
$images = array(img("slideshow/desktop/home1.jpg", "one"),img("slideshow/desktop/home2.jpg", "two"),img("slideshow/desktop/home3.jpg", "three"));
$captions = array("first image", "second image", "third image");
return array_combine($images, $captions);
}
And the slideshow view:
<div class="container">
<div class="row">
<div class="col-xs-12">
<div id="carousel-desktop" class="carousel slide visible-md visible-lg">
<ol class="carousel-indicators">
<li data-target="#carousel-desktop" data-slide-to="0" class="active"></li>
<li data-target="#carousel-desktop" data-slide-to="1"></li>
<li data-target="#carousel-desktop" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<?php $i = 1; ?>
<?php foreach ($image as $imageSrc => $caption ):?>
<?php $item_class = ($i == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<?php echo $image; if($caption != ""){ ?>
<div class="carousel-caption">
<?php echo $caption;?>
</div>
<?php } ?>
</div>
<?php $i++; ?>
<?php endforeach; ?>
</div>
<a class="left carousel-control" href="#carousel-desktop" data-slide="prev">
<span class="icon-chevron-sign-left"></span>
</a>
<a class="right carousel-control" href="#carousel-desktop" data-slide="next">
<span class="icon-chevron-sign-right"></span>
</a>
</div>
<br />
</div>
</div>
</div>
<br />
</div>
Unfortunately this does not work am I missing something? I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: image
Filename: pages/home.php
-AND-
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/home.php
Or is it completely wrong? I should point out that this site will not have access to a database.
When I have the array in the view file it works.
What I am trying to do is have a different model/helper file for each page as each page of this controller will have a slideshow but each with different images. So rather than recreating the view part countless times I want to simply have a new array for each page. I was thinking of using something like:
$this->load->helper('pages/'.$page);
-or-
$this->load->model('pages/'.$page.'.php');
so that each page has its own model/helper filer but obviously as I cannot get the first bit working I have no idea if this second bit will work.
Any help will be very much appreciated
You store images in $data['slideshow']; index, right? So simply use $slideshow in your view:
<?php foreach ($slideshow as $imageSrc => $caption ): ?>
I am creating some jQuery functionality on my website whereas an array of Facebook posts and Tweets is looped through to show on the front page.
I have 5 boxes on my front page which I need 5 to show random elements from this array at the same time, then I using some jQuery cycle functionality to cycle through this array. The only issue is, as I am looping over this array 5 times (with each box) and there are only 20 items in this array it can show repeated data.
To help explain this more here is my code:
<?php
$social_feeds = array_merge($tweets_feed, $facebook_feeds);
$social_feeds_arranged = $tweets_feed_instance->sortArrayItemsByDate($social_feeds);
?>
<div id="social_box_item1" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item2" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item3" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item4" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item5" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#social_box_item1').cycle({
});
jQuery('#social_box_item2').cycle({
});
jQuery('#social_box_item3').cycle({
});
jQuery('#social_box_item4').cycle({
});
jQuery('#social_box_item5').cycle({
});
});
</script>
What I need to happen I think is for the first loop to push the item it is displaying ot the back of the array, so when the second loop accesses the first item it will not show the same array item, the same with the 3rd, 4th and 5th loops. I have attempted to do this using array_push and array_shift but can't seem to get it working.
Does anybody have any ideas or can help point out where I am going wrong?
Those loops are not needed then. You can access those array items one by one, if you meant that. Try this:
$social_feeds = array_merge($tweets_feed, $facebook_feeds);
$social_feeds_arranged = $tweets_feed_instance->sortArrayItemsByDate($social_feeds);
?>
<div id="social_box_item1" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[0]['text']; ?>
</a>
</div>
<div id="social_box_item2" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[1]['text']; ?>
</a>
</div>
<div id="social_box_item3" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[2]['text']; ?>
</a>
</div>
<div id="social_box_item4" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[3]['text']; ?>
</a>
</div>
<div id="social_box_item5" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[4]['text']; ?>
</a>
</div>