Background color from table column - php

I have a hex color set inside of a table column and trying to set it to a div background but it will not apply it:
<?php
$user = $_SESSION['user'];
try{
$results = $dbh->query("SELECT *
FROM cat_List
INNER JOIN user_cat_link_table
ON cat_List.Cat_ID = user_cat_link_table.Cat_ID
WHERE user_cat_link_table.UserID = $user");
}catch(Exception $e) {
echo $e->getMessage();
die();
}
$docs = $results->fetchAll(PDO::FETCH_ASSOC);
foreach($docs as $docs){
echo '
<a href="catView.php?cat_id='.$docs["cat_id"].'">
<div class="indexBox" style="background-color="'.$docs["cat_color"].'">
<div class="indexBoxHeader">
<i class="fa fa-users" style="font-size: 2em;"></i></div>
<div class="indexBoxFooter">
<p>'.$docs["cat_title"].'</p>
</div>
</div>
</a>
';}
?>
If I echo the cat_color out it return the value #000 which is the value in the table? Is my syntax wrong?

It should be style="background-color:'.$docs["cat_color"].'"

Related

How to dislpay all animals that are status = 1 in PHP

I'm making an adoption website for a project.
I'm trying to display all animals whose status = 1 but the animals whose status = 0 keeps appearing in the category.
Is something wrong with my code? suggestions and corrections are welcome.
Thank you!
function.php
function get_animals($cat_id='', $animal_id='')
{
global $con;
$query = "SELECT * FROM animals WHERE status= 1";
if($cat_id!='')
{
$query = "SELECT * FROM animals WHERE category_name='$cat_id'";
}
if ($animal_id!='')
{
$query = "SELECT * FROM animals WHERE id=$animal_id";
}
return $result = mysqli_query($con,$query);
}
This is my animals.php file
<?php
$cat_id = '';
if(isset($_GET['id']))
{
$cat_id = mysqli_real_escape_string($con,$_GET['id']);
}
$particular_animal = get_animals($cat_id);
?>
<!-- End Navigation -->
<!-- Product Grid -->
<div class="container mt-5 ">
<div class="row">
<?php
if(mysqli_num_rows($particular_animal))
{
while($row = mysqli_fetch_assoc($particular_animal))
{
?>
<div class="col-md-4 product-grid">
<div class="row">
<div class="image border border-info bg-light">
<a href="animal_details.php? a_id=<?php echo $row ['id'] ?>">
<img src="admin/image/<?php echo $row['img']?>" class="w-100" alt="">
</a>
<h4 class="text-center mt-2 text-info font-weight-bold"><?php echo $row ['name'] ?></h4>
<p class="text-center mt-2"><?php echo $row ['gender'] ?></p>
</div>
</div>
</div>
<?php
}
}
else
{
echo "record not here";
}
?>
</div>
</div>
You're overwriting your $query (the one with the status) when $cat_id or $animal_id is set. Instead, add to the WHERE clause of your first query:
$query = "SELECT * FROM animals WHERE status= 1";
if($cat_id != '') {
$query .= " AND category_name='$cat_id'";
}
if ($animal_id != '') {
$query .= " AND id=$animal_id";
}
If your function gets a category id or an animal id, then your query is overriden and your status criteria is lost. This is a 1-command solution:
function get_animals($cat_id='', $animal_id='')
{
return mysqli_query($con,"SELECT * FROM animals WHERE status= 1" .
($cat_id ? " AND category_name = {$cat_id} " : "").
($animal_id ? " AND animal_id = {$animal_id} " : ""));
}

PHP function not showing data from dropdown menu

Currently creating 2 dropdown menus, one for category and one for subcategory. My current function shows all data on the page only for subcategories but not for categories. Why is this happening?
Current Functionality: User selects a category, page refreshes and is blank. Once user selects SUBcategory it shows all products in that subcategory.
Desired Functionality: User selects a category, page refreshes and shows all products in that category. Once user selects subcategory it shows all products in that subcategory.
function populate_search_catsub()
{
global $link;
$subcatt = "";
$query = 'SELECT * FROM item';
$result = mysqli_query($link, $query);
$catt = $_GET['catt'];
if (isset($_GET['subcatt'])) {
$subcatt=$_GET['subcatt'];
}
$nbprod = mysqli_query($link, "SELECT * FROM `item` WHERE cat='$catt' AND field='$subcatt'");
if (!isset($_GET['catt']) || $_GET['catt'] == '') {
unset($_GET['catt'], $_GET['submitsearchsub']);
populate_main();
} else {
if (isset($_GET['subcatt'])) {
echo '<span>Search results for Category="'.$catt.' And Sub Category='.$subcatt.'"</span><hr>';
}
$result = mysqli_query($link,"SELECT * FROM `item` WHERE cat='$catt' AND field='$subcatt'");
if ($cat = mysqli_fetch_row($result)) {
echo '
<div class="itemlist">
<span><h3>'.$cat[1].'</h3><h6><u>View</u></h6></span>
<div class="col-lg-12" style="background-color: white;"><br>
<div class="row">
<div class="col-lg-12" style="margin-right: 2%;">
<img src="https://via.placeholder.com/160x210">
</div>
</div><br>
</div>
</div>
<hr>
';
while ($cat = mysqli_fetch_row($result))
echo '
<div class="itemlist">
<span><h3 style="display:inline;">'.$cat[1].'</h3><h6 style="display:inline; margin-left: 1%;"><u>View</u></h6></span>
<div class="col-lg-12" style="background-color: white;"><br>
<div class="row">
<div class="col-lg-2" style="margin-right: 2%;">
<img src="https://via.placeholder.com/160x210">
</div>
</div><br>
</div>
</div>
<hr>
';
} else {
if (isset($_GET['subcatt'])) {
echo "<h2 >No results found</h2>";
}
unset($_GET['catt'], $_GET['submitsearchsub']);
populate_main();
}
}
}
First of all, grab $_GET['catt'] and $_GET['subcatt']. Then, you can build a query based on these parameters. And please, use mysqli_num_rows to check if there are more than zero rows returned.
function populate_search_catsub()
{
global $link;
$catt = $_GET['catt'] ?? ''; // Requires PHP 7.0 or upper.
$subcatt = $_GET['subcatt'] ?? ''; // Requires PHP 7.0 or upper.
$query = 'SELECT * FROM `item`'; // Initial query
if (!$catt) {
unset($_GET['submitsearchsub']);
populate_main();
} else {
$query .= " WHERE `cat` = '{$catt}'";
if ($subcatt) {
echo '<span>Search results for Category="' . $catt . ' And Sub Category=' . $subcatt . '"</span><hr>';
$query .= " AND `field` = '{$subcatt}'";
}
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) { // Check if result set is not empty.
while ($cat = mysqli_fetch_row($result))
echo '
<div class="itemlist">
<span><h3 style="display:inline;">'.$cat[1].'</h3><h6 style="display:inline; margin-left: 1%;"><u>View</u></h6></span>
<div class="col-lg-12" style="background-color: white;"><br>
<div class="row">
<div class="col-lg-2" style="margin-right: 2%;">
<img src="https://via.placeholder.com/160x210">
</div>
</div><br>
</div>
</div>
<hr>
';
} else {
if ($subcatt) {
echo '<h2 >No results found</h2>';
}
unset($_GET['catt'], $_GET['submitsearchsub']);
populate_main();
}
}
}

How to Display User Chats using PHP and MySQL

I wrote this query To fetch details but its not working. Here is my Code:
$sender_id=$_GET['sender_id'];
$sender_id=mysqli_real_escape_string($con,$sender_id);
$sender_id=htmlentities($sender_id);
$sql1="SELECT * FROM massges where receiver_id='".$_SESSION['u_id']."' and sender_id='".$sender_id."' order by msg_id";
$sql2="SELECT * FROM massges where receiver_id='".$sender_id."' and sender_id='".$_SESSION['u_id']."' order by msg_id";
$sqls=[$sql1,$sql2];
foreach($sqls as $k => $sql){
$run=mysqli_query($con,$sql);
if (mysqli_num_rows($run)>0) {
while ($row=mysqli_fetch_assoc($run)) {
$msg_text=$row['msg_text'];
$msg_id=$row['msg_id'];
$query1="SELECT * FROM users where u_id='".$sender_id."'";
$query2="SELECT * FROM users where u_id='".$_SESSION['u_id']."'";
$querys=[$query1,$query2];
foreach($querys as $k => $query){
$res=mysqli_query($con,$query);
while($data=mysqli_fetch_assoc($res)){
$user_image=$data['user_image'];
}
echo '<br>
<div class="row">
<div class="col s2" style="margin: 0px;">
<img src="users/'.$user_image.'" class="circle right" height="20px">
</div>
<div class="col s6"><span class=" blue lighten white-text" style="margin: 0px;border-radius: 10px;padding:5px;">'.$msg_text.'<span></div>
</div>';}
Here is the displayed data:
Result Image
How can I display sender's and receiver's messages?
you can do this in one query, your code will be much easier to debug if you do so:
$sender_id = $_GET['sender_id'];
$user_id = $_SESSION['u_id'];
// Get all Messages where the user and sender are involved
$statement = $con->prepare('
SELECT
messages.msg_text,
users.user_image
FROM
messages
JOIN
users ON users.u_id = messages.sender_id
WHERE
(
messages.sender_id = ?
AND messages.receiver_id = ?
) OR (
messages.sender_id = ?
AND messages.receiver_id = ?
)
ORDER BY
messages.msg_id
');
$statement->bind_param(
"iiii",
$sender_id,
$user_id,
$user_id,
$sender_id
);
$statement->execute();
// Assign the content of 'msg_text' and 'user_image'
$statement->bind_result($text, $image);
// loop through all results
while ($statement->fetch()) {
/* ECHO YOUR HTML HERE */
echo $text;
echo $image;
}
Links that may help:
http://php.net/manual/en/mysqli-stmt.bind-param.php
http://php.net/manual/en/mysqli-stmt.bind-result.php
$sender_id = $_GET['sender_id'];
$user_id = $_SESSION['u_id'];
$sql="SELECT
massges.msg_text,
massges.msg_id,
users.user_image
FROM
massges
JOIN
users ON users.u_id = massges.sender_id
WHERE
(
massges.sender_id = '".$sender_id."'
AND massges.receiver_id = '".$user_id."'
) OR (
massges.sender_id = '".$user_id."'
AND massges.receiver_id = '".$sender_id."'
)
ORDER BY
massges.msg_id";
$run=mysqli_query($con,$sql);
if (mysqli_num_rows($run)>0) {
while ($row=mysqli_fetch_assoc($run)) {
$msg_id=$row['msg_id'];
$msg_text=$row['msg_text'];
$user_image=$row['user_image'];
echo '<br>
<div class="row">
<div class="col s2" style="margin: 0px;">
<img src="users/'.$user_image.'" class="circle right" height="20px">
</div>
<div class="col s6"><span class=" blue lighten white-text" style="margin: 0px;border-radius: 10px;padding:5px;">'.$msg_text.'<span></div>
</div>'; } }

jquery output not working individually on php executed posts

i have a query that have many outputs and in those output i want to execute a jquery but that jquery is working in each & every output i want it to work only on the clicked one
this is my code
$query = "SELECT ph.likes, ph.image_url,ph.email,ph.username,ph.uid ,ph.id,ph.avatar_path
FROM photos as ph
inner join followers as fol
on fol.user_id = ph.uid
where fol.uid = '$id'
ORDER BY ph.image_url DESC ";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
while ($users = mysqli_fetch_assoc($fire)) {
$likes = $users['likes'];
$username = $users['username'];
$uid = $users['uid'];
$pixid = $users['id'];
$avatar_path5 = $users['avatar_path'];
?>
<div class="all" >
<div class="card" >
<div class="float" >
<div class="avatar" >
<img src="<?php echo $avatar_path5; ?>" width="100%" class="avatar">
</div>
<div class="username" style="font-weight: 600; size: 14px; text-decoration: none;">
<p><?php echo "<div><a href='users.php?id=".$users['uid']."'>
<h3>".$users['username']."</h3>
</div></a>"; ?></p>
</div>
</div>
<img src="<?php echo $users['image_url']?>" alt="Avatar" style="width:682px;">
<div class="container">
<h4><b><?php echo "<div><a href='users.php?id=".$users['uid']."'>
</div></a>";?></b></h4>
</div>
<span id="count" class="likes_count"><?php echo $users['likes']; ?> likes</span>
<div style="padding: 2px; margin-top: 5px;">
<?php
if (isset($_POST['liked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)
or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "INSERT INTO likes (user_id,username, post_id) VALUES ($id, '$fullname', $postid)")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n+1 WHERE id=$postid")or die(mysqli_error($con));
echo $n+1;
exit();
}
if (isset($_POST['unliked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "DELETE FROM likes WHERE post_id=$postid AND user_id=$id")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n-1 WHERE id=$postid")or die(mysqli_error($con));
echo $n-1;
exit();
}
?>
</div>
<div>
<?php
// determine if user has already liked this post
$results = mysqli_query($con, "SELECT * FROM likes WHERE user_id=$id AND post_id=".$users['id']."")or die(mysqli_error($con));
if (mysqli_num_rows($results) == 1 ): ?>
<!-- user already likes post -->
<span class="unlike fas fa-heart animated bounceIn" data-id="<?php echo $users['id']; ?>"></span>
<span class="like hide far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<?php else: ?>
<!-- user has not yet liked post -->
<span class="like far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<span class="unlike hide fas fa-heart animated bounceIn" data-id="<?php echo $users['id']; ?>"></span>
<?php endif ?>
<a class="com" href="comments.php"> <span class="far fa-comment"></span></a>
<p>This is a paragraph.</p>
<button class="y<?php echo $users['id']?>">Toggle slideUp() and slideDown()</button>
<script>
$(document).ready(function(){
$(".y").click(function(){
$("p").slideToggle();
});
});
</script>
</div>
</div><br><br>
<?php } ?>
see this script in my code
<p>This is a paragraph.</p>
<button class="y<?php echo $users['id']?>">Toggle slideUp() and slideDown()</button>
<script>
$(document).ready(function(){
$(".y").click(function(){
$("p").slideToggle();
});
});
</script>
i want it to work only on the clicked post not on each & every post please help me out
for ex a if click on a post button so i want this script to work only for that
particular post's button not for every post button how to solve this
You seem to have the jquery selectors mixed up. From my reading of your PHP you apply a variable class name to the buttons that starts with y, then you select those with a class of y which won't match.
One way round this would be to also apply a consistent class to all the buttons (buttonclass in my example)
Then you're selecting all <p> elements to apply the slideToggle() instead of just the one whose button was clicked. You need to apply it only to the parent <p>.
This is one method of doing it:
$(document).ready(function(){
$(".buttonclass").click(function(){
$(this).parent().slideToggle();
});
});

Category is not placed at the correct section

I have a problem while printing categories and sections (for a forum). So I have 2 tables : forum_section and forum_category -> the cat_id column from forum_section is linked with the section column from forum_category. Here's my query:
$forum = $con->query("SELECT a.*, b.*
FROM `forum_section` a
INNER JOIN `forum_category` b ON a.`cat_id` = b.`section` GROUP BY a.`cat_name` ORDER BY a.`cat_id`");
And that's how I'm trying to print them:
while($row = mysqli_fetch_object($forum)) {
echo '<div id="section">
<div class="section-head"><h3 class="yellow_text">'.$row->cat_name.'</h3></div>
'.printCategories($row->cat_id).'
</div>';
}
}
That's my printCategories function:
function printCategories($id) {
global $con;
$categories = $con->query("SELECT * FROM `forum_category` WHERE `section`='$id'");
while($row = mysqli_fetch_object($categories)) {
echo '<div class="cat">
<div class="cat-title">
<img src="styles/default/images/icons/'.$row->icon.'.png" alt="Icon">
<span>'.$row->name.'</span>
</div>
<div class="cat-topics">
asd
</div>
<div class="cat-posts">
asd
</div>
<div class="cat-lasttopic">
last topic name goes here
</div>
</div>';
}
}
So it does print them but not in the right place as you can see in this picture:
printCategories needs to return the result, not echo it, so that the concatenation in the caller will put it into the DIV that it's echoing. Your code is echoing before it returns anything, so it gets printed before the caller concatenates the result and prints its DIV.
function printCategories($id) {
global $con;
$categories = $con->query("SELECT * FROM `forum_category` WHERE `section`='$id'");
$result = '';
while($row = mysqli_fetch_object($categories)) {
$result += '<div class="cat">
<div class="cat-title">
<img src="styles/default/images/icons/'.$row->icon.'.png" alt="Icon">
<span>'.$row->name.'</span>
</div>
<div class="cat-topics">
asd
</div>
<div class="cat-posts">
asd
</div>
<div class="cat-lasttopic">
last topic name goes here
</div>
</div>';
}
return $result;
}

Categories