Last row still shows <div> - php

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>

Related

PHP display comma separated GROUP_CONCAT values separately

This question is with reference to my previous question posted here PHP display results equivalent to comma separated values in the database.
I nearly solved the problem. Now I am stuck somewhere else which if solved my issue will be solved.
The problem is:
Please have a look at this code first
<div class="ads-container">
<?php
$cat = $pdo->prepare("SELECT * FROM ads_category");
$cat-> execute();
$i = 0;
while($s = $cat->fetch()){
$ads = $pdo->prepare("SELECT *, GROUP_CONCAT(memberships.mbs_color) FROM advertisements
INNER JOIN memberships ON FIND_IN_SET(memberships.mbs_id, advertisements.ad_memberships)
LEFT JOIN ads_category ON advertisements.ad_category = ads_category.ac_id
WHERE ad_credits >= ac_credits AND ad_category = :cat AND ad_status = 'active'
GROUP BY advertisements.ad_id");
$ads-> bindValue(':cat', $s['ac_id']);
$ads-> execute();
while($a = $ads->fetch()){ extract($a);
?>
<div class="" <?php if($i++ != 0){ echo "style='margin-top: 30px'"; } ?>>
<i class="fa fa-bullhorn" aria-hidden="true"></i> <?php echo $ac_category; ?>
</div>
<div class="col-sm-4">
<div class="adcover">
<div class="ad-title">
<?php echo $ad_title; ?>
</div>
<div class="ad-footer-two"> <?php echo $a['GROUP_CONCAT(memberships.mbs_color)']; // this is giving me the values comma separated perfectly ?>
<span class="membership-indicator" style="background: <?php echo $mbs_color; ?>; margin-top: 4px; margin-left: 5px"></span>
</div>
</div>
</div>
<?php } } ?>
</div>
Now the problem is when I echo <?php echo $a['GROUP_CONCAT(memberships.mbs_color)']; ?> I get the comma separed color values perfectly. But I want to display it as a color not as a word in <span class="membership-incdicator"></span> as exactly as in https://prnt.sc/hvj91s. So, how can I do that? Do I have to use foreach() function here? If yes then please tell me how to write that code. If not, then please tell me how to accomplish this.
This whole thing looks very messy, but explode the list and loop:
$mbs_colors = explode(',', $a['GROUP_CONCAT(memberships.mbs_color)']);
foreach($mbs_colors as $color) {
echo '<span class="membership-indicator" style="background: ' . $color . '; margin-top: 4px; margin-left: 5px"></span>';
}
Also, you can use an alias: SELECT *, GROUP_CONCAT(memberships.mbs_color) AS colors and then use: $a['colors'].
This does beg the question; why GROUP_CONCAT then? I'm not sure but there must be a better query.

Table repeat region

I am pulling two different variables from the database. A name, and an image path. I want to display this on the page to look like this:
|Image 1|Image 2|Image 3|
|Name 1 |Name 2 |Name 3 |
I currently have this code:
<?php do { ?>
<img src="<?php echo $row_UserInfo['image_path'];?>" width="150" height="150"/>
<a href="HorseProfile.php?recordID=<?php echo $row_UserInfo['id']; ?>"style="color:#000000; text-decoration: none; text-align: center;">
<?php echo $row_UserInfo['Name']?></a>
<?php } while ($row_UserInfo = mysql_fetch_assoc($UserInfo)); ?>
I tried to add a break after the image so that the name goes right under it, but then when the next image appears, it goes next to the name before. I know this is probably extremely simple, but this has been a problem for me for a while and if someone could open my eyes and show me what I'm doing wrong, that would be deeply appreciated.
You should enclose your image and link in a div, and then float the div. Here is an example:
<?php do { ?>
<div style="float:left;">
<div>
<img src="<?php echo $row_UserInfo['image_path'];?>" width="150" height="150"/>
</div>
<div>
<a href="HorseProfile.php?recordID=<?php echo $row_UserInfo['id']; ?>"style="color:#000000; text-decoration: none; text-align: center;">
<?php echo $row_UserInfo['Name']?>
</a>
</div>
</div>
<?php } while ($row_UserInfo = mysql_fetch_assoc($UserInfo)); ?>

Entire page repeating with PHP [closed]

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.

How to display recent posts from database into php homepage?

I want to display post details ( title, description, username ) on my home page within a div tag in a proper format like all websites have. The problem I'm facing is that all the data from database are getting displayed as a plain text, one below the other. I am new to php, so please guide me to achieve the result.
Here is my code.
I want to display in this tag:
<div id='display'>
<h3 class='name'></h3>
<h1 class='title'></h1>
<p class='desc'></p>
<p class='cat'></p>
<p class='sub_cat'></p>
</div>
And my php code is:
<?php
$row="";
$link = mysql_connect("localhost","username","password");
mysql_select_db("database");
$query = "SELECT * from posts ORDER by post_id DESC limit 0,5";
$result = mysql_query($query);
$result = mysql_query($query) or die("Query to get blah failed with error:".mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<div id='display'>";
echo "<h3 class='name'>".$row['username']."</h3>";
echo "<h1 class='title' >".$row['post_title']."</h1>";
echo "<p class='cat'>".$row['cat']."</p>";
echo "<p class='sub_cat'>".$row['sub_cat']."</p>";
echo "<p class='desc'>".$row['post_desc']."</p>";
echo "</div>";
}
mysql_close($link);
?>
I think what you are missing is some CSS. In HTML, DIV-Containers are displayed as block elements. So without any configuration, they are displayed one below the other. To get them next to each other, just add some CSS to your page:
<div id='display'>
<h3 class='name'></h3>
<h1 class='title' ></h1>
<p class='desc'></p>
<p class='cat'></p>
<p class='sub_cat'></p>
</div>
<div class="clear"></div>
<style type="text/css">
.name, .title, .desc, .cat, .sub_cat {
float:left;
}
.clear {
clear:both;
}
</style>

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