Entire page repeating with PHP [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I've ran an SQL query to obtain reviews relating to a specific film as part of a film review site.
I've managed to obtain the required results, however the entire top section of the webpage now duplicates in between every record.
I'm really not sure why this could be as I'm a complete beginner with PHP.
I assume it must be a simple syntax problem but I can't see it.
Any help would be great! Thank you.
<!DOCTYPE html>
<?php
session_start();
try{
$conn = new PDO('mysql:host=localhost;dbname=xxx','xxx','xxx');
} catch(PDOException $e) {
echo $e->getMessage();
}
$result = $conn->query("
SELECT *
FROM FILM F, GENRE G, REVIEW R, USER U
WHERE F.GENRE_ID = G.GENRE_ID
AND F.FILM_ID = R.FILM_ID
AND U.USER_ID = R.USER_ID
AND R.FILM_ID = 128
");
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $result->fetch()){
?>
<html>
<head>
<title><? echo $row['TITLE']; ?></title>
<link href="CSS.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="favicon.ico">
<meta name="description" content="A unique, ground-breaking website for all things relating to classical cinema. The Classic revolutionises the way that we see classic cinema, and provides the movie goer with an opportunity to find all the reviews they need!">
<meta name="author" content="Stefan Batterbee">
<meta charset="UTF-8">
</head>
<body>
<div id="page">
<header>
<?php
if(isset($_SESSION['Logged_In']))
{
echo '<br><br>';
echo 'You are logged in!<br>';
echo '<a href="logout.php">
Click here to log out.</a>';
}
else
{
echo '<br>';
echo 'You are not logged in!<br>';
echo 'Click here to log in,<br>';
echo 'or click here to register.';
}
?>
</header>
<nav>
<ul id="navigation">
<li>H O M E </li>
<li>F I L M R E V I E W S </li>
<li>A R T I C L E S</li>
<li>A B O U T U S</li>
</ul>
</nav>
<div id="breadcrumbs">
<a class="link" href="index.php">Home</a> > <a class="link" href="genres(list).php">Reviews</a> > <? echo '<a class="link" href="'.$row ['GENRE_TYPE'].'(list).php">'; echo $row ['GENRE_TYPE']; echo '</a>' ?> > <? echo '<a class="link" href="'.$row ['FILM_ID'].'.php">'; echo $row ['TITLE']; echo '</a>' ?>
</div>
<div id="filminfo">
<img class="greyshadow" src="Images/2001aspaceodyssey.jpg" width="200" height="200" alt="2001" longdesc="Images/2001aspaceodyssey.jpg">
<div id="filminfotext">
<h1><span itemprop="itemreviewed"><? echo $row['TITLE']; ?></span></h1>
<br>
<table width="500" height="80" border="0">
<tr>
<td>Genre:</td>
<td><? echo $row['GENRE_TYPE']; ?></td>
</tr>
<tr>
<td>Release Year:</td>
<td><? echo $row['RELEASE_YEAR']; ?></td>
</tr>
<tr>
<td>Starring:</td>
<td><? echo $row['LEAD_ACTOR']; ?></td>
</tr>
<tr>
<td>Directed by:</td>
<td><? echo $row['DIRECTOR']; ?></td>
</tr>
<tr>
<td>Running Time:</td>
<td><? echo $row['RUNNING_TIME']; ?></td>
</tr>
</table>
</div>
</div>
<div id="viewreviews">
<h2>P R E V I O U S U S E R R E V I E W S</h2>
<span itemscope itemtype="http://data-vocabulary.org/Review-aggregate">
<span itemprop="itemreviewed">2001: A Space Odyssey</span> - Classic Film Reviews
<span itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">
Rating:
<span itemprop="average">9.2</span> out of
<span itemprop="best">10</span>
</span>
based on
<span itemprop="count">6</span> reviews.
</span>
</div>
<div id="synopsis">
<?php
echo '<table width="760" border="0">';
echo '<tr>';
echo '<td rowspan="2">';
echo $row['RATING'];
echo "</td>";
echo '<td>';
echo $row['USERNAME'];
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'; echo $row['COMMENTS']; echo '</td>';
echo '</tr>';
echo '</table>';
}
$conn = null; ?>
<center><img src="Images/review_button.jpg"></center>
</div>
<footer>
<p class="textleft">Created by Stefan Batterbee (2013)</p>
<p class="textright">Click <a class="link" href="https://www.facebook.com/the.classic.cinema.reviews">HERE</a> to access our Facebook page.</p>
</footer>
</div>
</body>
</html>

You are starting your while loop before rendering the header. Move the PHP where you start the loop to the location where you want to start the loop.

Replace this part of your code:
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $result->fetch()){
to:
$row = $result->fetchAll(PDO::FETCH_ASSOC);
There is no reason to loop through the query results and this is the answer to your question of why it's duplicates in between every record.

You have a query that returns data from tables with different items count, this is influencing the results returned. To understand better, var_dump($row) and forget the html for now: you're getting:
TITLE - RELEASE_YEAR - GENRE_TYPE - RATING - USERNAME - COMMENT
all in the same row, repeated times the number of comments. And you're using the single $row to output the whole page. Incidentally, should you have no reviews, you wouldn't even see the page once.
You should
remove the REVIEW table from the first query, and remove the while loop: this is a movie page, you only want to render the movie information once
run another query just before the table where you outupt the RATING (the one 760px wide), and query only the REVIEW table. Then run a while loop to print out all the ratings.
Use different variable names ($ratingrow for example) so you don't lose your mind.

Related

PHP is duplicating results for first three cards of each row

Morning/Afternoon/Evening,
I'm terrible with PHP. I finally got it to connect/pull the information that I wanted.
I used css to setup a grid of 3 cards.
I'm pulling Image/URL, Name, Age, State.
Right now when I load the page the every, with all three cards, hold the exact same information.
Example:
Page:
Ben Ben Ben
Janna Janna Janna
Steven Steven Steven
What I want:
Page:
Ben Janna Steven
Here is my PHP, and below the css.
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Covid-Deaths</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="https://kit.fontawesome.com/1f285a5a86.js" crossorigin="anonymous"></script>
<script src="http://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<h1>You're terrible at this</h1>
<p>Get each picture to be unique</p>
<?php
//database Connection
include 'dbconfig.php';
// retrieving data from table accounts
$query = "SELECT * FROM test_info";
$result = mysqli_query($conn, $query);
?>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<div class="grid-container">
<div class ="card">
<img src="uploaded-images/<?php echo $row['picture'];?>" width="80%"/>
<h4><?php echo $row['names']; echo ", "; echo $row['age'];?></h4>
<p><?php echo " "; echo "State: "; echo $row['state']; ?></p>
</div>
<div class ="card">
<img src="uploaded-images/<?php echo $row['picture'];?>" width="80%"/>
<h4><?php echo $row['names']; echo ", "; echo $row['age'];?></h4>
<p><?php echo " "; echo "State: "; echo $row['state']; ?></p>
</div>
<div class ="card">
<img src="uploaded-images/<?php echo $row['picture'];?>" width="80%"/>
<h4><?php echo $row['names']; echo ", "; echo $row['age'];?></h4>
<p><?php echo " "; echo "State: "; echo $row['state']; ?></p>
</div>
</div>
</div>
<?php
}
}
?>
</body>
</html>
And the CSS
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 20px;
}
Again, I'm super new at this and can't quite figure out how to google the exact words. Would appreciate any help.
Inside the while loop change your code as below:
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<div class="grid-container">
<div class ="card">
<img src="uploaded-images/<?php echo $row['picture'];?>" width="80%"/>
<h4><?php echo $row['names']; echo ", "; echo $row['age'];?></h4>
<p><?php echo " "; echo "State: "; echo $row['state']; ?></p>
</div>
<?php
}
}
?>
Since you are having all state, name, age, image all information in one card and you are having that code in a while loop so you don't need the other cards.
$query = "SELECT * FROM test_info";
So if the above query result returns more than 1 row, then automatically multiple cards will be generated and information would be loaded in that.
remove the extra 2 card container since your are using a loop. Try this tutorials for a better understanding on how to fetch data using php and mysql
https://www.tutorialrepublic.com/php-tutorial/php-mysql-crud-application.php

how to view newest replies from my followed users

i am creating a following system in my website.
now when a user follows lets say three users and each user has lets say 3-4 messages.
when one of the users replies to a new message that has been sent to him i want this message to be the top message to be displayed.
what i am getting now is that the messages will go in stacks for each user even if it is a new message.
here is my php and queries:
$uid=$_SESSION['active_user_id'];
$usersFollowingsQuery = $db->prepare("SELECT mr.*, f.* FROM messages_reply mr INNER JOIN follow f on mr.from_id = f.followed_id
WHERE follower_id = ? ORDER BY follow_id desc");
$usersFollowingsQuery->bindValue(1,$uid);
$usersFollowingsQuery->execute();
if($usersFollowingsQuery->rowCount()==0){
echo"<div class ='forgot'>Follow users to find their content here</div>";
}
else{
echo"<div class ='forgot'>This is your feed</div>";
while($row3 =$usersFollowingsQuery->fetch()){
$followed_id = $row3['followed_id'];
$usersPicQuery = $db->prepare("SELECT * FROM users WHERE id=?");
$usersPicQuery->bindValue(1,$followed_id);
$usersPicQuery->execute();
$row2 = $usersPicQuery->fetch();
$d=$row['date'];
?>
<div Class="inside-card" style="width:95%; margin-top:24px;"
<td>
<div class="msg-body">
</a>
</img>
<p style="font-size:16px; color:#696969"><?php echo $row3['question']; ?></p>
<div class="msg-action">
<div>
<a href=<?php echo "'q=" . $row2['username'] . "'" ;?>> <img src=<?php echo "'" . $row2['picture'] . "'";?> width="32px" height="32px" >
<p ><?php echo "" . $row2['username'] . "" ;?></p></a>
<span class="msg-icon" ><?php echo timeAgo($d); ?></span>
</div>
<p ><?php echo $row3['answer']; ?></p>
</div>
</td>
</tr>
</div>
<?php
}
}
echo"</div>";
how can i modify my query such that whenever one of my followers replies to a new message it becomes the first message to be displayed regardless of who i followed first?
I solved the problem, only needed to add order by date desc

Last row still shows <div>

I'm learning PHP/MySQLi at the minute and I need help with something I just can't seem to find online. I am using <div class="hr"> </div> after every topic in the forum script I am making but I don't want it to display one after the last topic their is, how do I stop my script from displaying it after the last row? I would be grateful for any help and just so I'm a 100% clear.
Here is how it looks:
Topic Title Here
-------------------------------------
Topic Title Here
-------------------------------------
But I want:
Topic Title Here
-------------------------------------
Topic Title Here
My code of where this part is:
<?php
}
$dn2 = mysql_query('select t.id, t.title, t.authorid, u.username
as author,
count(r.id) as replies from topics as t left join topics as r on
r.parent="'.$id.'"
and r.id=t.id and r.id2!=1 left join users as u on u.id=t.authorid
where t.parent="'.$id.'" and t.id2=1 group by t.id order by t.timestamp2 desc');
if(mysql_num_rows($dn2)>0)
{
?>
<table id="main-table">
<tr>
<td id="side">
</td>
<td id="content">
<?php
while($dnn2 = mysql_fetch_array($dn2))
{
?>
<div><a href="read_topic.php?id=<?php echo $dnn2['id']; ?>">
<?php echo htmlentities($dnn2['title'], ENT_QUOTES, 'UTF-8'); ?></a>
- <a class="wtb" href="profile.php?id=<?php echo $dnn2['authorid']; ?>">
<?php echo htmlentities($dnn2['author'], ENT_QUOTES, 'UTF-8'); ?></a></div>
<div class="hr"> </div>
<?php
}
?>
Without seeing your code, an easy way to fix it would be to draw the line first unless it's the first topic
if ($topicNumber != 1) echo ('<div class="hr"> </div>');
<div><?php echo $dnn1['topic']; ?><div>
<? } ?>
You can handle this easily with your css:
<style>
.wrapper div {border-bottom: 1px dashed black}
.wrapper div:last-child{ border-bottom: 0}
</style>
<div class="wrapper">
<div><?php echo $dnn1['topic']; ?><div>
<? } ?>
</div>

php order by 'most recent' 'most liked' 'least like'

I have a post system in place
<?php
/**
Display the results from the database
**/
$q = ("SELECT * FROM threads ORDER BY posted");
$r = mysql_query($q);
if(mysql_num_rows($r)>0): //table is non-empty
while($row = mysql_fetch_assoc($r)):
$net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down
?>
<div class='entry'>
<span class='link'>
<?php echo $row['author']; ?>
<?php $row['posted'] = date("jS M Y h:i",$row['posted']); echo $row['posted']; ?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $row['message']; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<br/>
<div class='message'><?php echo $row['message']; ?><br/></div>
<?php echo "<a href='msg.php?id=$row[id]'/> Comments/Add comments $row[replies]</a>" ?>
<?php echo "Likes: " . $row['votes_up'] . "&nbsp "; echo "Dislikes: " . $row['votes_down'] . "&nbsp"; ?>
</span>
<span class='votes_count' id='votes_count<?php echo $row['id']; ?>'></span>
<span class='vote_buttons' id='vote_buttons<?php echo $row['id']; ?>'>
<a href='javascript:;' class='vote_up' id='<?php echo $row['id']; ?>'></a>
<a href='javascript:;' class='vote_down' id='<?php echo $row['id']; ?>'></a>
<br/>
</span>
</div>
<br/>
<?php
endwhile;
endif;
?>
I want to add text that says 'order by : Most recent | Most liked | least liked '
As you can see i think ive got it already posting most recent by defualt which is what i want.
But what i want also is when you click 'Most liked' & 'least liked' it sorts by 'Vote_up' ( likes) & 'vote_down' (dislikes) all on the same page and shows posts with most likes on them (most liked) and most dislike (least liked)
EDIT***
sorry my question is how can i add 2 functions that when on click sorts by 'most liked' and 'least liked'
in html:
<a href='script.php?order=recent'>Recent</a>
<a href='script.php?order=liked'>Liked</a>
...
in php:
if ($_GET['order'] == 'recent') $order = "posted";
elseif ($_GET['order'] == 'liked') $order = "smth";
...
$q = "SELECT * FROM threads ORDER BY ".$order."";
but actually it's better to use some js framework (i prefer extjs for that) to sort the output on client side
AFAIK you have few choices how to do this:
Have the links pass a $_GET value via URL (causes a page refresh!!), then test for this in the PHP and run the necessary SQL to get the new record order.
Use AJAX to perform the same request asynchronously, with a PHP script to handle the SQL function and return the desired results.
Use a jQuery plugin such as tablesorter to (probably) basically provide the same functionality as that in 2, or via it's own filtering system -- I'm not sure i've not used it!!
My preference would be 1. (i.e a pure PHP + MySQL solution) as this offers the best universal functionality. You could always add javascript / ajax later to make things more swish for more modern browsers and users!

posts overlapping

Now i dont know if this is simple or hard. If its just css or php code i need
But basically i have posting system and users can comment on posts. In the comments page it shows orginal post and one users have left (the comments)
I had one in there and this was fine but i added another and it looked like this...
[1]: http://i.stack.imgur.com/2fIXd.jpg
As you can see its completly different! Heres my code for it...
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
echo "<a href='Untitled9.php'>Go Back...</a>";
?>
<br/><br/>
<div class="message">
<?php
$sql = mysql_query("SELECT * FROM threads WHERE id = '".
mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." $posted"; ?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2"><?php echo " ".$r['message'].""; ?></div>
<?php echo "Likes: ".$r['votes_up']." "; echo "Dislike: ".$r['votes_down']."";>
</div>
<br/>
<hr width="725px">
<?php
echo "<h3>Replies...</h3>"; ?>
<div class="message"><?php
$sql = mysql_query("SELECT * FROM replies WHERE thread = '".
mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." $posted"; ?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2">
<?php echo " ".$r['message']."" ; } ?> </div>
</div>
<hr width="725px">
<form action="newreply.php" method="POST">
Your Name: <input type="text" name="author">
<input type="hidden" value="<?php echo $_GET['id']; ?>" name="thread"><br>
Message:<br><textarea cols="60" rows="5" name="message"></textarea><br>
<input type="submit" value="Post Reply">
</form>
The code looks really messy on here. I tried editing but couldnt get much better.
So bascially what i want to know is how do i prevent this (the overlapping) from happening?
Edit * CSS
.message {
width: 500px;
color: black;
background: white;
padding:8px;
border:1px solid white;
margin:5px auto;
-moz-border-radius:8px;
}
.message2 {
background-color: grey;
}
It looks to me as though everything is posting inside the second php function but i have some code pretty much the same for just the individual post and this displays normally i.e. as many as i want. Im just wondering is there something i need to add/change
Wrong (Your code):
<?php echo " ".$r['message']."" ; } ?> </div>
</div>
Correct:
<?php echo " ".$r['message']."" ; ?> </div>
</div>
<?php } ?>
You were opening multiple DIVs in your while loop but only closing two.
Similarly to Cobra_Fast's reply, it seems that the positioning of your divs seemed to be causing the problem, and also the position of your while loop.
Try replacing the replies section with the following and let me know if it is any better.
<?php
echo "<h3>Replies...</h3>";
$sql = mysql_query("SELECT * FROM replies WHERE thread = '".mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
?>
<div class="message">
$posted = date("jS M Y h:i",$r['posted']);
echo $r['author']." ".$posted;
?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $r['message']; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</div>
<div class="message2">
<?php
echo " ".$r['message'];
?>
</div>
<?php
}
?>

Categories