I have a comment and reply system in which I want to nest replies in an indented manner using recursion. I am getting all the comments and their replies in a normal manner without indentation and everything displayed in an order according to id.
I have a column in the comment table type which tells whether it is a comment or reply and another column which stores parentId of the reply.
I have tried something like
<?php displayComments($comment);
function displayComments($comment) {
foreach($comment as $value) { ?>
<ul class="commentList">
<li>
<div class="commentData" id="<?php echo $value['id']; ?>">
<p><?php echo $value['author']; ?><span class="date"><?php echo $value['date']; ?></span></p>
<p><?php echo $value['content']; ?></p>
<p><span class="reply">Reply</span> <i class="fa fa-thumbs-up" aria-hidden="true" tabindex="1"></i> <i class="fa fa-thumbs-down" aria-hidden="true" tabindex="1"></i> </p>
</div>
</li>
</ul>
<?php }
} ?>
where $comment is an array which stores all the comment and replies.
Please help me how can i display the replies in a nested manner if I know which is a reply and the parentId of the reply
Related
I am currently working on a tour website, where the number of destinations is available for the user. A Login user can like any destination which will be added to his favorites destination section. I have done this task successfully. But after liked the destination the like button appears multiple times or equal to the number of likes have the user done. Maybe I am wrong in my logic here is my code. The first loop is fetching all the destination and the second loop fetching the favorites destination.
Note:-Here I am showing only logic code not complete HTML or other PHP code
<?php foreach($listing_data as $list){ ?>
<?php foreach($favorites as $fav){ if($fav['link_id']==$list['id']){?>
<li class="pull-right"> <a class="Theme" id="liked"><i class="fas fa-heart"></i> </a> </li>
<?php } else {?>
<li class="pull-right"> <i class="far fa-heart"></i> </li>
<?php } } }?>
Just add a helper variable
and take out the html in 2th foreach.
$like = false; in each Site
Then in each site you loop $favorites, then walk in $favorites for check if TheSite is liked;
after second foreach you should compare if $like is true for use class "fas" or false for use class "far"
<?php foreach($listing_data as $list){
$like = false;
foreach($favorites as $fav){
if($fav['link_id']==$list['id']){
$like = true;
}
}
if($like){ ?>
<li class="pull-right">
<a class="Theme" id="liked"><i class="fas fa-heart"></i></a>
</li>
<?php } else { ?>
<li class="pull-right">
<i class="far fa-heart"></i>
</li>
<?php
}
}
?>
This is just a refactoring of the code to make it look a bit cleaner.
The links are not included as they are ? and also use id's that will not be unique.
So the code could become something like...
<?php foreach ($listing_data as $list): ?>
<?php
foreach ($favorites as $fav) {
$like = ($fav['link_id'] == $list['id']);
}
?>
<li class="pull-right">
<?php if ($like): ?>
<span>More Sensible Link 1</span>
<?php else: ?>
<span>More Sensible Link 2</span>
<?php endif; ?>
</li>
<?php endforeach; ?>
i am creating a CMS system. if post is not published then it will not render in html otherwise show in html. So if I fetch a associative array from database "cms" then it has some published and unpublished post. so how can i skip the unpublished posts and show only published posts in html.
i have tried if condition
<!-- First Blog Post -->
<h2 style = "color:green;">
<?php echo $post_title ;?></a>
</h2>
<p class="lead">
by <?php echo $post_author ;?></a>
</p>
<p><span class="glyphicon glyphicon-time"></span><?php $post_date ?></p>
<hr>
<img class="img-responsive" src="images/<?php echo $post_image ;?>" alt="">
<hr>
<p><?php echo $post_content ?></p>
<a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
**<?php } ?> **
**<?php } **// <!--loop ends here so that we can fetch 'n' no. of posts and displaying each post like below HTML -->
?>
</div>
if post == published then only show it in html otherwise don't show it.
Check if post is not published at the start of the loop. then use the continue to skip through the rest of the loop.
while(items) {
if(!$post->published) continue;
// this part only runs if post is published
}
i'm trying to store html tag that includes php script.
This is for the navigation menu, i'm trying to display only the menu for a particular user role.
I'm storing it this way
public function edit_module(){
$id = $this->input->post('Module_ID', TRUE);
$update = $this->db->update('ref_modules', array('Module_Name'=>$this->input->post('Module_Name', TRUE),
'Module_Menu'=> htmlspecialchars($this->input->post('Module_Menu')),
'UpdatedBy'=>$this->session->userdata('user_id')), "Module_ID = '$id'");
if($update){
return TRUE;
}
}
For display
<!--Menu-->
<!--================================-->
<div id="mainnav-menu-wrap">
<div class="nano">
<div class="nano-content">
<ul id="mainnav-menu" class="list-group">
<!--Category name-->
<li class="list-header">Navigation</li>
<?php if($this->data['menu']): ?>
<?php foreach($this->data['menu'] as $row): ?>
<?php echo htmlspecialchars_decode($row->Module_Menu); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div> <!-- nano -->
</div><!-- mainnav-menu-wrap -->
<!--================================-->
<!--End menu-->
When i view the source, its fine but when i click the link, special characters are showing.
http://localhost/folderName/%3C?php%20echo%20base_url(%27citizens%27);%20?%3E
Here's a sample value that i am inserting
<li class="<?php echo ($title == 'Citizens' ? 'active-link' : '');?>">
<a href="<?php echo base_url('citizens'); ?>">
<i class="fa fa-user"></i>
<span class="menu-title">
<strong>Citizens</strong>
</span>
</a>
</li>
Any idea guys? Thanks in advance!
Inserting Html into database For Different user role is not the right way. You can use if Conditions To show your Menu for different User roles.
I am getting this Error: operand should contain 1 column(s) from the following code
$select = mysql_query("SELECT (SELECT id_post,id_category,id_user,title,description,image,date_added FROM r_post) as id_post,id_category,id_user,title,description,image,date_added ,(SELECT name FROM r_category) as categoryname")or die(mysql_error());
while ($result = mysql_fetch_assoc($select)) {
<div class="tc-ch">
<?php if(!empty($result['image'])){?>
<div class="tch-img">
<img src="admin/images/<?= $result['image'] ?>" class="img-responsive" alt=""/>
</div>
<a class="blog blue"><?= $result['name'] ?></a>
<?}?>
<h3> <?= $result['title'] ?></h3>
<p><?= $result['description'] ?></p>
<div class="blog-poast-info">
<ul>
<li><i class="glyphicon glyphicon-user"> </i><a class="admin" href="#"><?= $result['id_user'] ?> </a></li>
<li><i class="glyphicon glyphicon-calendar"> </i><?= $result['date_added'] ?></li>
<li><i class="glyphicon glyphicon-comment"> </i><a class="p-blog" href="#"><?= $result['categoryname'] ?></a></li>
</ul>
</div>
</div>
<?php } ?>
Could someone help me to get over this ?
You cannot just select two tables in one query if the columns don't match (in which you could use a union) or you want to join.
Without seeing the structure of your database, I can't say for sure, but it seems to me that you want to join these on id_category like so:
SELECT id_post,
id_category,
name AS categoryname, id_user, title, description, image, date_added FROM r_post rp INNER JOIN r_category rc ON rc.id_category = rp.id_category
If my assumptions are wrong about your database please clarify what exactly you need. Also, for future reference, since this is purely a SQL question, just post your query and information about your database so we don't get lost in all of the other irrelevant code.
Edit for how to fetch results:
You are correct in doing
while ($result = mysql_fetch_assoc($select))
This will iterate over every row. All you then need to do is use
echo $result['col_name'];
in php code. So for example, for a paragraph tag you would use:
<p><?php echo $result['description']; ?></p>
That basically prints the result into the HTML code.
I'm quite new to php, so be gentle.....
I have a basic list view that I have set up to match my html/css styles on a sidebar.....but every few listgroup items there are 'header' items on the list that do not inherit the same styles as other listgroup items.
For example, one list group item would have checkboxes and save icons where as the heading list group item would be the same size but would have bold text and no buttons.
This is what I have. On the section below the main one, I have the HTML values for a header list group item. Is it possible to tell the PHP to detect 'step-heading' as a class and then display the following code for all relevant values?
EDIT: I know that in the if-statement that $step-heading is invalid because I have not declared it as a variable...I simply have no idea what to put there otherwise.
<?php foreach ($tasks as $i => $task) { ?>
<li class="list-group-item" tabindex="-1">
<a class="listgroupwrap" href="#g<?= $i + 1 ?>">
</a>
<span class="step-number-container"> <?=$i + 1 ?>
</span>
<span class="btn-check">
<i class="glyphicon glyphicon-ok"></i>
</span>
<!--<span class="step-checkbox"></span>-->
<span class="step-name"><?php echo $task["name"] ?>
</span>
<span class="step-show-hide">
<span class="btn">
<i class="glyphicon glyphicon-chevron-right">
</i>
</span>
</span>
</li>
<?php } ?>
<?php
if ( $tasks == "step-heading" ) {
?>
<li class="list-group-item step-heading" tabindex="-1">
<span class="step-number-container">2</span>
<a class="listgroupwrap" href="#g<?= $i + 1 ?>">
</a>
<span class="step-name">Pre-meeting email, send below items:</span>
</li>
<?php;
}
?>
You can't have it both ways:
<?php foreach ($tasks as $i => $task) { ?>
^^^^^^---array
if ( $tasks == "step-heading" ) {
^^^^^^---array-as-string
An array in string context is simply the literal word Array.
Perhaps you mean:
if ( $task == "step-heading" ) {
^---no S
instead.