I have comments stored in a database with the post id that the comment is on, the comments id, the comments parent id, and when it was created, the content, etc.. Now I've got a query that will select all of the comments for a particular post and loop through each one displaying it. However, how can I 'indent' the comments to show that they are a child of another comment?
My current idea (somewhat pseudo-code) was to do:
get comments
for each comments as comment {
if comment has parent
margin += 16;
style = margin-left: margin
else
margin = 0
<div class="post" style="<?php echo $style; ?>">
...
</div>
}
However, this doesn't seem to be working as some of the replies are not positioned correctly, or sometimes don't even position under the parent comment. Am I doing something wrong here, is there a better way to do this?
Actual Code (it's very ugly):
<?php
$comments = $backend->getCommentsFromPost($post_id);
$level = 0;
$margin = 0;
foreach ($comments as $comment) {
$author = $backend->getUserById($comment['comment_author_id']);
$author_name = $author[0]['user_name'];
$author_id_62 = $backend->to62($author[0]['user_id']);
$when = $backend->getTimePosted($comment['comment_created']);
$comment_karma = 2;
$style = "";
if ($comment['comment_parent'] != NULL) {
$margin = $margin + 60;
$style = "style=\"margin-left: " . $margin . "px;\"";
}
else {
$margin = 0;
}
?>
<div <?php echo $style; ?> class="post">
<div class="row">
<div class="col-md-8">
<div class="comment">
<p class="comment-data">
<?php echo $author_name; ?> | <?php echo $comment_karma; ?> points | <?php echo $when; ?></p>
<?php echo $comment['comment_content']; ?>
<p class="comment-footer">
<a class='cbtn btn-primary' title='upvote' href='#'><span class='fa fa-arrow-up'></span></a>
<a class='cbtn btn-primary' title='downvote' href='#'><span class='fa fa-arrow-down'></span></a>
<?php
if ($backend->isLoggedIn()) {
$user_id = $_SESSION['user_id'];
$comment_id = $backend->to62($comment['comment_id']); ?>
<a class='cbtn btn-primary' title='post reply' href='comment-reply.php?comment=<?php echo $comment_id; ?>'><span class='fa fa-reply'></span></a>
<?php
if ($user_id == $author[0]['user_id'] || $backend->isUserNotPeasant($user_id)) {
?>
<a class='cbtn btn-danger' title='delete comment' href="delete-comment.php?id=<?php echo $comment_id; ?>"><span class='fa fa-trash-o'></span></a>
<?php
}
}
?>
</p>
</div>
</div>
</div>
</div>
<?php } ?>
Cheers
It should be simply:
<style>
.post {margin-left: 0;}
.post div {margin-left: 16px;}
</style>
<div class="post">
POST HERE
<div>
reply
<div>
2nd reply
<div>
4th level
</div>
</div>
</div>
<div>
reply 2
</div>
</div>
<div class="post">
another post
</div>
http://jsfiddle.net/4f2wzwne/
You can prepend the comment with any number of as desired. One per level seems good. Track the level depth of the comment, which you can get from your query, and translate that to number of to prepend with.
First get comments without parent, then loop in them and get child comments. That will fix your problem I think.
Related
I have the following data and would like to display it in different containers in html.
Name Price Difference Signal
CA.PA 15.85 3.5609257364073 MACD
AZN.ST 896 3.4881049471963 MACD
AMGN 258.57 1.6391533819031 SMA 50/200
The containers are winner_1. As of right now the first winner_1 display the last Name from the above table.
How can I get it to say CA.PA in the first winner_1, and AZN.ST in the second winner_1, and AMGN in the last winner_1.
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r){
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
}
?>
<div class="winner_1">
<?php echo $name; ?>
</div>
<div class="winner_1">
<?php echo $name +1; ?>
</div>
<div class="winner_1">
</div>
</div>
</div>
The page can be seen here:
https://signal-invest.com/markets-today/
One option is generated div tags using php:
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'>";
echo "<a href='#'>{$name}</a>";
echo "</div>";
}
?>
</div>
</div>
You should see following logic and try doing this way. Hopefully your problem will be resolved.
<div class = "overview">
<h1>Winners</h1>
<div class = "winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'><a href='#'> $name </a></div>";
}
?>
</div>
</div>
Sorry, I'm just a complete novice in PHP
Background: This code is a loop of three slider as you can see here - http://rashelectrical.com.au/. I need to make the H1 tag only on the first slide and make the other two H2 tag
Question:
How can I make the 2nd and last loop to H2 and H1 only on the first
loop?
Thanks
Here is my code:
https://jsfiddle.net/4jm5vhbf/
<?php while(have_rows('banner_slider')) : the_row();?>
<div class="sl" >
<div class="header-content relative-block align-center text-center flex-container align-middle" style="background:url('<?php the_sub_field('image');?>') no-repeat">
<div class="row">
<div class="columns">
<div class="header-content-inner relative-block animate-children">
<h3><?php the_sub_field('subheading');?></h3>
<h1><?php the_sub_field('heading');?></h1>
<p><?php the_sub_field('description');?></p>
<div class="button-group align-center">
<?php $n = 1; while(have_rows('button_links')) : the_row();
$link_lr = get_sub_field('link');?>
<?php if($n == 2){
$n2 = 'hollow';
}else{
$n2 = '';
}?>
<a class="button <?php echo $n2; ?>" href="<?php echo $link_lr['url']; ?>"><span><?php echo $link_lr['title']; ?></span></a>
<?php $n++; endwhile;?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;?>
Its kinda hard to tell without seeing the full code, maybe something like that?
<!-- Instead of H3 tag here, use P tag with css class and change the font size with css -->
<h3><?php the_sub_field('subheading'); ?></h3>
<!-- count the heading elements -->
<?php $headings = count( get_sub_field( 'heading' ) ); ?>
<!-- Condition here if heading 1 echo H1 else echo H2 -->
<?php echo ($headings == 0) ? '<h1>' . the_sub_field('heading') . '</h1>' : '<h2>' . the_sub_field('heading') . '</h2>' ?>
<p><?php the_sub_field('description'); ?></p>
<!-- Your rest of code -->
Also for SEO and accessibility reasons, your HTML markup should considering the "Semantic" of your Heading tags .. so don't use H3 tag before H1 tag.
I'm developing a simple online portfolio website for my final project. I have a page to show all design stored in database. This is the template of a item,
+---------------------+
+ +
+ +
+ image +
+ +
+ +
+ +
+---------------------+
+ Title +
+ +
+---------------------+
This is the code that i used to load all items from databse.
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$title = $row['title'];
$image = $row['image'];
$designer = $row['designer'];
$views = $row['views'];
$likes = $row['likes'];
$price = $row['price'];
?>
<div class = "col-md-3 col-sm-6"> <div class = "wow fadeInUp animated portfolio-item" data-wow-duration = "500ms" data-wow-delay = "600ms">
<div class = "img-wrapper">
<img src = "<?php echo $image; ?>" class = "img-responsive" alt = "" ></div>
<div class = "portfolio-item-text"><h4><a href = "preview.php?title">
<?php echo $title; ?>
</a>
</h4>
<p id = "portfolio-subtitle">
by <?php echo $designer; ?>
</p>
<div class="row" style="margin-top:10px;">
<div class="col-md-3" style="float:left;">
<h6><?php echo $price; ?></h6>
</div>
<div class="col-md-7" style="float:right;">
<div class="portfolio-icons">
<ul class="list-inline">
<li><i class="fa fa-eye"></i> <?php echo $views; ?></li>
<li><i class="fa fa-thumbs-o-up"></i> <?php echo $likes ?></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
I need to open another page to preview each design. So i used $_SESSION to bring data to my preview page inside the while loop.
$_SESSION['id'] = $row['design_id'];
$_SESSION['title'] = $row['title'];
but it only preview the last item. How can i solve this.
You need to change the link
<?php echo $title; ?>
to something like this:
<?php echo $title; ?>
Then you can get in the preview.php file the id with $_GET['id']. But don't forget to check if it is a number before usage. (is_numeric() or int_val())
I'm making a site with conrete5. It's the first one I might add. I have made myself a couple of custom blocks. Named News, Teammates and References.
Now News and Teammates are not editable anymore. I will paste the News -blocks sourcecode.
----------- FORM.php ---------------------
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); ?>
<?php
$al = Loader::helper('concrete/asset_library');
echo $al->file('optional', 'fID', t('Valitse kuva'), $bf, $args);
?>
<div class="form-group">
<?php echo $form->label('otsikko', t('Otsikko'));?>
<?php echo $form->text('otsikko', $otsikko);?>
</div>
<div class="form-group">
<?php echo $form->label('teksti', t('Teksti'));?>
<?php echo $form->text('teksti', $teksti); ?>
</div>
<div class="form-group">
<?php echo $form->label('korkeus', t('Korkeus'));?>
<?php echo $form->select('korkeus', array("108px"=>t("Pieni"),"299px"=>t("Iso")), $korkeus); ?>
</div>
<div class="form-group">
<?php echo $form->label('koko', t('Leveys'));?>
<?php echo $form->select('koko', array("col-md-3"=>t("Pieni"),"col-md-6"=>t("Iso")), $koko); ?>
</div>
<div class="form-group">
<?php echo $form->label('link', t('Linkki'));?>
<?php echo $form->text('link', $link); ?>
</div>
<div class="form-group">
<?php $psh = Loader::helper('form/page_selector');
echo $psh->selectPage('targetCID', $targetCID); ?>
</div>
----------- view.php ---------------------
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
$c = Page::getCurrentPage();
if($size=="col-md-3"){
$class='col-md-3';
$tag = $class;
}else{
$class="col-md-6";
$tag= $class;
}
if ($c->isEditMode()){
$class="editmode";
$editingStyle="padding: 15px; background: #ccc; color: #444; border: 1px solid #999;";
}
else {
$editingStyle = "";
}
$random = rand();
if($korkeus == "299px"){
$padding = '4px';
}else {
$padding = '5px';
}
$p = Page::getByID($targetCID);
$a = new GlobalArea('Header Navigation');
$blocks = $a->getAreaBlocksArray($c);
foreach ($blocks as $block){
if ($block->getBlockTypeHandle()=="autonav"){
$block->setCustomTemplate('cdrop.php'); // it's templates/cdrop.php -check the select option values when you set custom template manually at edit mode. I think you will need just "my_template"
$bv = new BlockView($block);
$bv->render('view');
}
}
?>
<?php $p = Page::getByID($targetCID); ?>
<a href="index.php">
<div class="pull-left <?= $koko;?>" style="padding:<?= $padding ?>;<?php echo $editingStyle;?>">
<div class="col-lg-12 alapalkki box" style="z-index:2;position:relative;">
<div class="image-big" style="background-color:transparent;text-align:center;position:relative;z-index:1;">
<!-- FiGuRe this shit out......... !-->
<?php
if($fID != 0){
$file = File::getByID($fID);
$filePath = $file->getVersion()->getRelativePath();
}
?>
<?php echo '<img src="' . $filePath . '" style="max-height:' . $korkeus . ';width:100%;"/>'; ?>
</div>
<div class="col-lg-12 " style="position:relative;z-index:255;padding:2px 0 0 15px;">
<div class="htitle">
<h4 style="color:white;"><b><?php echo $otsikko; ?></b></h4>
<p style="color:white;"><?php echo $teksti; ?></p>
</div>
</div>
</div>
</div>
</a>
Why is this not being an editable block? Why doesn't the concrete5 even recognize its existence when it is on the page? It just says at the area that it's empty.
$p = Page::getByID($targetCID);
$a = new GlobalArea('Header Navigation');
$blocks = $a->getAreaBlocksArray($c);
foreach ($blocks as $block){
if ($block->getBlockTypeHandle()=="autonav"){
$block->setCustomTemplate('cdrop.php'); // it's templates/cdrop.php -check the select option values when you set custom template manually at edit mode. I think you will need just "my_template"
$bv = new BlockView($block);
$bv->render('view');
}
}
?>
There's the problem. No idea what so ever what that is doing there..... Removed it. Worked like a charm.
-Kevin
I have a list of content that I would have a paging. Until now I was able to count correctly how content we are, then we have 3 content, and imposed a maximum of 2 applications per page, in my index shows the link to go to the next page.
Unfortunately, however, always displays the same content. For example:
http://i62.tinypic.com/156skjo.png
http://i60.tinypic.com/30xcy1l.png
However, I paste the current code, I hope you can help me out. Thank You
<?php require_once 'app/init.php'; ?>
<?php echo View::make('header')->render() ?>
<div class="row">
<!-- START MAIN GRID -->
<?php
// Create a variable imposed where the number of records
// To display on each page
$x_pag = 2;
// Retrieve the current page number.
// Usually you use a querystring
$pag = isset($_GET['pag']) ? $_GET['pag'] : 1;
// Check if $pag is valued and if numeric
// ... Otherwise I assign a value of 1
if (!$pag || !is_numeric($pag)) $pag = 1;
$quest = DB::table('questions')
->count();
// Using a simple mathematical operation define the total number of pages
$all_pages = ceil($quest / $x_pag);
// Calculation of which record start
$first = ($pag - 1) * $x_pag;
$questions = DB::table('questions')
->orderBy('id', 'desc')
->take($x_pag)
->get();
foreach ($questions as $question):
$user = User::find($question->user_id);
?>
<div class="col-md-4 col-lg-4">
<div class="main-grid">
<div class="profile-inner img-responsive" style="background-image: url('images/<?php echo $question->h_image; ?>');border-radius: 10px 10px 0px 0px;">
<img src="<?php echo $user->avatar; ?>" class="small-thumb" >
</div>
<div class="description">
<h5><strong><?php echo $question->user_name; ?></strong>
<?php if (Auth::check() && Auth::user()->id != $question->user_id): ?>
<?php $contact = Contact::find(Auth::user()->id, $question->user_id); ?>
<?php if (!empty($contact) && !empty($contact->accepted)): ?>
( <?php _e('main.remove_contact') ?> )
<?php elseif (!empty($contact)): ?>
( <a href="javascript:EasyLogin.removeContact(<?php echo $question->user_id ?>)" data-contact-id="<?php echo $question->user_id ?>" ><?php _e('main.cancel_contact') ?></a> )
<?php else: ?>
( <?php _e('main.add_contact') ?> )
<?php endif ?>
<?php endif ?>
</h5>
<h3><?php echo $question->h_title; ?>?</h3>
<hr />
Help +
</div>
</div>
</div>
<?php endforeach; ?>
<!-- MAIN GRID END -->
</div>
<?php
// If the total pages are more than 1 ...
// Mold the link to go back and forth between different pages!
if ($all_pages > 1){
if ($pag > 1){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag - 1) . "\">";
echo "Back</a> ";
}
if ($all_pages > $pag){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag + 1) . "\">";
echo "Next</a>";
}
}
?>
<?php echo View::make('footer')->render() ?>