My controller code just generate ten copies of the same data. And I put the data in an array of array.Controller Code Below:
for($i=0;$i < $this->per_page;$i++)
{
$temp['title'] = "test";
$temp['link'] = "localhost/cib/index.php";
$temp['month'] = '4';
$temp['day'] = '21';
$temp['authorlink'] = "localhost/cib/index.php/author";
$temp['author'] = "Charles";
$temp['content'] = "tetestersafaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$results[] = $temp;
}
$data['main_content'] = 'report_list';
$data['posts'] = $results;
$this->load->view('include/templates', $data);
And I show the data generated by controller in the view page report_list, the code of report_list is
<div id="bg_top">
</div>
<div class = "container" id="content">
<div class="row-fluid">
<div class="span9" id="content_left">
<h2>解题报告列表</h2>
<link href="<?php echo base_url('assets/css/style.css') ?>" rel="stylesheet">
<?php if(!empty($posts) && is_array($posts)):?>
<?php foreach($posts as $post):?>
<div class="entry">
<h2 class="entrytitle"><?php echo anchor($post->link, $post->title)?><span></span></h2>
<div class="entrymeta1">
<div class="meta-date">
<span class="meta-month"><?php echo $post->month?></span>
<span class="meta-day"><?php echo $post->day?></span>
</div>
<span class="meta-author"><?php echo anchor($post->authorlink, $post->author)?></span>
<span class="meta-category"></span>
<span class="meta-comment"></span>
<div class="clear"></div>
</div><!-- [entrymeta1] -->
<div class="entrybody">
<p><?php echo $post->content;?><?php echo anchor($post->link, '阅读全文')?></p>
</div>
<div class="entrymeta3">
</div>
</div><!-- [entry] -->
<?php endforeach;?>
<?php endif;?>
</div>
<div class="span3">
<?php echo "hello world2";?>
</div>
</div>
</div>
The question is where am wrong? why is there errors "Trying to get property of non-object"?
I tried to print the data in the controller, it works quite well.
foreach($results as $post)
{
foreach($post as $key=>$value)
{
echo $key.':'.$value;
echo '<br/>';
}
}
You need to print the variables in the view file in array style like this:
<?php echo anchor($post['link'], $post['title'])?>
Still you can always print the array to view the structure:
<?php echo "<pre>";print_r($posts);echo "</pre>";
My assumption is that
You are expecting $posts as array of objects. But in $posts in some loaction there is no object with property month, day etc. So just before the for loop you should look the type of $posts by
var_dump($posts)
This will give you the clear picture about your error
Change all the variables $post->link to array $post['link']
$post->month
$post->day
every fields that are using in template.
Related
I have the following function.php:
function getPublishedPosts() {
// use global $conn object in function
global $conn;
$sql = "SELECT * FROM posts WHERE published=true";
$result = mysqli_query($conn, $sql);
// fetch all posts as an associative array called $posts
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
$final_posts = array();
foreach ($posts as $post) {
$post['topic'] = getPostTopic($post['id']);
array_push($final_posts, $post);
}
return $final_posts;
}
and I am calling it to the other page with this:
<!-- THIS LOOP -->
<?php $posts = **getPublishedPosts();** ?>
<div class="content">
<h2 class="content-title">Recent Articles</h2>
<hr>
<!-- more content still to come here ... -->
<!-- Add this ... -->
<?php $posts = getPublishedPosts(); ?>
<?php foreach ($posts as $post): ?>
<div class="post" style="margin-left: 0px;">
<img src="<?php echo BASE_URL . '/static/images/' . $post['image']; ?>" class="post_image" alt="">
<!-- Added this if statement... -->
<?php if (isset($post['topic']['name'])): ?>
<a
href="<?php echo BASE_URL . 'filtered_posts.php?topic=' . $post['topic']['id'] ?>"
class="btn category">
<?php echo $post['topic']['name'] ?>
</a>
<?php endif ?>
<a href="single_post.php?post-slug=<?php echo $post['slug']; ?>">
<div class="post_info">
<h3><?php echo $post['title'] ?></h3>
<div class="info">
<span><?php echo date("F j, Y ", strtotime($post["created_at"])); ?></span>
<span class="read_more">Read more...</span>
</div>
</div>
</a>
</div>
<?php endforeach ?>
</div>
However, when it is on the server, the page goes blank.
I finally solved it, it was just a server extension and version problem regarding the incompatibility of my function.
Trying to display the custom posts on my archive page within a bootstrap row containing 3 columns then starting a new row, got the code but new to PHP and dont know where to put the content.
<?php
//Columns must be a factor of 12 (1,2,3,4,6,12)
$numOfCols = 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
?>
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
$rowCount++;
if($rowCount % $numOfCols == 0) echo '</div><div class="row">';
}
?>
</div>
<div class="embed-container">
<?php the_field('podcast_embed_link'); ?>
</div>
<h3><?php the_title(); ?></h3>
<p><b><?php echo $date->format( $format_out );?></b></p>
<p><?php the_field('description'); ?></p>
<?php if( get_field('thumbnail') ): ?>
<img src="<?php the_field('thumbnail'); ?>" />
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div><!-- #content -->
Here is the code for the page archive.podcasts.php, where would i add the custom fields within the row loop?
First of all, you don't need to close and open row tag each 3 items. If you leave the code like this:
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
}
?>
</div>
you will get the same effect, but without the separation that a row tag involves. Notice that the line involving "col-md-4" has already changes in order to not create wrong col size usage.
In this part of code:
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
You must get wrong bootstrap class like col-md-41, col-md-412.
Correct you code by this way:
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
So this is the function from model Publicationdata that sends the result array to controller based on the author id that controller provides it->
public function getSpPubData($tId)
{
$this->db->order_by("pub_year", "desc");
$q = $this->db->get_where('rsc_faculty_publications', array('pub_author_id' => $tId));
$error = $q->num_rows() > 0 ? FALSE : TRUE;
if ($error === FALSE)
{
return $q->result();
}
else{
return $error;
}
}
now on controller i am saving the array as-
$data['pubData'] = $this->Publicationdata->getSpPubData("10006);
the rsc_faulty_publications table has a field pub_year.. now on view when I am accessing $pubData on view.. i want to print the publications yearwise. they will be printed in a loop when the current year publications will be at top.. so what I want is to fetch the yearwise publications from $pubData through loop.. the view is like this-
<?php
for($i=0;$i<sizeof($pubData);$i++){ ?>
<div class="panel panel-info">
<div class="panel-heading" data-toggle="collapse" data-parent="#accordion<?php echo $pubData[$i]->pub_year; ?>" href="#year<?php echo $pubData[$i]->pub_year; ?>" title="Click to expand/collapse">
<h4 class="panel-title">Publication Year: <?php echo $pubData[$i]->pub_year; if($pubData[$i]->pub_year==date('Y')){echo " (".sizeof($pubData)." Publications until now)";}?></h4>
</div>
<div id="year<?php echo $pubData[$i]->pub_year; ?>" class="panel-collapse collapse">
<div class="panel-body">
<?php while($pubData[$i]->pub_year==$pubData[$i+1]->pub_year){
?>
<div class="well well-info">
<p><?php echo $pubData[$i]->pub_title; ?></p>
<p><?php echo $pubData[$i]->pub_authors; ?></p>
<p><?php echo $pubData[$i]->pub_publisher; ?></p>
</div>
<?php
}?>
</div>
</div>
</div>
<?php } ?>
First load the view file in controller and pass the data.
$this->load->view('view_file', $data);
Use following code in the view file to retrieve the data.
foreach($pubData->brands as $brand): ?>
after getting the value from model say
$data['value']=$this->model1->getValue();
Now you can pass the value you obtained to view page like this
$this->load->view('viewpagename',$data);
Now in the view page you can easily access it using the array name you passed .ie value. So in the view page do
foreach($value as $val){
print what you want here
}
<?php $i=0;
$n=0;
$brands = array('info','warning','primary','success');
$len=sizeof($pubData);
while($i<$len){ ?>
<?php if($i==0||$i-1!==-1 && $pubData[$i]->pub_year!==$pubData[$i-1]->pub_year){ ?>
<div class="panel panel-<?php echo $brands[$n];?>">
<div class="panel-heading" data-toggle="collapse" data-parent="#accordion1" href="#year<?php echo $pubData[$i]->pub_year; ?>" title="Click to expand/collapse">
<h4 class="panel-title">Publication Year: <?php echo $pubData[$i]->pub_year; if($pubData[$i]->pub_year==date('Y')){echo " (".sizeof($pubData)." Publications until now)";}?></h4>
</div>
<div id="year<?php echo $pubData[$i]->pub_year; ?>" class="panel-collapse collapse">
<div class="panel-body">
<?php
}?>
<div class="well well-<?php echo $brands[$n];?>">
<p><?php echo $pubData[$i]->pub_title; ?></p>
<p><?php echo $pubData[$i]->pub_authors; ?></p>
<p><?php echo $pubData[$i]->pub_publisher; ?></p>
</div>
<?php if($i==$len-1 || $i+1!==$len && $pubData[$i]->pub_year!==$pubData[$i+1]->pub_year){
if($n==3){$n=0;}else{$n++;} ?>
</div>
</div>
</div >
<?php } $i++;
} ?>
This is my code, actually, it actually retrieves data, but, its looking bad.
I use Jquery, (nivoSlider), and Bootstrap loaded in that order.
I have 2 results on DB, it successfully fetched title, description and other information, but it looks bad formatted, or I cannot make it to display the information correctly.
QUESTION: How can I achieve From this-> http://prntscr.com/5imr6e to this-> http://prntscr.com/5imy35 - With its proper thumbnails ?
Any help will be greatly appreciated. Thank you
$sql = "SELECT * FROM homeslider ORDER by id DESC LIMIT 6";
$query = $handler->prepare($sql);
$query->execute();
$row = $query->fetchAll();
return $row;
}
?>
<?php
$data = getContent();
foreach ($data as $row) {
echo '<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
';
$id = $row['id'];
$titulo = $row['titulo'];
$descripcion = $row['descripcion'];
$link = $row['link'];
$imgurl = $row['imgurl'];
$ultimo_update = $row['ultimo_update'];
$captions = '';
}
echo'
<img src="images/slider/'.$imgurl.'" data-thumb="images/slider/'.$imgurl.'" data-transition="fold" title="#htmlcaption_'.$id.'" />'; ?>
<?php $captions = '<div id="htmlcaption_'.$id.'" class="nivo-html-caption">
'.$titulo.'<br/>'.$descripcion.'<span class="nivoButtonSpan">Leer más <i class="glyphicon glyphicon-share-alt"></i></span>';?>
</div> <!-- Close htmlcaption_# -->
<?php echo $captions; ?>
</div> <!-- Close slider -->
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
Well the first thing to do is switch this to use a more readable output syntax - breaking in and out of php instead of echoing all the html as complete strings. Additionally i think you want all the images to be slides but im pretty sure you are actually creating a slider for each image...
<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<?php foreach($data as $row): ?>
<?php // lets use printf since the majority of this is static with just a couple vars ?>
<?php printf(
'<img src="images/slider/%s" data-thumb="images/slider/%s" data-transition="fold" title="#htmlcaption_%s" />',
$row['imgurl'],
$row['imgurl'],
$row['id']
); ?>
<?php endforeach; ?>
</div> <!-- Close slider -->
<?php // output the captions ?>
<?php foreach ($data as $row): ?>
<div id="htmlcaption_<?php echo $row['id'] ?>" class="nivo-html-caption">
<?php echo $row['titulo']; ?>
<br />
<?php echo $row['descripcion'] ?>
<span class="nivoButtonSpan">
Leer más <i class="glyphicon glyphicon-share-alt"></i></span>
</span>
</div>
<?php endforeach; ?>
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
We could further optimize this by building a big string of the captions in the loop so that we do not need loop over the array twice:
<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<?php
// initialize captions as an empty string
$captions = '';
?>
<?php foreach($data as $row): ?>
<?php // lets use printf since the majority of this is static with just a couple vars ?>
<?php printf(
'<img src="images/slider/%s" data-thumb="images/slider/%s" data-transition="fold" title="#htmlcaption_%s" />',
$row['imgurl'],
$row['imgurl'],
$row['id']
); ?>
<?php ob_start(); // start a buffer ?>
<div id="htmlcaption_<?php echo $row['id'] ?>" class="nivo-html-caption">
<?php echo $row['titulo']; ?>
<br />
<?php echo $row['descripcion'] ?>
<span class="nivoButtonSpan">
Leer más <i class="glyphicon glyphicon-share-alt"></i></span>
</span>
</div>
<?php
// get the buffer contents, append them to $captions
// and then clear the buffer and stop buffering
$captions .= ob_get_clean();
?>
<?php endforeach; ?>
</div> <!-- Close slider -->
<?php // output the captions ?>
<?php echo $captions; ?>
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
It seems that you are not closing the nivoslider div:
echo'
<img src="images/slider/'.$imgurl.'" data-thumb="images/slider/'.$imgurl.'" data-transition="fold" title="#htmlcaption_'.$id.'" />'; ?>
<?php $captions = 'CLOSE HERE: </div><div id="htmlcaption_'.$id.'" class="nivo-html-caption">
'.$titulo.'<br/>'.$descripcion.'<span class="nivoButtonSpan">Leer más <i class="glyphicon glyphicon-share-alt"></i></span>';?>
</div> <!-- Close htmlcaption_# -->
I'm attempting to get a list of users who searched a term on Twitter and so far no luck. Here's my code:
$parameters = array('q' => '#puppy', 'count' => 2);
$results = $connection->get('search/tweets', $parameters);
//print_r($result);
foreach ($results as $data) {
?>
<div id="update">
<div id="left"><a href="https://twitter.com/#!/<?php echo $data->user->screen_name; ?>" target="_blank"/><img width="48px" height="48px" src="<?php echo $data->user->profile_image_url; ?>"/></a></div>
<div id="right">
<div class="user"><a href="https://twitter.com/#!/<?php echo $data->user->screen_name; ?>" target="_blank"/><?php echo $data->user->screen_name; ?></a> <?php echo $data->user->name; ?></div>
<div class="detail">
<?php echo $data->text; ?>
</div>
</div>
</div>
<?php
}
and this is what it outputs:
Notice: Undefined property: stdClass::$user in line 150
Trying to get property of non-object in line 150
I know I'm accessing that array in a weird way, I just can't figure out how it needs to be.
For anyone out there who may be needing this, it was just a matter of getting the right data from the array in the right way. Here's the code I used:
$parameters = array('q' => '#puppy', 'count' => 2);
$results = $connection->get('search/tweets', $parameters);
$resultsuser = $results->statuses[0]->user;
$i = 0;
foreach ($results->statuses as $data) {
$user_screen_name = $data->user->screen_name;
$user_profile_image_url = $data->user->profile_image_url;
$user_text = $data->text;
?>
<div id="update">
<div id="left"><a href="https://twitter.com/#!/<?php echo $user_screen_name; ?>" target="_blank"/><img width="48px" height="48px" src="<?php echo $user_profile_image_url; ?>"/></a></div>
<div id="right">
<div class="user"><a href="https://twitter.com/#!/<?php echo $user_screen_name; ?>" target="_blank"/><?php echo $user_screen_name; ?></a> <?php echo $user_screen_name; ?></div>
<div class="detail">
<?php echo $user_text; ?>
<hr/>
</div>
</div>
</div>
<?php
$i++;
}
?>