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>';
}
Related
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; ?>
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 am learning php nad html and mssql
Hello i dont know anything wrong can someone explain me?
+(everything configured righ in db i guess)
<?php
include('Database\DBconnect.php');
?><?php
$procs = "{call p_bas_sel_DataTest()}";
$paramss = array();
while($getquestionsfromphp=sqlsrv_fetch_object($results)){ ?>
Now HTML
<label class="questlabel">1.blah blah?</label>
<div class="answers" id="ans1" style="margin-left: 2em">
<button ><label ><span ><?php echo $getquestionsfromphp['D_Value'];?></span></label></button>
</div><?php } ?>
<?php
$sql = "SELECT * FROM Data_Test WHERE lang='M'" ;
$query = sqlsrv_query ($conn,$sql);
$arr = array();
while ($row = sqlsrv_fetch_array($query)){
$array[] = $row;
}
?>
I fixed my error coding like this i put data into array from database then used on some text as index numbers
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'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