How to prevent duplicate results in For Each loop? - php

I am using a foreach loop to look at custom taxonomies associated with my custom post type. The issue I am having is that I am getting multiple buttons to display for posts that have more than one tax term selected.
What I would like to have happen is the loop search for one or more of the "age/grades" and then display a single button with the correct text and link. However, I am getting one button for each grade selected for the program (e.g. a Grade 3-6 program has four buttons: one for each applicable grade).
Any idea on how to prevent the duplicates?
Here is my current code:
<?php
$agegroup = wp_get_post_terms(get_the_ID(), 'camper_grade');
if ($agegroup) {
foreach ($agegroup as $group) {
if ($group->slug == 'age-2' || 'age-3' || 'age-4') { ?>
<a href="/preschool">
<div class="blue-btn">
More Preschool Camps
</div>
</a> <?php ;
}
elseif ($group->slug == '1st-grade' || '2nd-grade' || '3rd-grade' || '4th-grade' || '5th-
grade' || '6th-grade') { ?>
<a href="/grades-k-6">
<div class="blue-btn">
More Grade K-6 Camps
</div>
</a> <?php ;
}
elseif ($group->slug == '7th-grade' || '8th-grade' || '9th-grade' || '10th-grade' || '11th-
grade' || '12th-grade' ) { ?>
<a href="/teen">
<div class="blue-btn">
More Teen Adventures
</div>
</a> <?php ;
}
}
}
?>

Use separate foreach loops and break when it's found.
Your || is also not working how you want it to. You should use in_array which correctly compares the same value to many others:
<?php
$agegroup = wp_get_post_terms(get_the_ID(), 'camper_grade');
if ($agegroup) {
foreach ($agegroup as $group) {
if (in_array($group->slug, ['age-2', 'age-3', 'age-4'])) { ?>
<a href="/preschool">
<div class="blue-btn">
More Preschool Camps
</div>
</a> <?php ;
break;
}
}
foreach ($agegroup as $group) {
if (in_array($group->slug, ['1st-grade', '2nd-grade', '3rd-grade', '4th-grade', '5th-grade', '6th-grade'])) { ?>
<a href="/grades-k-6">
<div class="blue-btn">
More Grade K-6 Camps
</div>
</a> <?php ;
break;
}
}
foreach ($agegroup as $group) {
if (in_array($group->slug, ['7th-grade', '8th-grade', '9th-grade', '10th-grade', '11th-grade', '12th-grade'])) { ?>
<a href="/teen">
<div class="blue-btn">
More Teen Adventures
</div>
</a> <?php ;
break;
}
}
}
?>

You need to add a flag to indicate whether the button has been displayed already for that group, and only output the button if it hasn't. Note that your logical conditions are incorrect, you need to compare the slug with each value individually (or better yet, use in_array). For example:
if ($agegroup) {
$preschool = $grades_k_6 = $teen = false;
foreach ($agegroup as $group) {
if (in_array($group->slug, array('age-2', 'age-3', 'age-4')) && !$preschool) { ?>
<a href="/preschool">
<div class="blue-btn">
More Preschool Camps
</div>
</a> <?php ;
$preschool = true;
}
elseif (in_array($group->slug, array('1st-grade', '2nd-grade', '3rd-grade', '4th-grade', '5th-grade', '6th-grade')) && !$grades_k_6) { ?>
<a href="/grades-k-6">
<div class="blue-btn">
More Grade K-6 Camps
</div>
</a> <?php ;
$grades_k_6 = true;
}
elseif (in_array($group->slug, array('7th-grade', '8th-grade', '9th-grade', '10th-grade', '11th-grade', '12th-grade')) && !$teen) { ?>
<a href="/teen">
<div class="blue-btn">
More Teen Adventures
</div>
</a> <?php ;
$teen = true;
}
}
}

Related

PHP logic "and" "&&" for WordPress site restriction

I have two different user roles on my Website: Employer and Candidate. Every one had a type of profile but the profile from Candidate should see only employers and nobody else.
So I want a restriction in Wordpress like:
Employer CAN see Candidate
Candidate CAN'T see other Candidate
Candidate CAN see own Profile
This is controlled by a plugin but it seems to be broken BECAUSE:
Employer CAN see Candidate
Candidate CAN see other Candidate
Candidate CAN'T see own Profile
In the .php file from candidate profile is this code:
<?php
if (!$show_candidate_public_profile) {
if ($candidate->get_public_account() || get_current_user_id() == $candidate->get_author_id()) {
$check = 1;
} else {
$check = 2;
}
} else {
if (is_user_logged_in()) {
if ($show_candidate_public_profile == 2 && get_current_user_id() == $candidate->get_author_id()) {
if ($user->is_employer() && $candidate->get_public_account()) {
$check = 3;
} else {
$check = 4;
}
} else {
if ($candidate->get_public_account() || get_current_user_id() == $candidate->get_author_id()) {
$check = 1;
} else {
$check = 2;
}
}
} else {
$check = 0;
}
}
and the code for results right after code from above:
if (!$check) {
?>
<div class="iwj-alert-box">
<div class="container">
<span>
<?php echo sprintf(__('You must be logged in to view this page. Login here', 'iwjob'), add_query_arg('redirect_to', $candidate->permalink(), $login_page_id)); ?>
</span>
</div>
</div>
<?php
} else {
if ($check == 2) {
?>
<div class="iwj-alert-box">
<div class="container">
<span>
<?php echo esc_html__('This profile is not public now.', 'iwjob'); ?>
</span>
</div>
</div>
<?php } elseif ($check == 4) {
?>
<div class="iwj-alert-box">
<div class="container">
<span>
<?php echo esc_html__('This profile is not public or only employers can see.', 'iwjob'); ?>
</span>
</div>
</div>
<?php } else {
?>
<div class="iw-parallax" data-iw-paraspeed="0.1" style="background-image: url('<?php echo esc_url($cover_image_url); ?>');"></div>
<div class="iw-parallax-overlay"></div>
<div class="content-top">
<div class="container">
<div class="info-top">
<div class="candidate-logo">
The check 3 is the only one check showing profile.
I tried to change the "&&" "||" "==" but i can't figure it out how this logic work.
So much php is too much for me. I have asked the Plugin creator but I am always waiting 5 days to reply and I need it now.
I would be very happy if someone would help me with this.
Thank you very much!
Martin
This code should work according to the behavior you have described(of course it will depend on the good functionality of your plugin). I had to take off some conditions since I don't know what they are and also you didn't provide further details, if you need them you have to add later, but this is quite simple and this code is much more readable.
First portion:
<?php
if(!is_user_logged_in())
$check=false; //if user is not logged in check is false
else
{
//check if user is employer or if is the profile owner
if ($user->is_employer() || get_current_user_id() == $candidate->get_author_id())
$check = 1; //sets 1 if allowed
else
$check = 2; //sets 2 if denied
}
?>
Second portion:
if (!$check) //is check false? then show login message
{
?>
<div class="iwj-alert-box">
<div class="container">
<span>
<?php echo sprintf(__('You must be logged in to view this page. Login here', 'iwjob'), add_query_arg('redirect_to', $candidate->permalink(), $login_page_id)); ?>
</span>
</div>
</div>
<?php
}
else //check it's not false, so do more tests
{
if ($check == 2) //if equals 2 then shows access denied message
{
?>
<div class="iwj-alert-box">
<div class="container">
<span>
<?php echo esc_html__('This profile is not public now or only employers can see.', 'iwjob'); ?>
</span>
</div>
</div>
<?php
}
elseif($check == 1) //user is profile owner or is an employer, show everything
{
?>
<div class="iw-parallax" data-iw-paraspeed="0.1" style="background-image: url('<?php echo esc_url($cover_image_url); ?>');"></div>
<div class="iw-parallax-overlay"></div>
<div class="content-top">
<div class="container">
<div class="info-top">
<div class="candidate-logo">
If you want more tests, like about profile being public or not, you really have to provide more information. I hope it can help you.

How to change <div> property of certain content panel in a php webpage?

<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="row text-center">
<?php
if($_GET[id] == 0)
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
while($rowsb = mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname = 'whitebg';
} else {
$classname = 'bg-blue co-white';
}
?>
<div class="col-md-4 mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
I am working on a dynamic website. here is the code of a particular page. In this page there is a div section with class="col md-4". If the number of content panel in that division appears to 5 or 7 in that case I want only the last panel (i.e 5th and 7th) to be in full column (col-12). What should I do?
Since you are using col-md-4, 3 divs will be shown each row. So I think what you are looking for is a way to make the last div full width if its going to be alone in its row. In that case, you will need to make it col-md-12 on 4th (not 5th) and 7th and 10th records and so on. This is exactly what the following code does,
I think what you want to do is show the
<?php
if($_GET[id] == 0)
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
$number_of_records = mysql_num_rows($querysb);
$counter = 1;
while($rowsb=mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname= 'whitebg';
}else{
$classname= 'bg-blue co-white';
}
//if($number_of_records %3 ==1 && $counter == $number_of_records)
if(($number_of_records == 5 || $number_of_records == 7) && $counter == $number_of_records)
$col_class = "col-md-12";
else
$col_class = "col-md-4";
?>
<div class="<?php echo $col_class;?> mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php
$counter++;
} ?>
</div>
</div>
</div>

PHP loop rating bar error

I have a 5 point rating system I am trying to change up how it displays the rating.
So before it would be 5 dots all white if not rated, then color in the ratings blue.. I would display these dots via fontawesome glyphs. So it would output 5 of them.
But now i'm trying to change it where the 5 point rating would display a progress bar and the 5 points be used as percentages for the length of progress.
Sorry if i am vague, I can explain more if needed. It currently displays all the bars and rating values, but just once. So it's not functioning the way I want it to.
<?php if(count($languages) > 0) { ?>
<div class="col-md-6">
<ul class="no-bullets">
<?php foreach($languages as $index => $language) { ?>
<li>
<span class="skillset-title"><?= $language->title; ?> (<?= $language->endorsement; ?>)</span>
<span class="skillset-rating">
<?php
$levelpercentage = 0;
for($stars == 1; $stars <= 5; $stars++) {
if ($stars == 5):
echo $levelpercentage = 100;
elseif ($stars == 4):
echo $levelpercentage = 80;
elseif ($stars == 3):
echo $levelpercentage = 60;
elseif ($stars == 2):
echo $levelpercentage = 40;
elseif ($stars == 1):
echo $levelpercentage = 20;
endif;
?>
<div class="progress-bar blue stripes">
<span style="width: <?= ($language->level >= $stars) ? $levelpercentage : '0'; ?>%;"></span>
</div>
<?php } ?>
</span>
</li>
<?php if(ceil(count($languages) / 2) == $index + 1) { ?>
</ul>
</div>
<div class="col-md-6">
<ul class="no-bullets">
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<div class="alert alert-warning">
No languages were found!
</div>
<?php } ?>
It currently displays all the bars and rating values
That's because you assign and echo your bars and rating values in a for loop.
Your $stars variable is assigned every value from 1 to 5, displaying the bar at each step. Simply remove your for loop and it should work fine (provided you use an already defined $stars variable)
Bonus : Because I'm in a good mood, I have to tell you there's a simpler way to build your $levelpercentage variable than using an if...else for each value from 1 to 5, just use :
$levelpercentage = $stars * 20;
Thank you! That really helped me out. Was trying to simplify what I was trying to do. This is what I did and it seems to be working.
<?php if(count($languages) > 0) { ?>
<div class="col-md-6">
<ul class="no-bullets">
<?php foreach($languages as $index => $language) { $stars = 0; $stars <= 5; $stars++; ?>
<li>
<span class="skillset-title"><?= $language->title; ?> (<?= $language->endorsement; ?>)</span>
<span class="skillset-rating">
<div class="progress-bar blue stripes">
<span style="width: <?= ($language->level >= $stars) ? $language->level * 20 : 0; ?>%;"></span>
</div>
</span>
</li>
<?php if(ceil(count($languages) / 2) == $index + 1) { ?>
</ul>
</div>
<div class="col-md-6">
<ul class="no-bullets">
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<div class="alert alert-warning">
No languages were found!
</div>
<?php } ?>

While loop need to create new div every 5 elements

I've got a while loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.
<?php
$cn = 1;
while($result->next()) {
if ($cn % 5 == 0) {
?>
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php } ?>
<div class="ce_card autogrid-type_cte n5 one_fifth autogrid_mode_auto autogrid <?php echo $class2; ?> <?php echo $class; ?> block">
<div class="card_wrapper">
<a class="download_image" title="<?php echo $result->name; ?>"
<div class="ce_image attribute image">
<div class="ce_image block">
<figure class="image_container">
<img src="<?php echo $imageVar->path; ?>" onerror="this.onerror=null; this.src='files/Intershop/media/images/customers/<?php echo $rest; ?>.png'" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
</div>
</div>
</a>
</div>
</div>
<div class="clear autogrid_clear"></div>
<?php if ($cn % 5 == 0) { ?>
</div>
</div>
<?php
}
$cn++;
?>
<?php } ?>
I hope you guys can help me.
Ok , after ask you in comments I know what's your purpose.
You wanna print div(.autogrid_wrapper .cte .block) before 1st item and close this div after while walk through 5th item and so on.
$cn = 1;
while($result->next()) {
if($cn % 5 == 1) {
//div(.autogrid_wrapper .cte .block)
}
// HTML wraps image
if($cn % 5 == 0) {
//print the close tag of div(.autogrid_wrapper .cte .block)
}
$cn ++;
}
Embed this flow control in your code, I think this will work.
Try with this hope it's helps
<?php
$cn = 1;
while($result->next()) {
if ($cn % 5 == 0) { //check if number is divided by 5 like 5,10,15 etc
//new div'
}
YOUR HTML
if ($cn % 5 == 0) {
//close div'
}
$cn++;
} ?>

I have spent weeks trying to fix my PHP code. I need some guidance

my code is suppose to display multiple movies. However, it is only displaying ONE movie. I know I'm suppose to tell you what I've tried. I've moved code, checked the db, but nothing has changed. The code used to work, but I added some new code and the code is now not working. I then deleted the new code but the problem continued. I have a lot of code and realize this question will just be deleted. This is all I'm asking: If you see something weird, comment on it.
Note: I promise there is no background with the db information and that there is more than one row of information in the db. Thanks!
movies.php
<?php
error_reporting(0);
require "start.php";
require "dropbox_auth.php";
session_start();
if(!$_SESSION["id"]) {
header("location: index.php");
}
$id2 = $_SESSION["id"];
$name = $_GET["name"];
$name2 = $_GET["name2"];
$db2 = new mysqli("127.0.0.1", "root", "", "crave crap");
$one2 = $db2->query("SELECT * FROM users WHERE id='$id2'");
$two2 = $one2->fetch_object();
$three2 = $two2->username;
$four2 = $two2->premium;
if(isset($name)) {
$findName = $db2->query("SELECT * FROM movies WHERE id='$name'");
$fetchName = $findName->fetch_object();
$real_title = $fetchName->title;
echo "<div id='notVerifiedDiv'> <span id='notVerifiedText'> <strong> $real_title </strong> added to liked movies </span> </div> </div>";
$page = $_GET["page"];
$sorting = $_GET["sorting"];
header("Refresh:1; url=http://localhost/Drop%20Box/movies.php?page=".$page."&sorting=".$sorting);
}
if(isset($name2)) {
$findName2 = $db2->query("SELECT * FROM movies WHERE id='$name2'");
$fetchName2 = $findName2->fetch_object();
$real_title2 = $fetchName2->title;
echo "<div id='notVerifiedDiv'> <span id='notVerifiedText'> <strong> $real_title2 </strong> removed from liked movies </span> </div> </div>";
$page = $_GET["page"];
$sorting = $_GET["sorting"];
header("Refresh:1; url=http://localhost/Drop%20Box/movies.php?page=".$page."&sorting=".$sorting);
}
if(!$four2) {
// They are not premium
header("location: premium.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Movies </title>
<link rel="stylesheet" href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css">
<link rel='stylesheet' href='main.css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src='main.js'></script>
</head>
<body>
<!-- Navigation -->
<div id='nav'>
<!-- Profile Wrapper -->
<ul class="profile-wrapper">
<li>
<!-- user profile -->
<div class="profile">
<img src="avatars/default.png" />
<?php echo $three2; ?> <i class="fa fa-caret-down" id="caret-down"></i>
<!-- more menu -->
<ul class="menu">
<li>Sign Out</li>
<li>Sign Out</li>
<li>Sign Out</li>
</ul>
</div>
</li>
</ul>
<!-- End of Profile Wrapper -->
</div>
<!-- End of Navigation -->
<!-- Movie Content -->
<div id='movie_content2'>
<div id='movie_line'>
<div id='movie_line2'></div>
<!-- Dropdown Selection -->
<?php
$sorting = $_GET["sorting"];
$page = $_GET["page"];
if($sorting == "") {
header("location: http://localhost/Drop%20Box/movies.php?page=1&sorting=recent");
}
if($sorting == "recent" || $sorting !== "popular" && $sorting !== "history" && $sorting !== "likes") {
echo '<nav>
<ul id="dropdown_selection">
<li>Most Recent
<ul>
<li> Most Liked </li>
<li> My History </li>
<li> My Likes </li>
</ul>
</li>
</ul>
</nav>';
}
if($sorting == "popular") {
echo '<nav>
<ul id="dropdown_selection">
<li>Most Liked
<ul>
<li> Most Recent </li>
<li> My History </li>
<li> My Likes </li>
</ul>
</li>
</ul>
</nav>';
}
if($sorting == "history") {
echo '<nav>
<ul id="dropdown_selection">
<li>My History
<ul>
<li> Most Recent </li>
<li> Most Liked </li>
<li> My Likes </li>
</ul>
</li>
</ul>
</nav>';
}
if($sorting == "likes") {
echo '<nav>
<ul id="dropdown_selection">
<li>My Likes
<ul>
<li> Most Recent </li>
<li> Most Liked </li>
<li> My History </li>
</ul>
</li>
</ul>
</nav>';
}
?>
<!-- End of Dropdown Selection -->
<?php
// Pagination
$per_page = 10;
$count_total = $db2->query("SELECT * FROM movies");
$pages = $count_total->num_rows;
$total_pages = ceil($pages / $per_page);
if(!isset($_GET["page"])) {
header("location: movies.php?page=1");
} else {
$page = $_GET["page"];
}
$start = (($page - 1) * $per_page);
$sorting = $_GET["sorting"];
if($sorting == "recent" || $sorting !== "popular" && $sorting !== "history" && $sorting !== "likes") {
$movie = $db2->query("SELECT * FROM movies LIMIT $start, $per_page");
}
if($sorting == "popular") {
$movie = $db2->query("SELECT * FROM movies ORDER BY likes DESC LIMIT $start, $per_page");
}
session_start();
$id3 = $_SESSION["id"];
if($sorting == "history") {
$movie = $db2->query("SELECT m.id AS mid,m.photo AS pho, m.destination AS des,m.length AS len,m.length_content AS lenc,m.description AS desa,m.rating AS rat FROM movies m INNER JOIN history h ON h.movie_id = m.id WHERE h.user_id = $id3 ORDER BY h.id LIMIT $start, $per_page");
}
if($sorting == "likes") {
$movie = $db2->query("SELECT * FROM movies m INNER JOIN likes h ON h.number_likes = m.id WHERE h.user = $id3 ORDER BY h.id LIMIT $start, $per_page");
}
$number = 0;
$fetch_movie = $movie->num_rows;
if($fetch_movie > 0) {
while($movie3 = mysqli_fetch_array($movie)) {
$number++;
if($sorting !== "history") {
$id2 = $movie3["id"];
$photo = $movie3["photo"];
$rating = $movie3["rating"];
$destination = $movie3["destination"];
$length = $movie3["length"];
$length_content = $movie3["length_content"];
$description = $movie3["description"];
} else {
$id2 = $movie3["mid"];
$photo = $movie3["pho"];
$destination = $movie3["des"];
$length = $movie3["len"];
$length_content = $movie3["lenc"];
$description = $movie3["desa"];
$rating = $movie3["rat"];
}
$sorting = $_GET["sorting"];
$findLike = $db2->query("SELECT * FROM likes WHERE number_likes='$id2'");
$numLike = $findLike->num_rows;
echo "<div id='descriptive_div' number='$number'> <i class='fa fa-caret-left' id='descriptive_caret' number='$number'></i> <a href='like.php?number=$id2&page=$page'>";
$sorting = $_GET["sorting"];
if($sorting !== "likes") {
if($numLike == 0) {
// Not Liked
echo "<a href='like.php?number=$id2&page=$page&code=1&sorting=$sorting'> <div class='like_button' number='$number'> Like </div> </a>";
} else {
echo "<a href='like.php?number=$id2&page=$page&sorting=$sorting'> <div class='liked_button' number='$number'> Like </div> </a>";
}
} else if($sorting == "likes") {
echo "<a href='like.php?number=$id2&page=$page&sorting=$sorting'> <div class='liked_button' number='$number'> Like </div> </a>";
}
echo "</a> <span id='descriptive_div_text'> $description </span> </div>";
if($length_content) {
// hr & min
echo "<a href='open.php?destination=$destination'> <div class='movie_length' number='$number'> <div id='movie_length_text' number='$number'> $length </div> </div> </a>";
} else {
// min
echo "<a href='open.php?destination=$destination'> <div class='movie_length2' number='$number'> <span id='movie_length_text' number='$number'> $length </span> </div> </a>";
}
$sorting = $_GET["sorting"];
if($rating == "PG-13") {
if($sorting !== "likes") {
if($numLike == 0) {
// Not Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
} else {
// Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating4' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
}
} else if($sorting == "likes") {
// Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating4' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
}
}
if($rating == "PG") {
if($sorting !== "likes") {
if($numLike == 0) {
// Not Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating2' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
} else {
// Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating5' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
}
} else if($sorting == "likes") {
// Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating5' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
}
}
if($rating == "G" || $rating == "R") {
if($sorting !== "likes") {
if($numLike == 0) {
// Not Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating3' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
} else {
// Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating6' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
}
} else if($sorting == "likes") {
// Liked
echo "<a href='open.php?destination=$destination'> <div class='movie_rating6' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
}
}
}
if($numLike) {
// Liked
echo "<a href='open.php?destination=$destination'> <img src='$photo' class='movie_size3' number='$number'> </a>";
} else {
// Not Liked
echo "<a href='open.php?destination=$destination'> <img src='$photo' class='movie_size' number='$number'> </a>";
}
} else {
echo "No Movies to Display";
}
if($fetch_movie !== 0) {
echo "<br>";
for($number=1;$number<=$total_pages;$number++) {
if($page == $number) {
echo '<div class="complete_page">'.$number.'</div>';
} else {
$sorting = $_GET["sorting"];
echo ' <div class="number_page">'.$number.'</div>';
}
}
}
?>
</div>
</div>
<!-- End of Movie Content -->
</body>
</html>
UPDATE
One thing to note is that the ONE movie is from the results of the LAST row in the db and not the FIRST row in the db. Usually, the information only comes from the FIRST db if there is a problem with the while loop. What does it mean if the ONE movie is coming from the last row in the db? I'm trying to provide more info so users can have a better understanding of how to solve this problem...
I have provided an image of my project:
These are some spots for improvement
always put session_start() as the first thing in code, those includes later may use sessions and your code will fail – Muhammed M. just now edit
$name = $_GET["name"]; $name2 = $_GET["name2"]; make sure they are set, replace like this: $name=isset($_GET['name'])? $_GET['name']:''; if $_GET['name'] is set, you will have it in $name, otherwise $name will be empty string ''. Do this for all $_GET, $_POST, or any other variable you just assume it is there, when in fact it might not be.
Try separating HTML from PHP harder. At least you could do is have all PHP related code on top, even if its' foreach that constructs your table, assign it in a variable, and include in you html. Better to have two files: main.php, main_html.php, you get it, something like this.
You have header() after echo(), it won't work, will give error. header() must be called before any output. Move all of your if(..) { header()} to the top of the page. That redirection logic should be the first in your code.
There are other things, but I feel like these are the most critical at this point for you.
And one more thing - please look at other's code, learn how to code properly, and you will love PHP for that. Don't look at some junky code and blame PHP. Download well known PHP frameworks for instance, dig inside learn what PSR-0, PSR-1... etc are. These all will help you become a better coder. And yes - don't give up! Many many years of PHP behind my back, but I am still learning every day ))

Categories