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>'; } }
Related
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} " : ""));
}
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();
}
}
}
enter image description here
How can I make a loop with php where I separate the comments as rounds
All comments for the round 1
<div id="$row['round']">
$row['comments']
</div>
All comments for the round 2
<div id="$row['round']">
$row['comments']
</div>
but I can have many rounds, so I don't know the exact query to use
<div id="chat" class="chat">
<ul class="nav nav-tabs">
<?php
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<li><a data-toggle="tab" href="#menu'.$row['round'].'">'.$row['round'].'</a></li>';
}
?>
</ul>
<div class="tab-content">
<?php
$i=-1;
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<div class="tab-pane '.( $i = $i ? 'active' : '' ).'" id="menu'.$row['round'].'">';
$test = $row['round'];
$stmt2 = $DB->query("SELECT * FROM messages INNER JOIN system_users ON messages.user_id = u_userid WHERE $id = video_id AND $test = round ORDER BY date ASC");
while ($row2 = $stmt2->fetch()) { ?>
<br />
<p><b>round:</b> <?php echo $row2["round"]; ?></p>
<p><b>Message:</b> <?php echo $row2["message"]; ?></p>
<p><b>User:</b> <?php echo $row2["u_username"]; ?></p>
<p><b>time:</b> <?php echo date_format (new DateTime($row2["date"]), 'd|m|Y H:i:s'); ?></p>
<hr />
<?php
}$i =0;
echo '</div>';
}
?>
</div>
I am completely sure this is not the best solution, but it is working,
thanks anyway..
How can I page info that is received from mysql table..
I want to show on every page 4 rows
I have this code :
<?php
include_once "config.php";
$sql_select_product = "SELECT * FROM product ORDER BY date DESC;";
$result_select_product = mysqli_query($connection, $sql_select_product);
if (mysqli_num_rows($result_select_product) > 0) {
while ($row_select_product = mysqli_fetch_assoc($result_select_product)) {
echo "
<div class='col-6 col-sm-3' style='margin-bottom: 20px;'>
<img width='250' height='250' src='on_login_pages/uploads/$row_select_product[img]'/>
<div class='des'>
<div class='price\">$row_select_product[price]</div>
<div class='name\">$row_select_product[name]</div>
<div class=''>$row_select_product[weight]</div>
<div class=''>$row_select_product[work_type]</div>
</div>
</div>";
}
}
$connection->close();
?>
If you call servername/path/script_name.php?page=[page_number]the script will return only 4 elements of the query.
<?php
$page_number = fiter_input(INPUT_GET,'page',FILTER_VALIDATE_INT,1,1);
$rows_per_page = 4;
$offset = ($page_number - 1) * $rows_per_page;
include_once "config.php";
$sql_select_product = sprintf("SELECT * FROM product ORDER BY date DESC LIMIT %d OFFSET %d;",
$rows_per_page,$offset);
$result_select_product = mysqli_query($connection, $sql_select_product);
if (mysqli_num_rows($result_select_product) > 0) {
while ($row_select_product = mysqli_fetch_assoc($result_select_product)) {
echo "
<div class='col-6 col-sm-3' style='margin-bottom: 20px;'>
<img width='250' height='250' src='on_login_pages/uploads/$row_select_product[img]'/>
<div class='des'>
<div class='price\">$row_select_product[price]</div>
<div class='name\">$row_select_product[name]</div>
<div class=''>$row_select_product[weight]</div>
<div class=''>$row_select_product[work_type]</div>
</div>
</div>";
}
}
Use limit in your query. Something like this:
select * from table_name order by column_name asc limit $offset,$startfrom;
I am trying to make a youtube like main page. With the code below I want to make videos that are recommended for my users.
The following code shows only a user's video.
<?php $query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
?>
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php } ?>
I want to show this section only one time.
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
Anyone can help me in this regard ?
The easiest way would be with a counter, like this:
<?php
$query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
$counter = 1;
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
if($counter == 1){
$counter++;
echo '<div class="onerilent"><img src="'.$user_avatar.'">'.$user_name.' Recommended for you</div>';
}
?>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php
}
?>
note the $counter variable is set to 1 before the loop, and inside the loop there is a condition to check if it is set to the value 1, and if it is, then it echo's your html and increments the $counter, so that it is no longer set to 1