Yii2: How to display Multiple images in view from path saved in database? - yii2

I have column called images in database which contain image paths.
Query:
$query = new Query;
$todo = (new yii\db\Query())
->select(['images'])
->from('room_types')
->andWhere("id = '$model->id'")
->all();
View :
<?php
foreach ($todo as $row)
{
?>
<?php echo Yii::getAlias('#web').'/'.$row; ?>
<?php
}
?>
Images path saved in db:
uploads/room_img/30.jpg;uploads/room_img/300.jpg;uploads/room_img/11928_569674493052762_732198968_n.jpg;
Tried with explode():
<?php
function room_images() {
$query = mysql_query("SELECT images FROM room_types WHERE id = $model->id");
while($row = mysql_fetch_array($query)) {
$e[] = explode(" ", $row[0]);
foreach($e as $r) {
echo $r;
}
}
}
?>
But nothing is showing

Use img method of Html class - reference
In your view file
use yii\helpers\Html;
// ...
<?php foreach ($todo as $key=>$row): ?>
<!-- html code if you need -->
<?php
foreach (explode(';', $row['images']) as $key_img => $value_img)
{
echo Html::img(Yii::getAlias('#web').'/'.$value_img);
}
?>
<!-- html code if you need -->
<?php endforeach; ?>
// ...

Related

How to display Name in codeigniter

Please help me, I want to display the name of the users from the table according to sender_id and recipient_id, how do the code model and view in my CodeIgniter
Tables schema
Data Table users
Data Table Messages
Model
function get_pesan_view(){
$this->db->from('messages');
$this->db->join('users', 'users.id = messages.sender_id', 'left');
$this->db->join('users', 'users.id = messages.recipient_id', 'left');
$q2 = $this->db->get();
return $q2->result();
}
Contoller
public function view_pesan()
{
$data['get_pesan']=$this->Emp_model->get_pesan_view();
$this->load->view('employee/top');
$this->load->view('employee/nav', $data);
$this->load->view('employee/slidbar', $data);
$this->load->view('employee/pesan', $data);
$this->load->view('employee/bottom');
}
View
<?php foreach ($get_pesan as $pesan) { ?>
<strong> Sender : <?php echo $pesan->name ; ?> </strong>
<strong> Recipient : <?php echo $pesan->name ; ?> </strong>
<a> <?php echo $pesan->body ; ?></a>
<?php } ?>
I want to display the name of the sender and recipient together in view
Controller
public function get_names(){
$this->load->model('model_name'); //If not loaded in the autoload.php file
$names = $this->model_name->all_names();
$this->load->view('view_file_name', compact('names'));
}
The query in the model will be like this
public function all_names(){
$this->db->select('name');
$this->db->from('users');
$this->db->join('messages', 'users.id = messages.sender_id');
return $this->db->get()->result();
}
To join with recipient_id do like this
$this->db->join('messages', 'users.id = messages.recipient_id');
Then use foreach loop to show all names in the view
foreach($names as $row){
echo $row->name;
}
model
$this->db->select('messages.*,u1.{name of field for username} as sender_name,u2.{name of field for username} as recipient_name');
$this->db->from('messages');
$this->db->join('users u1', ' u1.id = messages.sender_id', 'left');
$this->db->join('users u2', ' u2.id = messages.recipient_id', 'left');
$q2 = $this->db->get();
return $q2->result();
View
<?php foreach ($get_pesan as $pesan) { ?>
<strong> Sender : <?php echo $pesan->sender_name; ?> </strong>
<strong> Recipient : <?php echo $pesan->recipient_name; ?> </strong>
<a> <?php echo $pesan->body ; ?></a>
<?php } ?>
i have forgotten you cannot join one tale more than one time you need to use table aliases. it will work for you/ just place the name what ever fild name is in your data base.
it will work or else share me screen short of your table and error pages

Display latest wordpress featured images on static html page

I have a wordpress blog page installed in subfolder of my website(converted from html)and a static html home page.
I would like to display 3 latest posts and its featured images on home page. With code below i can display latest posts text but i dont know how to show featured images of posts. Into index.php of a wordpress custom theme i placed featured photo inside a div:
<div id="blogphoto"><?php the_post_thumbnail(); ?></div>
This is the code on static html index.php page which is pulling out latest post. Can anyone help me to get featured images of these posts too?
<div id="wp-post">
<?php
$args = array('numberposts'=>1);
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<h3>".$recent_post['post_title']."</h3> <br>";
echo "<span>".$recent_post['post_date']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post2">
<?php
$args = array('numberposts'=>1 , 'offset'=>1 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post3">
<?php
$args = array('numberposts'=>1 , 'offset'=>2 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
Please try to this code the_post_thumbnail getting the feature image
<?php
if ( $query->have_posts() ) {
$i = 1;
while($query->have_posts()) {
echo '<div id="wp-post-'.$i.'">';
$query->the_post();
?><h2><?php the_title(); ?></h2>
<?php
if ( has_post_thumbnail() ) { // only print out the thumbnail if it actually has one
echo '<p>post says it has a featured image</p>'; // double checking
the_post_thumbnail('thumbnail');
} else {
echo '<p>this post does not have a featured image</p>';
}
echo '</div>';
$i++;
}
} else {
echo '<p>no posts found</p>';
}
?>

Send variable to next page without overwriting

I want to pass $ids variable retrieved from db from page1 to page2 with session method but the code doesn't work or overwrites the variable.
page1
<?php
$queryString = "WHERE id='$id'";
$sqls = mysql_query("SELECT ids FROM markers $queryString");
$i=0;
while($row=mysql_fetch_array($sqls)) {
$_SESSION['ids'][$i]=$row['ids'];
echo $_SESSION['ids'][$i];
echo 'Modify<br />';
$i++;
}
//echo $_SESSION['ids'];
?>
page2
<?php
session_start();
include_once "scripts/connect_to_mysql.php";
$ids= $_SESSION['ids'].[$i];
//echo $ids;
?>
Thanks a lot for help.
you must do also session_start(); in page1 to work with sesion variables.
your code should work like that:
while($row=mysql_fetch_array($sqls)) {
$arrays[]=$row['ids'];
$_SESSION['ids']= $arrays[];
echo $_SESSION['ids'][0];
echo $_SESSION['ids'][1];
.......
echo 'Modify<br />';
}
page2
<?php
session_start();
include_once "scripts/connect_to_mysql.php";
$ids= $_SESSION['ids'];
echo $ids[0];
echo $ids[1];......
?>

How do I restart my loop with get_next_post()?

I'm looking for a succinct method of making get_next_post()
double back to the beginning once it hits the last post.
Currently, it stops once it hits the final post.
Here are a few lines of code from the codex
for context that are similar to what I'm using:
<?php $next_post = get_next_post();
if (!empty( $next_post )): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo $next_post->post_title; ?>
</a>
<?php endif; ?>
http://codex.wordpress.org/Function_Reference/get_next_post
Thanks in advance for your suggestion.
you can't do it with get_next_post - full stop.
Here's how I've done it...
<?php
/**
* Infinite next and previous post looping in WordPress
*/
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
$prev_title = strip_tags(str_replace('"', '', $previous_post->post_title));
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
// get the first posts ID etc
$args = array('post_type'=>'project', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID;
$first_title = get_the_title( $first_id );
// get the last posts ID etc
$last_post = end($posts);
$last_id = $last_post->ID; // To get ID of last post in custom post type outside of loop
$last_title = get_the_title( $last_id );
// if the current post isn't the first post
if($current_id != $first_id) { ?>
Previous Project:<?php echo $prev_title; ?>
<?php
// if the current post is the first post
} else { ?>
Previous Project
<?php }; ?>
<?php
// if the current post isn't the last post
if($current_id != $last_id) { ?>
<a rel="next" href="<?php echo get_permalink($next_id) ?>" title="<?php $next_title ?>" class="prev-next next"> Next Project <?php echo $next_title; ?></a>
<?php
// if the current post is the last post
} else { ?>
<a rel="next" href="<?php echo get_permalink($first_id) ?>" title="<?php $last_title ?>" class="prev-next next"> Next Project <?php echo $first_title; ?></a>
<?php } ?>
Elements take from here Getting first & last post in custom post type outside of loop

How to get getRequestUri() in layout.phtml

In my zend layout.phtml I am serving my navigation like this:
<?php
$userInfo = new Zend_Session_Namespace('userInfo');
if($userInfo->userType=='admin')
{
echo '<li >home</li>';
echo '<li >Addbooks</li>';
echo '<li class="selected">EditBook</li>';
echo '<li>Adduser</li>';
echo '<li>Logout</li>';
}
?>
in a normal page I can get url this way
$this->view->assign('url',$this->getRequest()->getRequestUri());
$url = $this->getRequest()->getRequestUri();
$b=basename($url);
$this->view->assign('b',$b);
I want this url in my layout.phtm how can I pass this from bootstrap.php to layout.phtml? here my requirement is to add a class to li like this:
<li <?php if($b==EditBook) echo 'class="selected' ?> ">EditBook</li>
Better way to obtain it, is to write a view helper that will assign necessary variables
class Helper_Params extends Zend_Controller_Action_Helper_Abstract {
$view = $this->getActionController()->view;
$request = $this->getRequest();
$view->requestUri = $request->getRequestUri();
}
and init it in your Bootstrap
public function _initHelpers() {
Zend_Controller_Action_HelperBroker::addHelper ( new Helper_Params () );
}