Suppose I have code like bellow
<?php
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)) {
echo '<div class="single">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
}
?>
And I want to append first and last in div class <div class="single"> in every set of 5 images i.e
<div class="single first">
<img src="image1.jpg" alt="title1" />
</div>
<div class="single">
<img src="image2.jpg" alt="title2" />
</div>
<div class="single">
<img src="image3.jpg" alt="title3" />
</div>
<div class="single">
<img src="image4.jpg" alt="title4" />
</div>
<div class="single last">
<img src="image5.jpg" alt="title5" />
</div>
How to do it through loop, please help,
Thanks :)
Using modulo:
<?php
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
$i = 0;
$class = "";
while($row = mysql_fetch_assoc($query)) {
if($i % 5 == 0){
$class = 'first';
}else if($i % 5 == 4){
$class = 'last';
}else{
$class = "";
}
echo '<div class="single ' . $class . '">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
$i++;
}
?>
<?php
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
$x=0;
while($row = mysql_fetch_assoc($query)) {
$x++;
if ( $x == 1 ) { $c = ' first'; }
elseif ( $x == 5 ) { $c = ' last'; }
else { $c = ''; }
echo '<div class="single' . $c . '">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
}
?>
You need to count your iterations, and use the modulo function to get parts of 5:
$sql = "SELECT * FROM images";
$query = mysql_query($sql);
$i = 0;
while($row = mysql_fetch_assoc($query)) {
if($i % 5 == 0)
echo '<div class="single first">';
else if($i % 5 == 4)
echo '<div class="single last">';
else
echo '<div class="single">';
echo '<img src="'.$row['image'].'" alt="'.$row['title'].' " />';
echo '</div>';
$i++;
}
How about this:
$index = 0;
while($row = mysql_fetch_assoc($query)) {
$classes = 'single';
switch ($index % 5) {
case 0:
$classes .= ' first';
break;
case 4:
$classes .= ' last';
break;
}
echo <<<HTML
<div class="$classes">
<img src="{$row['image']}" alt="{$row['title']}" />
</div>;
HTML;
$index++;
}
As a sidenote, have you considered using CSS nth-child property instead?
Related
i have a code for my table pagination.
but now i have a problem. the pagination is showing EVERY page. but i got over 900 pages.
ALSO: i need to use PDO
i want the pagination to work like this:
Image
i dont know how to make this in my already excisting code:
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $oConn->prepare($sql);
$pagination_statement->execute();
$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
} else {
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$pdo_statement = $oConn->prepare($query);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
I dont see your table and your query in your question so I will give 2 complete examples tested on my demo site, you need to change it to your own variables.
Solution 1.
Here is the example pagination for items belong to a category, for simple pagination :
$perpage = "3";
//This limit of the page to show on each page
$n_stmt = $pdo->prepare("SELECT * FROM categories");
$n_stmt->execute();
$total_posts = $n_stmt->rowCount();
$total_pages = ceil($total_posts/$perpage);
$page = !empty($_GET['page']) && $_GET['page'] ? (int) $_GET['page'] : 1;
if($page < 1) $page = 1;
if($page > $total_pages) $page = $total_pages;
$limit = ($page - 1) * $perpage;
$pag_limit = 10;
$stmt = $pdo->prepare("SELECT * FROM categories ORDER BY created DESC LIMIT :limit, :perpage");
$stmt->bindValue(":limit",$limit, PDO::PARAM_INT);
$stmt->bindValue(":perpage",$perpage, PDO::PARAM_INT);
$stmt->execute();
while($news = $stmt->fetch(PDO::FETCH_ASSOC)){
// Do what ever you want here
}
And pagination:
<div class="pagination">
<?php if($page >1){?>
First
Preview
<?php } for($i = $page - $pag_limit; $i < $page + $pag_limit + 1; $i++){
if($i > 0 and $i <= $total_pages){
if($i == $page){?>
<?php echo $i;?>
<?php }else{?>
<?php echo $i;?>
<?php
}
}
?>
<?php } ?>
<?php if($page != $total_pages){?>
next
Last
<?php } ?>
</div>
Solution 2.
Here is pagination for search result with your codes, as I said this code works on my demo site you need to change your own variables and its in pdo:
define("ROW_PER_PAGE",10);
//this goes on top of your page
require_once("db.php");
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
$search_keyword = htmlspecialchars(strip_tags($_POST["search"]["keyword"]), ENT_QUOTES);
}
$sql = 'SELECT * FROM posts WHERE title LIKE :keyword OR descriptions LIKE :keyword OR subject LIKE :keyword ORDER BY id DESC ';
/* Pagination Code starts */
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $pdo->prepare($sql);
$pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pagination_statement->execute();
$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div class=\"pagination\">";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= "<input type=\"submit\" name=\"page\" value=" . $i . " class=\"btn-page current\" />";
} else {
$per_page_html .= "<input type=\"submit\" name=\"page\" value=" . $i . " class=\"btn-page\"/>";
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$pdo_statement = $pdo->prepare($query);
$pdo_statement->bindValue(":keyword", "%" . $search_keyword . "%", PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
Your html part with pagination at the bottom and your result in form same as you can see
<form name="frmSearch" action="search/" method="post">
<div class="searchf">
<input type="text" name="search[keyword]" class="field" value="<?php echo $search_keyword; ?>" id="keyword" maxlength="25">
<input type="submit" name="submit" class="searchf-btn" value="Ara">
</div>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<div class="news_box">
<a href="<?php echo htmlspecialchars($row["news_url"]);?>/" title="<?php echo htmlspecialchars($row["title"]);?>">
<div class="title"><h2><?php echo htmlspecialchars($row["title"]);?></h2></div>
<div class="image">
<img src="images/posts/<?php echo htmlspecialchars($row["img"]);?>" alt="<?php echo htmlspecialchars($row["title"]);?>"/></div>
<div class="spot"><?php echo htmlspecialchars($row["subject"]);?></div>
</a>
</div>
<?php
}
}
?>
<div class="cl"> </div>
//Here is pagination
<?php echo $per_page_html; ?>
</form>
I am using seo urls in demo, you need to set htaccess for links, or change pagination links like so : your_page.php?page=$i.
Both tested on my demo site and working, I used some filtering functions you can remove them.
im struggling with php while loop. The goal is to get the quote from the database and display it with 3 columns on each row. As it look now the while loop is displaying one column each row. How should i correct this problem?
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$row = $ip['id'];
$row = $ip['quote'];
$row = $ip['topic'];
$row = $ip['author'];
$nr = 0;
$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc() ) {
$nr++;
echo
"<div class='container row'>
<div class='col s12 m6 l4 z-depth-1'>
<div class='card-panel grey darken-4 white-text center'><h5>Citat: ". $row["id"] ."</h5></div> <pre class='flow-text black-text' wrap='soft'>" ."<p class=''>Författare: ". $row["author"] ."</p>"
. "<p class=''>Citat: ". $row["quote"] ."</p>" . $row["topic"] ."</pre>
<div class='content_wrapper'>
<h4>Vote </h4>
<div class='voting_wrapper' id='". $row["id"] ."'>
<div class='voting_btn'>
<div class='up_button'> </div><span class='up_votes'>0</span>
</div>
<div class='voting_btn'>
<div class='down_button'> </div><span class='down_votes'>0</span>
</div>
<br>
</div>
</div>
</div>
</div>";
}
}else {
echo "0 results";
}
$conn->close();
?>
You could do something like this:
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//DO YOU NEED THESE VARIABLES? I DON'T SEE THEIR USE HERE... BESIDES, $row DOES NOT EXIST YET...
$row = $ip['id'];
$row = $ip['quote'];
$row = $ip['topic'];
$row = $ip['author'];
$nr = 0;
$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$output = "";
while($row = $result->fetch_assoc() ) {
$topic = trim($row["topic"]);
$quote = trim($row["quote"]);
$author = trim($row["author"]);
$id = trim($row["id"]);
$output .= injectNColumnWrapper(3, $nr, "container row", $nr);
$output .="<div class='col s12 m6 l4 z-depth-1'>";
$output .="<div class='card-panel grey darken-4 white-text center'>";
$output .=" <h5>Citat: {$id}</h5>";
$output .="</div>";
$output .="pre class='flow-text black-text' wrap='soft'>";
$output .="<p class='flow-text-p author'>Författare: {$author}</p>";
$output .="<p class='flow-text-p citat'>Citat: {$quote}</p>";
$output .="<p class='flow-text-p topic'>{$topic}</p>";
$output .="</pre>";
$output .="<div class='content_wrapper'>";
$output .="<h4>Vote </h4>";
$output .="<div class='voting_wrapper' id='vote-{$id}'>";
$output .="<div class='voting_btn'>";
$output .="<div class='up_button'> </div>";
$output .="<span class='up_votes'>0</span>";
$output .="</div>";
$output .="<div class='voting_btn'>";
$output .="<div class='down_button'> </div>";
$output .="<span class='down_votes'>0</span>";
$output .="</div>";
$output .="<br>";
$output .="</div>";
$output .="</div>";
$output .="</div>";
$nr++;
}
$output .= "</div>";
echo $output;
}else {
echo "0 results";
}
$conn->close();
function injectNColumnWrapper($cols_per_row, $closePoint, $cssClass="container row", $nthElem=""){
$blockDisplay = "";
if( ($closePoint == 0) ){
$blockDisplay = "<div class='" . $cssClass . " container_nr_" . $nthElem . "'>" . PHP_EOL;
}else if( ($closePoint % $cols_per_row) == 0 && ($closePoint != 0) ){
$blockDisplay = "</div><div class='" . $cssClass . " container_nr_" . $nthElem . "'>" . PHP_EOL;
}
return $blockDisplay;
}
?>
You should have 3 Columns per Row like so:
<div class="container">
<div class="col s12 m6 l4 z-depth-1">Column 1</div>
<div class="col s12 m6 l4 z-depth-1">Column 2</div>
<div class="col s12 m6 l4 z-depth-1">Column 3</div>
</div>
I hope this helps a little bit...
Try it:-
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$row = $ip['id'];
$row = $ip['quote'];
$row = $ip['topic'];
$row = $ip['author'];
$nr = 0;
$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);
$chngrow=0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc() ) {
$nr++;
$chngrow = $chngrow + 1;
echo
"<div class='container row'>
<div class='col s12 m6 l4 z-depth-1'>
<div class='card-panel grey darken-4 white-text center'><h5>Citat: ". $row["id"] ."</h5></div> <pre class='flow-text black-text' wrap='soft'>" ."<p class=''>Författare: ". $row["author"] ."</p>"
. "<p class=''>Citat: ". $row["quote"] ."</p>" . $row["topic"] ."</pre>
<div class='content_wrapper'>
<h4>Vote </h4>
<div class='voting_wrapper' id='". $row["id"] ."'>
<div class='voting_btn'>
<div class='up_button'> </div><span class='up_votes'>0</span>
</div>
<div class='voting_btn'>
<div class='down_button'> </div><span class='down_votes'>0</span>
</div>
<br>
</div>
</div>
</div>
</div>";
$mod = ($chngrow % 3);
echo ($mod==0) ? "<br>" : "";
}
}else {
echo "0 results";
}
$conn->close();
?>
Elaborating #RuchishParikh comment, which already contains the core of the solution:
$nr = 0;
while ($row = $result->fetch_assoc()) {
$nr++;
if ($nr % 3 == 0) {
echo "<div class='container row'>\n"; # start of row
}
echo "<div class='col s4 m6 l4 z-depth-1'>\n"; # start of column
echo " ...\n";
echo "</div>\n"; # end of column
if ($nr % 3 == 0) {
echo "</div>\n"; # end of row
}
}
Try like this,
$nr =0;
while($row = $result->fetch_assoc() ) {
$nr++;
if(($count-1)%3==0) echo '<div class="row">';
//Add your content units here.
if(($count)%3==0) echo '</div>';
}
increment a counter variable for every loop. open table tag and tr tag outside of the loop. the while loop should have td tag alone. if count%3==0 then close the tr tag and open a new tr tag.
$i=0
?><table><tr><?
while(condition)
{
$i++;
if($i%3==0)
{
?></tr><tr><?
}
?><td>your data</td><?
}
?></tr></table><?
You messed up with the variables.
Don't need to declare $row below:
$row = $ip['id'];
$row = $ip['quote'];
$row = $ip['topic'];
$row = $ip['author'];
As stated here Why shouldn't I use mysql_* functions in PHP? don't use mysql_* functions.
You need to fetch 3 rows and place them inside one container.
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$db = new PDO("mysql:host=$servername;dbname=$dbname;charset=UTF-8",
$username
$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$nr = 0;
$stmt = $db->query('SELECT * FROM table');
while($row1 = $stmt->fetch(PDO::FETCH_ASSOC)) {
$row2 = $stmt->fetch(PDO::FETCH_ASSOC);
$row3 = $stmt->fetch(PDO::FETCH_ASSOC);
$rows[] = $row1;
if ($row2) $rows[] = $row2;
if ($row3) $rows[] = $row3;
echo "<div class='container row'>\n"
foreach($rows as $row) {
$nr++;
$id_ = $row["id"];
$author_ = $row["author"];
$quote_ = $row["quote"];
$topic_ = $row["topic"];
echo
"<div class='col s12 m6 l4 z-depth-1'>
<div class='card-panel grey darken-4 white-text center'><h5>Citat:$id_ </h5></div>
<pre class='flow-text black-text' wrap='soft'>
<p class=''>Författare: $author_</p>
<p class=''>Citat: $quote_</p>
$topic_
</pre>
<div class='content_wrapper'>
<h4>Vote </h4>
<div class='voting_wrapper' id='$id_'>
<div class='voting_btn'>
<div class='up_button'> </div><span class='up_votes'>0</span>
</div>
<div class='voting_btn'>
<div class='down_button'> </div><span class='down_votes'>0</span>
</div>
<br />
</div>
</div>
</div>";
}
echo "</div>\n";
$rows = null;
}
if ($nr == 0){
echo "0 results";
}
?>
Try this
$i=0
?><table><tr><?
while(condition)
{
$i++;
if($i%3==0)
{
?></tr><tr><?
}
?><td>your data</td><?
}
?></tr></table><?
I have review function which I need to run in PHP while loop. But somehow it's not working if I call the function in while loop.
But If I manually put the code (Rating Function) in while then it's working. Can you tell me why ?
Here is the function code:
function rating($star) {
echo "<div class='my_all_star'>";
$total = "";
for ($i=1; $i <= $star; $i++) {
$total .= "<img src='../images/star.png'/> ";
}
for ($i=1; $i <= (5 - $star); $i++) {
$total .= "<img src='../images/star_null.png'/> ";
}
if($star > 1 ){
$reviews = " Reviews";
}else{
$reviews = " Review";
}
$total .= ($star) . $reviews;
return $total;
echo "</div>";
}
Update : While Loop:
<?php
$grq = mysqli_query($conn, "SELECT tbl_reviews.*, tbl_users.FName, tbl_users.LName FROM tbl_reviews LEFT JOIN tbl_users ON tbl_reviews.reviewerID = tbl_users.UserID WHERE tbl_reviews.ProductID = '$pid' ");
while($allreviews = mysqli_fetch_array($grq)){
$review_text = inputvalid($allreviews['ReviewText']);
$review_date = inputvalid($allreviews['ReviewDate']);
$st_rating = (int) $allreviews['StarRating'];
$fname = inputvalid($allreviews['FName']);
$lname = inputvalid($allreviews['LName']);
?>
<div class="single_main_reviews">
<div class="col-sm-2 text-center rev_author_info">
<img src="../images/avater-1.png" alt="" />
<p>Reviewed by</p>
<?php echo $fname ." ". $lname; ?>
</div>
<div class="col-sm-10 rev_author_content">
<div class="rev_author_content_head">
<div class="">
<?php
echo "<div class='my_all_star'>";
$total = "";
$star = $st_rating;
for ($i=1; $i <= $star; $i++) {
$total .= "<img src='../images/star.png'/> ";
}
for ($i=1; $i <= (5 - $star); $i++) {
$total .= "<img src='../images/star_null.png'/> ";
}
if($star > 1 ){
$reviews = " Reviews";
}else{
$reviews = " Review";
}
$total .= ($star) . $reviews;
echo $total;
echo "</div>";
?>
</div>
<div class="pull-right"><span class="review-date"><?php echo $review_date; ?></span></div>
</div>
<div class="clear"></div>
<p><?php echo $review_text; ?></p>
</div>
</div>
<?php
}
?>
dont echo and return data at the same time.
function rating($star) {
$total = "<div class='my_all_star'>";
for ($i=1; $i <= $star; $i++) {
$total .= "<img src='../images/star.png'/> ";
}
for ($i=1; $i <= (5 - $star); $i++) {
$total .= "<img src='../images/star_null.png'/> ";
}
if($star > 1 ){
$reviews = " Reviews";
}else{
$reviews = " Review";
}
$total .= ($star) . $reviews;
$total .="</div>";
return $total;
}
I am wanting to use 8 images from my database and load them into a HTML Table. I would like to display 4 images per table row, but however when i run my code below, i seem to get all images in one table row. Any help would be great. Thank you in advance.
$count = $get->rowCount();
if ($count > 0)
{
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
if ($i == 0)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
put this code directly it will works...
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
$i++;
if ($i == 1)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i >= 4)
{
echo '</tr>';
$i = 0;
};
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
In your current code, you're verifying if the $i variable is == 0 in order to add a row. But you're always increasing it's value at the end, even after you set it to zero in the if($i > 4)
Change these lines
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
To this:
if ($i > 4)
{
$i = 0;
echo '</tr>';
}
else
$i++;
I have two rank in my forum Admin = 1 and User = 0 Then this code is doing so the admin get red name.
if($info['rank'] == 1) { $x= "<a class='admin'>".$last_user."</a>";
} else { $x= "<a class='user'>".$last_user."</a>"; }
But everyone is getting red name..
use the $x where you want to wish.
<?PHP
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res)) {
if($info[rank] == 1) { $x= "<div class='admin'>".$last_user."</div>"; } else { $x="<div>".$last_user."</div>"; }
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
$topics .="
<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
<div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'><i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)." Comments <i class='icon-user icon-1'>
</i>".$last2_user."
$x
<i class='icon-calendar'> </i> ".convertdate($date)."</div>
</div>";
$topics .= "<div class='right_cat'>
<div class='face'>
<img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
<div class='comments_cat'>
<div class='comments_top'>Comments</div>
<div class='comments'>".topicreplies($cid, $tid)."</div></div>
<div class='views_cat'>
<div class='views_top'>Views</div>
<div class='views'>".$views."</div></div>
</div></li>";
}
echo $topics;
} else {
echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}
?>
I have make the variable for you in the top.
Hope it work
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res)) {
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
$topics .= "<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'><div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'><i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)." Comments <i class='icon-user icon-1'>
</i>".$last2_user." ";
Syntax error is there at last line i.e ".$last2_user."
Just close the quote at " ";
This will solve your problem.
It looks like you are trying to insert an if statement inside the string you are creating. You can't do that as far as I know. However, the solution is easy. You end the string, insert your if statement, then append the rest of what you wanted. Like this:
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res))
{
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "")
{ $last_user = getusername($row['topic_creator']); }
else
{ $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "")
{ $last2_user = 'Started by'; }
else
{ $last2_user = 'Most recent by'; }
$topics .= "<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
<div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'>
<i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)."
Comments <i class='icon-user icon-1'>
</i>".$last2_user." ";
// The string above is ended - and you insert your if statement.
// I changed the echo to an append to the string btw.
if ($info['rank'] == 1) { $topics .= "<div class='admin'>".$last_user."</div>"; } else { $topics .= "<div>".$last_user."</div>"; }
// Now you get to continue appending to the string.
$topics .= " <i class='icon-calendar'> </i> ".convertdate($date)."</div>
</div>";
$topics .= "<div class='right_cat'>
<div class='face'>
<img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
<div class='comments_cat'>
<div class='comments_top'>Comments</div>
<div class='comments'>".topicreplies($cid, $tid)."</div></div>
<div class='views_cat'>
<div class='views_top'>Views</div>
<div class='views'>".$views."</div></div>
</div></li>";
}
echo $topics;
} else {
echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}
The following code is what I have in the example provided above, and Here
<style type="text/css">
.user { font-weight:900; color: #000; text-decoration: none !important; }
.admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<form action="" method="POST">
Input number and press enter:
</br>User = 0 Admin = 1</br>
<input name="rank" id="rank" type="text" />
</form>
<?php
if (isset($_POST['rank'])) {
$info['rank'] = $_POST['rank'];
if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; }
else { $x= "<a class='user'>User</a>"; }
echo $x;
}
?>
As I mentioned in my comment above your code works fine, unless there is something going on elsewhere that we cant see with what you have given us.
And without a form.
<style type="text/css">
.user { font-weight:900; color: #000; text-decoration: none !important; }
.admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<?php
$info['rank'] = 1;
if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; }
else { $x= "<a class='user'>User</a>"; }
echo $x;
?>