Comments on the page wont show up - php

Hello i have a problem i cant see the comments on the page.
I dont get any error on the page so i am stuck on this moment
can somebody help me??
This is the php code:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
and the rest of the page is:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
Hope somebody see the problem.

try like this for printing comments..
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()){ //opening while block
?>
<div>
<h5><?php echo $row->name;?></h5>
<blockquote><?php echo $row->comment;?></blockquote>
<?php }// ending while block ?>
</div>
</div>

<div id="comments">
<?PHP
$query = $db->query("SELECT name, comment FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()) {
?>
<div>
<h5><?= $row->name ?></h5>
<blockquote><?= $row->comment ?></blockquote>
</div>
<?PHP } ?>
</div>
If that does not work, i would suggest dumping out the SQL query and making sure it is working.

Related

Bootstrap 4 Image product didn't line up properly

I'm tryin to make a simple online shopping page for my PHP training.
But, I'm kinda stuck in the HTML part where using bootstrap didn't make
something more simpler.
The result of the code makes my product line up vertically, and what I want to with it is to make it line up horizontally.
Code :
<div class="container">
<?php
$connect = mysqli_connect('localhost', 'root', '', 'cart');
$query = 'SELECT * FROM products ORDER by id ASC';
$result = mysqli_query($connect, $query);
if ($result):
if (mysqli_num_rows($result)>0):
while ($product = mysqli_fetch_assoc($result)):
//print_r($product);
?>
<div class="col-sm-4">
<form class="" action="welcome.php?action=add&id=<?php echo $product['id'] ?>" method="post">
<div class="products">
<img src="<?php echo $product['image']; ?>" class="img-fluid text-center" />
<h4 class="text-info"><?php echo $product['name']; ?></h4>
<h4>$ <?php echo $product['price']; ?></h4>
<input type="text" name="quantity" class="form-control" value="1" />
<input type="hidden" name="name" value="<?php echo $product['name'] ?>" />
<input type="hidden" name="price" value="<?php echo $product['price']; ?>" />
<button type="submit" name="add_to_cart" class="btn btn-outline-primary">Add to Cart</button>
</div>
</form>
</div>
<?php
endwhile;
endif;
endif;
?>
For bootstrap grid system to work , the tags must be in following order:
<div class="container">
<div class="row">
<div class="col-md-6">
One of two columns
</div>
<div class="col-md-6">
One of two columns
</div>
</div>
</div>

I have two tables,from that am fetching data with left join

post id is the common field in both the tables.If I want to select the comments under same post_id,only 1 time the post should display and all its comments under it..
this is my code....
home.php
<?php
$sql="SELECT family_news.post_id,family_news.username,family_news.post,
family_news.fileToUpload,family_news.description,
family_comments.post_id,family_comments.comment,
family_comments.commenter
FROM family_news
LEFT JOIN family_comments ON family_news.post_id = family_comments.post_id
ORDER BY family_news.post_id DESC";
if($result = mysqli_query($con, $sql))
{
while($row = mysqli_fetch_array($result))
{
?>
<div class="form-group">
<b style="font-size:30px;color:brown;">
<?php
echo $row['username'];
?>
</b>
<br>
<?php
if($row['fileToUpload'] != NULL)
{
?>
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<br>
<img class="img-responsive" src="../admin_panel/uploads/<?php echo $row['fileToUpload']; ?>" alt="Image Loading" height='80' width='120'>
<div class="form-group" style="font-size:15px;color:purple;">
<?php
echo $row['description'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</div>
</form>
<?php
}
if($row['post'] != NULL)
{
?>
<br>
<div class="form-group" style="font-size:15px;color:purple;">
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<?php
echo $row['post'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</form>
<?php
}
?>
</div>
<?php
}
}
?>
</div>
</section>
post_comment.php
<section class="col-lg-6 connectedSortable">
<?php
$post_id=$_POST['post_id'];
$comment=$_POST['comment'];
$commenter=$_POST['commenter'];
if($comment != NULL) {
$sql="INSERT INTO family_comments
(post_id, comment, commenter)
VALUES ('$post_id', '$comment', '$commenter')";
if($con ->query($sql) == TRUE)
{
echo 'Comment saved Successfully...';
}
} else {
echo 'Please Write Something...';
}
?>
</section>

PHP : Login in to the admin page

I am trying to login into the admin and lecturer page in the following code.But it is not working properly.When i login after entering loginid and password, click on submit , no error occured.
admin.php
<?php
session_start();
if(isset($_SESSION["userid"]))
{
if($_SESSION["type"]=="admin")
{
header("Location: dashboard.php");
}
else
{
header("Location: lectureaccount.php");
}
}
include("header.php");
include("conection.php");
if(isset($_POST["uid"]) && isset($_POST["pwd"]) )
{
// echo "sdfsd". $_POST[uid];
$result = mysql_query("SELECT * FROM administrator WHERE adminid='$_POST[uid]'");
while($row = mysql_fetch_array($result))
{
$pwdmd5 = $row["password"];
}
if(md5($_POST["pwd"])=='$pwdmd5')
{
$_SESSION["userid"] = $_POST["uid"];
$_SESSION["type"]="admin";
header("Location: dashboard.php");
}
else
{
$log = "Login failed.. Please try again..";
}
}
if(isset($_POST["luid"]) && isset($_POST["lpwd"]))
{
$result = mysql_query("SELECT * FROM lectures WHERE lecid='$_POST[luid]'");
while($row = mysql_fetch_array($result))
{
$pwdm= $row["password"];
$_SESSION["lecname"] = $row["lecname"];
$_SESSION["coid"] = $row["courseid"];
}
//echo"pwd". md5($_POST["lpwd"]);
if(md5($_POST["lpwd"])==$pwdm)
{
//echo $_POST["lpwd"];
$_SESSION["userid"] = $_POST["luid"];
$_SESSION["type"]=="lecturer";
header("Location: lectureaccount.php");
}
else
{
$log12 = "Login failed.. Please try again..";
}
}
?>
<section id="page">
<header id="pageheader" class="normalheader">
<h2 class="sitedescription">
</h2>
</header>
<section id="contents">
<article class="post">
<header class="postheader">
<h2><u>Admin Login</u></h2>
<?php $log = isset($_POST['log']) ?>
<h2><?php echo $log;?></h2>
</header>
<section class="entry">
<form action="admin.php" method="post" class="form">
<p class="textfield">
<label for="author">
<small>Admin Login ID (required)</small>
</label>
<input name="uid" id="uid" value="" size="22" tabindex="1" type="text">
</p>
<p class="textfield">
<label for="email">
<small>Password (required)</small>
</label>
<input name="pwd" id="pwd" value="" size="22" tabindex="2" type="password">
</p>
<p>
<input name="submit" id="submit" tabindex="5" type="image" src="images/submit.png">
<input name="comment_post_ID" value="1" type="hidden">
</p>
<div class="clear"></div>
</form>
<form action="admin.php" method="post" class="form">
<div class="clear">
<hr />
<header class="postheader">
<h2><u>Lectures Login</u></h2>
<?php $log12 = isset($_POST['log12']) ?>
<h2><?php echo $log12;?></h2>
</header>
<section class="entry">
<p class="textfield">
<label for="author2"> <small><br />
Lecture Login ID (required)</small> </label>
<input name="luid" id="luid" value="" size="22" tabindex="3" type="text" />
</p>
<p class="textfield">
<label for="email2"> <small>Password (required)</small> </label>
<input name="lpwd" id="lpwd" size="22" tabindex="4" type="password" />
</p>
<p>
<input name="submit2" id="submit2" tabindex="5" type="image" src="images/submit.png" />
<input name="comment_post_ID2" value="1" type="hidden" />
</p>
<div class="clear"></div>
</form>
<div class="clear"></div>
</section>
</div>
</section>
</article>
</section>
<?php
include("adminmenu.php");
include("footer.php"); ?>
Database:
table fields in administrator are: adminid,password,adminname,address,contactno
table fields in lectures are:
lecid,password,courseid,lecname,gender,address,contactno.
Please provide solution for this issue.
instead of using this code
if(md5($_POST["pwd"])=='$pwdmd5')
use this
if(md5($_POST["pwd"])==$pwdmd5)
In the first case, the hashed pasword is compared to the string $pwdmd5, in the second to the content of $pwdmd5

PHP 1 item conversion to PHP loop

So I am trying to learn php, I have a simple 1 item order form, However I have 4 items in the data base I need to show on this order form. In javascript I would just write a loop for an array, however the whole php query form a table from a database has got me flustered and I am not sure how to loop the results to show more then the 1 item from the query and the still allow for ordering all on 1 page. Originaly I had it list a table so you would click on the item and it would redirect to a page for each item so each on can be ordered.
<?php
include_once 'helpers/helper.php';
include_once 'db_function.php';
session_start();
$show_query = "SELECT * FROM `meal_info` ORDER BY meal_time DESC LIMIT 4;";
$show_result = db_query($connect, $show_query);
if($show_result) {
$show_result_row = db_fetch_array($show_result);
}
?>
<?php include_once 'layout/header.php'; ?>
<?php
if(#$_SESSION['order_submit_msg']) {
echo flash_message('order_submit_msg');
unset($_SESSION['order_submit_msg']);
}
?>
<div>
<form action="make_deal.php" method="post">
<div>
<h1><?php echo $show_result_row['meal_name'];?></h1>
</div>
<hr>
<div>
<img class="meal-img" id="meal-img" src="<?php echo $show_result_row['meal_image'];?>" alt="">
</div>
<br>
<div>
<h2>Feature: <?php echo $show_result_row['meal_content'];?></h2>
<h3>Price: <?php echo $show_result_row['meal_price'];?></h3>
</div>
<input type="hidden" name="meal-id" value="<?php echo $show_result_row['meal_id'];?>">
<input type="hidden" name="meal-price" value="<?php echo $show_result_row['meal_price'];?>">
<br>
<div>
<legend>Name</legend>
<input type="text" name="order-username" placeholder="Full Name" required>
</div>
<br>
<div>
<legend>Phone</legend>
<input type="text" name="order-phone" placeholder="555-555-5555" required>
</div>
<br>
<div>
<legend>Address</legend>
<input type="text" name="order-address" placeholder="555 N. Main Street" required>
</div>
<br>
<div>
<legend>Quantity (Dozens)</legend>
<input type="text" name="order-count" placeholder="12" required>
</div>
<hr>
<div>
<button class="btn btn-warning btn-lg btn-block" type="submit">Submit Order</button>
</div>
</form>
</div>
<?php include_once 'layout/footer.php'; ?>
You're trying to display an array. You need to loop through the array.
<?php foreach ($show_result_row as $result) { ?>
<div>
<h1><?php echo $result['meal_name'];?></h1>
</div>
<hr>
<div>
<img class="meal-img" id="meal-img" src="<?php echo $result['meal_image'];?>" alt="">
</div>
<?php } ?>
Something like that.

Passing variables from view to controller (MVC)

In my view i have a foreach statement which grabs all the nessesary data from the database which displays correctly.
Here is the view:
<?php
?>
<div class="cmt-container" >
<?php
foreach($results as $row){
$user = $row->user;
$comment = $row->comment;
$date = $row->date;
$name = $row->name;
$joke = $row->joke;
$joke_id = $row->joke_id;
// Get gravatar Image
// https://fr.gravatar.com/site/implement/images/php/
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/"."?d=".$default."&s=".$size;
?>
<div class="cmt-cnt">
<img src="<?php echo $grav_url; ?>" />
<div class="thecom">
<h5><?php echo $user; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
</div>
</div><!-- end "cmt-cnt" -->
<?php
}
?>
<?php
echo form_open('comments/insertComment');
?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="joke_id">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php
echo form_close();
?>
My question is, how can i pass the $joke_id variable to the insertComment function in my comments controller.
I have put the input field as hidden on the joke_id field because i want to assign the ID of a joke to a comment, so the joke will have unique comments.
If the comments are on the same page as the joke, you can just take the $joke_id and put it in the hidden input:
<input type="hidden" name="joke_id" value="<?php echo $joke_id; ?>">
And when you add a comment (I assume you handle the form datas in the insertComment() function), you can access the joke id by $_POST['joke_id'].
(yes, that's not really secure but if your user can comment any joke, you just have to check that a joke with the id equal to $_POST['joke_id'] exists in the DB and if so, you just insert the comment)
Is that what you wanted?
try this..
<?php echo form_open('comments/insertComment'); ?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="jokeid" value="<?= $joke_id; ?>">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php echo form_close();?>
and in your insert comment controller
public function __construct() {
//codes here
$joke_id = $this->input->post('jokeid');
}

Categories