Recommended user video Like youtube - php

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

Related

Liking system, how do I fix it?

I have the following script for showing posts and liking them, but if I like one post it likes all the posts on the page, I can't think of another way to do it, can anyone give me some advice?
<?php
if ($sort == 1){
$result = $conn->query("SELECT * FROM posts ORDER BY date DESC LIMIT 4 ");
}
elseif($sort == 2)
{
$result = $conn->query("SELECT * FROM posts WHERE date > NOW() - INTERVAL 24 HOUR ORDER BY likes DESC");
}
elseif($sort == 3)
{
$result = $conn->query("SELECT * FROM posts ORDER BY likes DESC");
}
if ($result->num_rows > 0) :
while($row = mysqli_fetch_assoc($result)) : ?>
<div class="card mb-4">
<img class="card-img-top" src="<?php echo $row['image1'] ?>" alt="Card image cap">
<div class="card-body">
<h2 class="card-title"><?php print title; ?></h2>
<p class="card-text"><?php print text; ?></p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on <?php print $row['date'] ?> by
<?php print $row['author']; ?>
<?php
$id=$row['id'];
if($_POST['like']) {
$update = "UPDATE posts set `likes` = `likes`+1 where `id` ='$id'";
if ($conn->query($update) === TRUE) {
} else {
echo "Error updating record: " . $conn->error;
}
} ?>
<form action="" method="POST">
<button type = "submit" value = "like" name='like'style="font-size:24px"><?php echo $row['likes']; ?><i class="fa fa-thumbs-o-up"></i>
</form>
</div>
</div>
<?php endwhile; endif; ?>
Your while loop contains the update query so your code should be change like this.
in order to get the id to like you just need to use a hidden field to post that id like in this code
<?php
if($_POST['like']) {
$id=$POST['id'];
$update = "UPDATE posts set `likes` = `likes`+1 where `id` ='$id'";
if ($conn->query($update) === TRUE) {
} else {
echo "Error updating record: " . $conn->error;
}
} ?>
<?php
if ($sort == 1){
$result = $conn->query("SELECT * FROM posts ORDER BY date DESC LIMIT 4 ");
}
elseif($sort == 2)
{
$result = $conn->query("SELECT * FROM posts WHERE date > NOW() - INTERVAL 24 HOUR ORDER BY likes DESC");
}
elseif($sort == 3)
{
$result = $conn->query("SELECT * FROM posts ORDER BY likes DESC");
}
if ($result->num_rows > 0) :
while($row = mysqli_fetch_assoc($result)) : ?>
<div class="card mb-4">
<img class="card-img-top" src="<?php echo $row['image1'] ?>" alt="Card image cap">
<div class="card-body">
<h2 class="card-title"><?php print title; ?></h2>
<p class="card-text"><?php print text; ?></p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on <?php print $row['date'] ?> by
<?php print $row['author']; ?>
<form action="" method="POST">
<input name="id" type="hidden" value="<?php echo $row['id']; ?>">
<button type = "submit" value = "like" name='like'style="font-size:24px"><?php echo $row['likes']; ?><i class="fa fa-thumbs-o-up"></i>
</form>
</div>
</div>
<?php endwhile; endif; ?>

php and mysql to create tabs with bootstrap 3

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..

Pagination script added php page isn't loading or showing any error

I tried to add a pagination script to my existing php page of sql queries.
But after adding the script the page is kept on loading without showing any content or error.
My code goes as:
<?php include('db.php'); ?>
<?php // define how many results you want per page
$results_per_page = 10;
// find out the number of results stored in database
$sql10='SELECT * FROM smf_messages';
$result10 = mysqli_query($conn, $sql10);
$number_of_results = mysqli_num_rows($result10);
// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
?>
Now the sql query codes to get the data from the respective tables...
<?php
$sql2 = "SELECT * FROM smf_log_digest WHERE note_type = 'topic' ORDER BY id_msg DESC LIMIT 420";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$number = $row2["id_msg"];
?>
This query relates to the content from which table to be retrieved..
<?php
// retrieve selected results from database and display them on page
$sql20='SELECT * FROM smf_messages WHERE id_msg = $number AND id_board = 4 LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result20 = mysqli_query($conn, $sql20);
while($row20 = mysqli_fetch_array($result20)) {
$member = $row20["id_member"];
$replies = $row20["id_topic"];
?>
<?php
$sqlstep1 = "SELECT COUNT(*) AS total FROM smf_log_digest WHERE note_type = 'reply' AND id_topic = $replies";
$rowNumstep1 = mysqli_query($conn, $sqlstep1);
$countstep1 = mysqli_fetch_assoc($rowNumstep1);
?>
// Body
<article class="well btn-group-sm clearfix">
<div class="topic-desc row-fluid clearfix">
<div class="col-sm-2 text-center publisher-wrap">
<img src="assets/images/profile.png" alt="" class="avatar img-circle img-responsive">
<h5><?php echo $row3["poster_name"]; ?></h5>
<small class="online">Member</small>
</div>
<div class="col-sm-10">
<header class="topic-footer clearfix">
<!-- end tags -->
</header>
<!-- end topic -->
<h4> <?php echo $row20["body"]; ?></h4>
<div class="blog-meta clearfix">
<small><?php echo $countstep1["total"]; ?> Replies</small>
<small><?php echo date('m/d/Y', $row20["poster_time"]); ?></small>
</div>
View the topic →
</div>
</div>
</article>
//end of body
<?php
}
// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
?>
<?php }
} else {
echo "";
}
?>
Please note that the data base connections are all checked and are right..
Any help is appreciated..
add this on top then check for error.
error_reporting(E_ALL);
ini_set('desplay_errors','1');

My user ranking system php sql help me

see screenshot development
as seen in the picture, where is the red circle should the number of user rankig appear. example: 1ts, 2th, 3rd...
I have tried everything but not me.
My table is users
ID Name username password Wins Loses
I leave my php code file profile.php
<?
require_once('values.inc.php');
require_once('func.inc.php');
if(!($_GET[user])) exit('Error.');
open_conn();
$res=mysql_query("SELECT ID, Name, username, Thumbnail, LinkText, LinkUrl, country FROM users WHERE Status='1' ORDER BY RAND() LIMIT 2");
while($row=mysql_fetch_assoc($res)) {
$username[] = $row[username];
$ID[] = $row[ID];
$Thumbnail[] = $row[Thumbnail];
$Name[] = $row[Name];
$LinkText[] = $row[LinkText];
$Model=mysql_query("SELECT ID, Name, username, Wins, Loses, Draws, Thumbnail, LinkText, LinkUrl, country, (Wins) / (Wins+Loses) AS WinPercent, (Wins+Loses+Draws) AS Matches, DATE_FORMAT(DateTime, '%d %M %Y') AS Date FROM users WHERE username='$_GET[user]' LIMIT 1");
$Model=mysql_fetch_assoc($Model);
$WinPercent=#round(($Model['Wins'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
$LosePercent=#round(($Model['Loses'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
if($Model['LinkText'] != '' && $Model['LinkUrl'] != '') {$Url="<br><br>$Model[LinkText]";}
if($row['LinkText'] != '' && $row['LinkUrl'] != '') {$Url[]="<br><br>$row[LinkText]<br><br>";}else{$Url[]='';}
}
close_conn();
?>
<?php if (isset($Model['username'])) { ?>
<div class="content-holder">
<div class="profile-holder">
<div class="profile-image">
<img src="images/uploads/<?php echo ucwords($Model['Thumbnail'])?>" class="photo">
</div>
<div id="photo-upload-holder">
<label for="pic" id="photo-upload-label">
<h1 class="txtShadow"><?php echo ucwords($Model['Name'])?></h1>
</label>
</div>
<!--<p class=\"userName\">#dada</p>-->
</div>
<div class="dashboard-Details overFlow">
<div class="leftColumn pull">
<div class="displayBlock"><br>
<i class="heart-icon"></i>
<span><?php echo ucwords($Model['Wins'])?></span>
</div><br>
<div class="displayBlock">
<i class="star-icon"></i>
<span><?php echo ($WinPercent)?>%</span>
</div>
</div>
<div class="rightColumn push">
<h2><?php echo ucwords($Model['username'])?></h2>
<?php if($Model['country']):?>
<h4> <?php echo $countrys; ?> <?php echo ucwords($Model['country'])?></h4>
<?php endif?>
<?php if($Model['LinkText']):?>
<h4> Instagram: #<?php echo ucwords($Model['LinkText'])?></h4>
<?php endif?>
</div>
<?php } else { ?>
<?php echo $profile14; ?>
use quotes here
$username[] = $row['username'];
$ID[] = $row['ID'];
$Thumbnail[] = $row['Thumbnail'];
$Name[] = $row['Name'];
$LinkText[] = $row['LinkText'];

MySQL query doesn't show all the records correctly

The query am running against my database to get the 3 records order it by Random. The problem is that sometimes it shows all 3 records sometimes it only shows 2, 1 and other times its just blank. In the database I have around 28 records.
What I have tried
I have tried without LIMIT - Problem Same
I have echoed out $suggested_profile_id found all 3 records coming out.
This is the query that gets the records LIMIT it by 3
<?php
$sql = "SELECT * FROM members WHERE member_status='activated' ORDER BY RAND() DESC LIMIT 3";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
?>
This is the code that runs and gets all 3 records in a loop.
<!-- Suggested Friends -->
<div class="col-md-0 media-body">
<?php
foreach($rows as $row){
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$suggested_profile_id = $row->profile_id;
$suggested_profile_photo = $row->profile_photo;
$suggested_profile_username = $row->profile_username;
$suggested_profile_name = $row->profile_name;
if(
$suggested_profile_id != GET_SESSION_ID_VALUE(ENCRYPTION_KEY)&&
!is_in_ARRAY($make_string_to_ARRAY, $suggested_profile_id)
){
?>
<div class="row margin0">
<div class="col-md-4 pad0">
<a href="/<?php echo $suggested_profile_username; ?>" title="<?php echo $suggested_friends_profile_name; ?>" >
<?php
global $suggested_friends_profile_id;
$member_dir = dirname(dirname(dirname(__FILE__))) . "/members/" . $suggested_profile_id ."/smalll_" . $suggested_profile_photo;
if(file_exists($member_dir)){
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/members/<?php echo $suggested_profile_id; ?>/smalll_<?php echo $suggested_profile_photo; ?>" width="50" height="50">
<?php
} else {
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/assets/images/default.jpg" width="50" height="50">
<?php
}
?>
</a>
</div>
<div class="col-md-8 pad0">
<?php echo $suggested_profile_name; ?>
<span class="f12 gray">271 Mutual Friends</span>
Add as friend
</div>
</div>
<?php
}
}
}
?>
</div>
<!-- ** Suggested Friends -->
What am I missing? Is there any alternative way I can achieve this...thanks!
It looks to me like you're overwriting your $rows variable within the inner select.
foreach($rows as $row){ // <-- first $rows / $row
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT(); <-- $rows overwritten
foreach($rows as $row){
Break your display from your application logic and you won't have such a hard time debugging this kind of thing. Besides, you have a lot of duplicated code and that makes things hard to manage as well as being hard to debug.
Further, you wouldn't have this problem if you ran one query: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id and not only does your code get simpler and your double-foreach loop problem disappear, but your database access will also be a lot more efficient.

Categories