Lead Management System Comments Show for Assigned User - php

I am currently still learning PHP and I have been working on this project for a little over a week. I am pulling my hair out. So, we have a Lead Management System, users can enter comments for leads assigned to them. We want those comments to be hidden if the the lead is discarded or assigned to another user.
Here is the current code:
<?php $lead_id = $row['id'];?>
<?php $getcommentscount_stmt = "SELECT COUNT(*) FROM comments WHERE comment_by_user_id = $_SESSION[user_id] AND lead_id = $lead_id ORDER BY comment_created_on DESC";?>
<?php $result_commentscount = mysqli_query($mysqli, $getcommentscount_stmt);?>
<?php $comment_count = mysqli_fetch_row($result_commentscount);?>
<?php $num_comments = $comment_count[0];?>
<div class="panel-group" id="accordion2" style="margin-bottom: 0px;">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo<?php echo $row['id'];?>">Comments<span class="caret"></span> <span class="badge pull-right"><?php echo $num_comments;?></span></a>
</h4>
</div>
<div id="collapseTwo<?php echo $row['id'];?>" class="panel-collapse collapse">
<div class="panel-body">
<table class="table table-bordered">
<tr>
<td width="25%"><strong>Time</strong></td>
<td width="75%"><strong>Comment</strong></td>
</tr>
<?php $lead_id = $row['id'];?>
<?php $getcomments_stmt = "SELECT * FROM comments WHERE comment_by_user_id = $_SESSION[user_id] AND lead_id = $lead_id ORDER BY comment_created_on DESC";?>
<?php $result_comments = mysqli_query($mysqli, $getcomments_stmt);?>
<?php while($row2 = mysqli_fetch_array($result_comments)) { ?>
<tr>
<td width="25%"><?php echo $row2['comment_created_on'];?></td>
<td width="75%"><?php echo nl2br($row2['comment_text']);?></td>
</tr>
<?php } ?>
</table>
This is the last thing I tried:
<?php $getcomments_stmt = "SELECT * FROM comments WHERE comment_by_user_id = $_SESSION[user_id] AND lead_id = $lead_id AND times_assigned <> 1 OR times_assigned IS NULL ORDER BY comment_created_on DESC";?>
I am not sure what else to show, but any help is appreciated.
EDIT: I am getting this
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /var/www/html/html/showleads.html on line 884
Line 884:
<?php while($row2 = mysqli_fetch_array($result_comments)) { ?>

Related

Pick Up and Drop Dates not working correctly

so im currently working on a car rental and so far i have made reservations for certain cars.
Now what im trying to do is whenever someone searches for an available car , the cars that are not taken on these dates should show up .
I have stored pickup date and drop date on reservation table on database.
Car Database Start and End Rental Dates : https://i.stack.imgur.com/9mLTt.png
Car Showing when its not suppost to
https://i.stack.imgur.com/OiNRj.png
https://i.stack.imgur.com/5KMxP.png
Cars showing correctly https://i.stack.imgur.com/67oc6.png
So in other words if you are not able to get whats wrong i will try to make it as clear as possible.
I have 4 dates which are named as below
DB - StartDate = '2023-02-06'DB - EndDate = '2023-02-08'
DB - Selected_car = '12' is the car ID Then we have
StartDate = '2023-02-06' - This is the date inputed by the client
EndDate = '2023-02-07' - As Show in the images above
This search query would put the car as available which should not because the car is taken from date 06 till 08 also if you search till 09 as end date , the car shows available which should not because starting date is 06 and end is 08 which means the car is taken in these dates. Below you'll find the code i did for the search.php
<?php
if(isset($_POST['submit']))
{
$startDate = $_POST['pickupDate'];
$returnDate = $_POST['returnDate'];
$startDate = new DateTime($startDate);
$returnDate = new DateTime($returnDate);
$startDate = $startDate->format("Y-m-d ");
$returnDate = $returnDate->format("Y-m-d ");
if(empty($startDate))
{
$_SESSION['ErrorMessage'] = "Pickup Date can't be empty";
TakeTo("index.php");
}
elseif(empty($returnDate))
{
$_SESSION['ErrorMessage'] = "Return Date can't be empty";
TakeTo("index.php");
}
elseif($startDate == $returnDate)
{
$_SESSION['ErrorMessage'] = "Cars can only be rented for 1 or more days";
TakeTo("index.php");
}
elseif($returnDate <= $startDate)
{
$_SESSION['ErrorMessage'] = "Return date cannot be before Pickup Date";
TakeTo("index.php");
}
echo "<h3 class='text-center mt-2 bg-light'>Pickup Date : $startDate & Return Date : $returnDate <br> </h3>";
$carIDArray = array();
$data = [
'dateStart' => $startDate,
'dateEnd'=> $returnDate,
];
$sql = "SELECT
reservations.selected_car
FROM
reservations
WHERE
reservations.start_date >= :dateStart
AND
reservations.end_date <= :dateEnd
GROUP BY
reservations.selected_car"
;
$stmt = $con->prepare($sql);
$execute=$stmt->execute($data);
if($execute)
{
$results = $stmt->rowcount();
if($results >= 1)
{
while($resInfo = $stmt->fetch())
{
$carID = $resInfo['selected_car'];
$carIDArray[] = $carID;
}
$carIds = implode(",", $carIDArray);
$sql2 = "SELECT
cars.*
FROM
cars
WHERE
!FIND_IN_SET(cars.id,'$carIds') ORDER BY id DESC";
$stmt2 = $con->query($sql2);
while($carInfo = $stmt2->fetch())
{
$AllIds = $carInfo['id'];
$carName = $carInfo['car_name'];
$carEngine = $carInfo['engine'];
$carYear = $carInfo['production_year'];
$carSpend = $carInfo['100km'];
$carCapacity = $carInfo['full_tank'];
$carDoors = $carInfo['doors'];
$carImage = $carInfo['picture'];
$carTransmition = $carInfo['transmition'];
$carPrice = $carInfo['dailyprice'];
$carPetrol = $carInfo['fuel_type'];
?>
<!--Cars Display All-->
<div class="container mt-3">
<div class="container-fluid">
<div class="card mb-3 all-cars">
<div class="row g-0">
<div class="col-md-4 col-sm-6">
<img height="250" width="100%" src="carimages/<?php echo $carImage; ?>" class="rounded-star" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h3><?php echo $carName; ?></h3>
<hr class="bg-black">
<div class="d-block">
<table>
<tbody>
<tr>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gear"></i> <?php echo $carTransmition; ?> </span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-car-side"></i> <?php echo $carDoors; ?> Doors</span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gas-pump"></i> <?php echo $carSpend; ?>L/100km </span></td>
</tr>
<tr><td><br></td></tr>
<tr>
<td> <span class="m-3 bold-text "><i class="fa-solid fa-calendar-day"></i> <?php echo $carYear; ?></span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gas-pump"></i> <?php echo $carCapacity; ?>L </span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gas-pump"></i> <?php echo $carPetrol; ?> </span></td>
</tr>
</tbody>
</table>
<h5 class="text-end top-50 m-3 bold-text"><strong ><?php echo $carPrice; ?>€</strong>/day</h5>
Choose
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
else

ID number doesn't change

I've been creating a CMS blog and this is one of the pages that accessible by the admins. I'm trying to implement pagination in it.
As you can see, it shows the latest six posts in the first page with the IDs from 1 to 6, but when I click on the forward button, it shows the IDs 1 and 2 again for the other posts in which it should be 7 and 8. Could someone tell me what's causing this bug?
First page
Second page
<!-- Right Side Area Start -->
<div class="col-lg-10">
<div class="card-body bg-info">
<h2 class="large-georgia-white-bold">Top Posts</h2>
</div>
<table class="table table-striped table-hover">
<thead class="thead-dark small-times-white">
<tr>
<th>No.</th>
<th>Title</th>
<th>Date&Time</th>
<th>Author</th>
<th>Comments</th>
<th>Details</th>
</tr>
</thead>
<?php
$SrNo = 0;
global $ConnectingDB;
// Query When Pagination is Active i.e Dashboard.php?page=1
if (isset($_GET["page"])) {
$Page = $_GET["page"];
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$sql ="SELECT * FROM posts ORDER BY id desc LIMIT $ShowPostFrom,6";
$stmt=$ConnectingDB->query($sql);
}
// The default SQL query
else{
$sql = "SELECT * FROM posts ORDER BY id desc LIMIT 0,6";
$stmt=$ConnectingDB->query($sql);
}
while ($DataRows=$stmt->fetch()) {
$PostId = $DataRows["id"];
$DateTime = $DataRows["datetime"];
$Author = $DataRows["author"];
$Title = $DataRows["title"];
$SrNo++;
?>
<tbody class="small-times-black">
<tr>
<td><?php echo $SrNo; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $DateTime; ?></td>
<td><?php echo $Author; ?></td>
<td>
<?php $Total = ApproveCommentsAccordingtoPost($PostId);
if ($Total>0) {
?>
<span class="badge badge-success">
<?php
echo $Total; ?>
</span>
<?php } ?>
<?php $Total = DisApproveCommentsAccordingtoPost($PostId);
if ($Total>0) { ?>
<span class="badge badge-danger">
<?php
echo $Total; ?>
</span>
<?php } ?>
</td>
<td> <a target="_blank" href="FullPost.php?id=<?php echo $PostId; ?>">
<span class="btn btn-info">Preview</span>
</a>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<!-- Right Side Area End -->
<!-- Pagination -->
<nav>
<ul class="pagination pagination-lg">
<!-- Creating Backward Button -->
<?php if( isset($Page) ) {
if ( $Page>1 ) {?>
<li class="page-item">
«
</li>
<?php } }?>
<?php
global $ConnectingDB;
$sql = "SELECT COUNT(*) FROM posts";
$stmt = $ConnectingDB->query($sql);
$RowPagination = $stmt->fetch();
$TotalPosts = array_shift($RowPagination);
//echo $TotalPosts."<br>";
$PostPagination=$TotalPosts/6;
$PostPagination=ceil($PostPagination);
//echo $PostPagination;
for ($i=1; $i <= $PostPagination ; $i++) {
?>
<li class="page-item">
<?php echo $i; ?>
</li>
<?php } ?>
<!-- Creating Forward Button -->
<?php if ( isset($Page) && !empty($Page) ) {
if ($Page+1 <= $PostPagination) {?>
<li class="page-item">
»
</li>
<?php } }?>
</ul>
</nav>
</div>
</div>
</section>
<!-- Main area end -->
Your script always reassigns $SrNo = 0 when the page is loaded and starts over. You should add the page * 6 value to it so it becomes aware of the offset. In fact, you're already using that logic for $ShowPostFrom, so you can simply assign the same value to $SrNo and it should work:
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$SrNo = $ShowPostFrom; // <- this is what you should add
If you don't mind modifying $ShowPostFrom, you can drop $SrNo completely and just use $ShowPostFrom to show the number.

How to echo specific elements from a database

I'm trying to echo certain elements from my news table throughout an article. This involves fields such as the author, headline, content and date. At the moment I can only echo the row and any attempt to echo elements in the page itself is either met with small errors or it outputs nothing. I've had a look around and only found people having problems with simply printing out the row database which I can already do.
Basically when I have Author in my article I want to echo author from my database and it will show next to Author. I'm not sure how possible it is as I am yet to find anything on this or I'm overlooking it.
Here is my current PHP file and I've left in the initial part with the article author, date and headline.
<?php
/* Database connection settings */
$host = 'xxxxx';
$user = 'xxxxx';
$pass = 'xxxxx';
$db = 'xxxxx';
$dbconnect=mysqli_connect($host,$user,$pass,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query = mysqli_query($dbconnect, "SELECT * FROM news WHERE news_Id = 1")
or die (mysqli_errr($dbconnect));
while ($row = mysqli_fetch_array($query)) {
echo
"<tr>
<td>{$row['headline']}</td>
<td>{$row['content']}</td>
<td>{$row['author']}</td>
<td>{$row['date']}</td>
</tr>\n";
}
?>
<div class="container-fluid bg">
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="container">
<div class="row mb-2">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="news-title">
<h2><?php echo $row['headline']; ?></h2>
</div>
<div class="news-cats">
<ul class="list-unstyled list-inline mb-1">
<li class="list-inline-item">
<i class="fa fa-folder-o text-danger"></i>
<small>Author:</small>
</li>
<li class="list-inline-item">
<i class="fa fa-folder-o text-danger"></i>
<small>Posted:</small>
</li>
</ul>
</div>
<hr>
Since your query only returns one row, a while loop is inappropriate for fetching the data, as at the end of the loop, $row will be left as a false value, thus making any attempt to access e.g. $row['headline'] fail. Change your while loop
while ($row = mysqli_fetch_array($query)) {
to a simple assignment:
$row = mysqli_fetch_array($query) or die(mysqli_error($dbconnect));
Note you have a typo earlier in your code,
or die (mysqli_errr($dbconnect));
should be
or die (mysqli_error($dbconnect));
You are just printing out the result, but don't have a label. You can add one like this:
while ($row = mysqli_fetch_array($query)) {
echo
"<tr>
<td>Headline: {$row['headline']}</td>
<td>Content: {$row['content']}</td>
<td>Author: {$row['author']}</td>
<td>Date: {$row['date']}</td>
</tr>\n";
}

I need help displaying data in php

I am just learning php and I have a problem with a project. I have a database in mysql (localhost) and in it I have 3 tables: categories, products and restaurants, categories and restaurants are linked to products. And when I try to display products of a specified category it doesn't display anything. The connection to the database is good and the queries are good so I have no idea what's wrong. Pleas help.
This is the index.php
<?php
include_once './database.php';
include_once './header.php';
<div class="content" align="center">
<div id="galery" class="content_block" style="height:400px;">
<div class="galery">
<img src="./img/gallery/6.jpg">
</div>
</div>
<div class="content_block" style="padding-bottom:10px;">
$query = "SELECT * FROM categories";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
echo '<div class="contentButton inline"><div class="catFood_img foodCategory_1"></div><div>'.$row['category'].'</div></div>';
</div>
<div class="content_block" style="height:190px" align="center">
$query = "SELECT * FROM restourants";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
echo '<div class="restournats inline"><img src="./food/restourants/chens.jpg"></div>';
}
</div>
</div>
include_once './footer.php';
?>
And this is the code for categories.php
include_once './database.php';
include_once './header.php';
<div class="content" align="center">
<div class="content_block2">
$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');
$query = "SELECT * FROM products WHERE products.id_category=$category";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){
echo '
<a href="dishes.php?id='.$row['id'].'"><div class="item inline">
<div class="food_img"><img src="'.$row['img'].'" /></div>
<div class="title">'.$row['name'].'</div>
<div class="price inline">Price: '.$row['price'].' €</div><div class="btn">Choose</div>
</div>
</a>';
}
</div>
</div>
include_once './footer.php';
In your index.php you are creating the link as
echo '<a href="categories.php?cat='.$row['category'].'">
(double check $row['category'] given above is the desired category id value you need)
But in your categories.php you are trying to get the category as
$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');
Try $_GET['cat'] instead of $_GET['id_category']
Also try echo $category and echo $query to see if you are getting the desired values.

How do I sort elements in a while loop?

I'm about to display comments on a page, everything works fine but I want to display the comments from the user first. So, how do I display the user comments from the user_id first? I'm using a while loop to display the comments.
I would be very grateful if anyone could help me. :)
<?php
$sql = "SELECT * FROM `comments` WHERE `post_id`=$pid AND `active`=1 ORDER BY ups-downs DESC";
$result = $mysqli->query($sql);
$count = $result->num_rows;
if($count == 1){
$counttext = "1 Comment";
}else{
$counttext = $count ." Comments";
}
?>
<div class="media-area media-area-small">
<h3 class="text-center"><?php echo $counttext; ?></h3>
<?php
while($row = $result->fetch_assoc()){
$uid = (int)$row["user_id"];
$user = get_user_info($mysqli,$uid);
?>
<div class="media">
<a class="pull-left" href="#">
<div class="avatar">
<img class="media-object" src="<?php echo $user["picture"]; ?>" alt="...">
</div>
</a>
<div class="media-body">
<table width="100%">
<tr valign="middle">
<td align="left"><h4 class="media-heading text-left"><?php echo fb_clear_name($mysqli, $user["id"]); ?></h4></td>
<td align="right"><h6 class="pull-right text-muted"><?php echo $row["ups"] - $row["downs"]; ?> bits</h6></td>
</tr>
</table>
<p><?php echo htmlentities($row["content"]); ?></p>
<div class="media-footer">
<i class="fa fa-reply"></i> Reply
<a href="#" class="pull-right text-muted">
<?php echo $row["timestamp"]; ?>
</a>
</div>
</div>
</div>
<?php
}
?>
Greetings
You can prefilter the data. For example use
$user_comments=array();
$other_comments=array();
while($row = mysql_fetch_assoc($result))
{
if($row['user']==$current_user)
{
$user_comments[]=$row;
}
else
{
$other_comments[]=$row;
}
}
$comments=array_merge($user_comments,$other_comments);
Then you can display the information the way you want to.

Categories