how to use where conditions in posts model for same specials IDs
and how to receive title of special IDs in view
$items = $this->Posts->find()->where(['AND'=>[['id'=>3],['id'=>4],['id'=>5]]]);
$this->set(compact('items'));
view:
$items->id[0]->title
You can simplify your query in controller
// in controller
$items = $this->Posts->find()->where(['id IN'=>[3,4,5]]);
$this->set(compact('items'));
... and then you can access the titles in template by iterating over generated result
// in template
<?php foreach ($items as $item): ?>
<div><?= h($item->title) ?></div>
<?php endforeach; ?>
Related
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
I've tried GROUP BY with my data below but it only brings back one subtopic. How can I return all the subtopics and organise them under each topic without the topic_name appearing with each subtopic_name.
Edit: Included a screenshot of the page and here is the PHP used:
<ul class="topics-list">
<?php
foreach ($data as $key){
foreach ($key as $item){
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
?>
<div class="the_topic">
<h2 class="topic_change"><?php echo $topic_name; ?></h2>
<ul><li class="subtopic_name"><h3><?php echo $subtopic_name; ?></h3></li></ul>
<hr />
</div>
<?php } ?>
<?php } ?>
</ul>
You could use GROUP_CONCAT() to concatenate all subtopics into one string per topic, and then parse the string in your application code.
SELECT topic_name, GROUP_CONCAT(subtopic_name DELIMITER '§§§') as subtopic_names
FROM questions2
GROUP BY topic_name
But i do not recommend that, because you will get in troubles, if a subtopic contains your delimiter. I would just use your second query and group the result in the application code.
PHP code would look something like:
// group the data
$groupedData = array();
foreach ($data as $item) {
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
$groupedData[$topic_name][] = $subtopic_name;
}
// grouped output
foreach ($groupedData as $topic_name => $subtopic_names) {
echo '<div class="the_topic">';
echo '<h2 class="topic_change">' . $topic_name . '</h2><ul>';
foreach ($subtopic_names as $subtopic_name) {
echo '<li class="subtopic_name"><a href="#" data-toggle="modal" data-target="#lvlModal"><h3>';
echo $subtopic_name;
echo '</h3></a></li>';
}
echo '</ul><hr /></div>';
}
In my form I update more start and end dates from the same model at once. See the simplified form:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $i=>$date): ?>
<?= $form->field($date,"[$i]start"); ?>
<?= $form->field($date,"[$i]end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
In the model I need to control, if the end date is after the start date:
public function rules() {
return [
[['end'], 'compare', 'compareAttribute' => 'start', 'operator' => '>', 'message' => '{attribute} have to be after {compareValue}.'],
];
}
I tried to change selectors similarly as described in: Yii2: Validation in form with two instances of same model, but I was not successful. I suppose I need to change the 'compareAttribute' from 'mymodel-start' to 'mymodel-0-start' in the validation JS:
{yii.validation.compare(value, messages, {"operator":">","type":"string","compareAttribute":"mymodel-start","skipOnEmpty":1,"message":"End have to be after start."});}
So, I look for something like:
$form->field($date,"[$i]end", [
'selectors' => [
'compareAttribute' => 'mymodel-'.$i.'-start'
]
])
Solution
The solution is based on the answer of lucas.
In the model I override the formName() method, so for every date I have a unique form name (based on ID for the existing dates and based on random number for new dates):
use ReflectionClass;
...
public $randomNumber;
public function formName()
{
$this->randomNumber = $this->randomNumber ? $this->randomNumber : rand();
$number = $this->id ? $this->id : '0' . $this->randomNumber;
$reflector = new ReflectionClass($this);
return $reflector->getShortName() . '-' . $number;
}
The form then looks like this:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $date): ?>
<?= $form->field($date,"start"); ?>
<?= $form->field($date,"end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
Override the formName() method in your model class to make it unique. If you don't want to change your model class, create a subclass of it for the purpose of working for this controller action. After doing this, the html ID and name fields will automatically be unique.
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; ?>
// ...
I want to wrap my code in a function (and then put it in functions.php) so that I can call it elsewhere but my code fails as soon as I wrap it in a function.
I think this may be a scope issue, do I have to pass the the post number somehow to the function? If I get rid of the function that's wrapped around the query, the code works fine.
I'm guessing that the code is irrelevant really (although I may be wrong) - it's more to do with the fact that it's a loop and a function.
<?php function getGallery2() { ?>
<!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
<?php query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- 2. echo the test field -->
<?php $link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
<?php $alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php } ?>
<?php getGallery2(); ?>
You would have it something like this I think (not tested):
<?php function getGallery2() { ?>
$global post;
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php } ?>
Then call the function within any loop on any PHP page. Make sense? i.e. don't loop within the function. I don't understand why you don't just use a php include? i.e.
require('get-gallery.php');
Hope that helps :D
$post is not in the functions scope.
You can add global $post; to the top of the function or you can include it as a parameter like this:
function getGallery2($post){
// code
}
echo getGallery2($post)
Code inside a function can only see variables that were created within the same function or in global scope. Meaning the $post object is undefined.
//
On a slightly off topic note, you have lots of HTML comments within PHP. You could easily tidy things p by making it all PHP.
EDIT:
function getGallery2(){
global $post;
// 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page');
while ( have_posts() ) : the_post();
// 2. echo the test field -->
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true);
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true);
echo '<img src="'.$link.'" alt="echo $alt " />';
endwhile;
wp_reset_query();
}
getGallery2();