display different image in same div in php - php

I want to display first image,title and description in col1 div, second in col3 and so on
but in this first image repeat in all div . how to solve this
<?php
include('connection.php');
$perPage=1;
$i=1;
if(isset($_REQUEST['act']) && trim($_REQUEST['act']=='load_data')){
$page=1;
if(!empty($_GET["page"])) {
$page = $_GET["page"];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$msg='';
$q="SELECT * FROM addimages order by id desc limit $start,$perPage ";
$res = mysql_query($q);
$row = mysql_fetch_array($res);
$id = $row['art_id'];
$qe="select banner from images where id=$id";
$rs=mysql_query($qe);
$name=mysql_fetch_array($rs);
//$id=$row['id'];
//$no=mysql_num_rows($res);
if(!$row==""){
$msg .= "<div class='article_index' id='".$row["art_id"]."'><div class='article banner_add'><a href='#'><img height='220' src='./uploads/" . $name['banner'] . "' /></a></div>";
$msg .=" <div class='post_main' >
<div class='col1'>
<div class='post_img'><a href='#'><img class='lazy' data-original='./uploads/" . $row['image'] . "' width='633' height='441' src='./uploads/" . $row['image'] . "' style='display: inline;' /></a>
</div>
<div class='post_contant_main'>
<div class='post_title'><h2><a href='#'>".$row['title']."</a></h2></div>
<div class='post_contant'>".$row['desc']."</div>
</div>
</div>";
$msg .=" <div class='col3'>
<div class='post_img'><a href='#'><img class='lazy' width='299' height='191' src='./uploads/" . $row['image'] . "' style='display: inline;' /></a>
</div>
<div class='post_contant_main'>
<div class='post_title post_rtitle'><h2><a href='#'>".$row['title']."</a></h2></div>
</div>
</div>
<div class='col3'>
<div class='post_img'><a href='#'><img class='lazy' width='299' height='191' src='./uploads/" . $row['image'] . "' style='display: inline;' /></a>
</div>
<div class='post_contant_main'>
<div class='post_title post_rtitle'><h2><a href='#'>".$row['title']."</a></h2></div>
</div>
</div>
<div class='col2'>
<div class='post_img'><a href='#'><img class='lazy' width='461' height='300' src='./uploads/" . $row['image'] . "' style='display: inline;' /></a>
</div>
<div class='post_contant_main'>
<div class='post_title'><h2><a href='#'>".$row['title']."</a></h2></div>
<div class='post_contant'>".$row['desc']."</div>
</div>
</div>
<div class='col2'>
<div class='post_img'><a href='#'><img class='lazy' width='461' height='300' src='./uploads/" . $row['image'] . "' style='display: inline;' /></a>
</div>
<div class='post_contant_main'>
<div class='post_title'><h2><a href='#'>".$row['title']."</a></h2></div>
<div class='post_contant'>".$row['desc']."</div>
</div>
</div>
</div>
</div>
";
}
$msg .="<a class='next' href='#'></a>
<input type='hidden' id='pageno' name='pageno' value=''>";
echo $msg;
}
?>
i want to increase row array to next id and display its content
i am using infinite scroll plugin

you fetch only one top row. If you want to get all rows, you need to loop it thought.
while ($row = $result->fetch_row()) {
$msg .=" <div class='post_main' >
<div class='col1'>
<div class='post_img'><a href='#'><img class='lazy' data-original='./uploads/" . $row['image'] . "' width='633' height='441' src='./uploads/" . $row['image'] . "' style='display: inline;' /></a>
</div>
<div class='post_contant_main'>
<div class='post_title'><h2><a href='#'>".$row['title']."</a></h2></div>
<div class='post_contant'>".$row['desc']."</div>
</div>
</div>";
}

Related

How to echo the comment section according to which post section?

I'm doing about the blog webpage. User can post their status on the blog, so on user profile page, user can review what they posted.
I had meet the problem about the post section and comment section which are seperated.
What I wanted is to display the comment section with the post section.The comment section is display according to the post section.
Example: like facebook, I post a status, and other user comment on that post.
Img 1 is below:
On Img 1, you can see the comment section is not display according to the post section.
Img 2 is below:
This is the example what I want to do.
PHP & HTML code is below:
<div class="feed-card">
$postSql = "SELECT user.firstname, user.lastname, user.profile_photo, post.id ,post.post_contain, post.post_image, post.timestamp
FROM user
INNER JOIN post
ON user.id = post.user_id
WHERE user.id = '".$u_id."'
ORDER BY post.timestamp
DESC";
if ($postStmt = $conn->prepare($postSql)) {
/* execute query */
$postStmt->execute();
$postStmt->bind_result($user_firstname, $user_lastname, $user_profile_photo, $post_id, $post_content, $post_image, $post_timestamp);
$postIdArray = array();
while ($postStmt->fetch()) {
echo "post id: $post_id post content: $post_content";
echo '
<div class="media">
<div class="media-left">
<a class="media-object p-t-5">
<img src="' . $user_profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading">
' . $user_firstname . ' ' . $user_lastname . '
</h4>
<small class="timestamp">' . $post_timestamp . '</small>
<p>' . $post_content . '</p>
</div>
<div class="comment-tools ml35">
<ul>
<li id="' . $post_id . '"><img src="/img/ic_like.png">Like</li>
<li id="' . $post_id . '"><img src="/img/ic_comment-no-apply.png"><span class="unselect">Comment</span></li>
<li id="' . $post_id . '"><img src="/img/ic_noapplyshare.png"><span class="unselect">Share</span></li>
</ul>
</div>
</div>
';
$postIdArray[] = $post_id;
}
$postStmt->close();
foreach ($postIdArray as $value){
getComments($conn,$value);
}
}
function getComments($conn,$postId){
$commentSql = " SELECT id, user_id, user_desc, timestamp FROM comments WHERE post_id = ? ";
if($commentStmt = $conn->prepare($commentSql)){
$commentStmt->bind_param("i", $postId);
$commentStmt->execute();
$commentStmt->bind_result($commentId, $userId, $userComment, $comment_timestamp);
while ($commentStmt->fetch()) {
// echo "user dec: $userComment comment id: $commentId";
echo '
<div class="panel panel-radius">
<div class="panel-heading panel-custom-event custom-donut">
<p><img src="/img/people_liked.png">
</p>
</div>
<div class="panel-body body-panel no-border">
<hr class="custom-hr">
<div class="media-left">
<a href="#">
<img src="' . $row->profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"> ' . $row->firstname . ' ' . $row->lastname . '</h4>
<p>' . $userComment . '</p>
<div class="comment-tools">
<ul id="c_option">
<li id="c_like" value="' . $commentId . '">Like</li>
<li id="" value="' . $commentId . '"><a href="" onclick="return false;"><span class="unselect" id="comment_reply">Reply</span></li>
<li id="" value="' . $commentId. '"><img src="/img/people_liked.png"></li>
<li>
<small class="timestamp">' . $comment_timestamp . '</small>
</li>
</ul>
</div>
</div>
<div class="media m-b-20">
<div class="media-left">
<a href="#">
<img src="' . $user_profile_photo . '" class="reply_image" /></img>
</a>
</div>
<div class="media-body">
<div class="right-addon">
<input type="text" class="form-control addradius" id="reply">
</div>
</div>
</div>
</div>
</div>
';
}
$commentStmt->close();
return $userComment;
}else{
return "error preparing sql";
}
}
Instead of collecting all the post ids in an array and then fetch the comments for each, fetch the comments for each post as you are displaying the posts. I have not been able to test the code but this should work.
<div class="feed-card">
<?php
$postSql = "SELECT user.firstname, user.lastname, user.profile_photo, post.id ,post.post_contain, post.post_image, post.timestamp
FROM user
INNER JOIN post
ON user.id = post.user_id
WHERE user.id = '".$u_id."'
ORDER BY post.timestamp
DESC";
if ($postStmt = $conn->prepare($postSql)) {
/* execute query */
$postStmt->execute();
$postStmt->bind_result($user_firstname, $user_lastname, $user_profile_photo, $post_id, $post_content, $post_image, $post_timestamp);
$postIdArray = array();
while ($postStmt->fetch()) {
echo "post id: $post_id post content: $post_content";
echo '
<div class="media">
<div class="media-left">
<a class="media-object p-t-5">
<img src="' . $user_profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading">
' . $user_firstname . ' ' . $user_lastname . '
</h4>
<small class="timestamp">' . $post_timestamp . '</small>
<p>' . $post_content . '</p>
</div>
<div class="comment-tools ml35">
<ul>
<li id="' . $post_id . '"><img src="/img/ic_like.png">Like</li>
<li id="' . $post_id . '"><img src="/img/ic_comment-no-apply.png"><span class="unselect">Comment</span></li>
<li id="' . $post_id . '"><img src="/img/ic_noapplyshare.png"><span class="unselect">Share</span></li>
</ul>
</div>
</div>
';
//$postIdArray[] = $post_id;
getComments($conn,$post_id);
}
$postStmt->close();
//foreach ($postIdArray as $value){
// getComments($conn,$value);
//}
}
function getComments($conn,$postId){
$commentSql = " SELECT id, user_id, user_desc, timestamp FROM comments WHERE post_id = ? ";
if($commentStmt = $conn->prepare($commentSql)){
$commentStmt->bind_param("i", $postId);
$commentStmt->execute();
$commentStmt->bind_result($commentId, $userId, $userComment, $comment_timestamp);
while ($commentStmt->fetch()) {
// echo "user dec: $userComment comment id: $commentId";
echo '
<div class="panel panel-radius">
<div class="panel-heading panel-custom-event custom-donut">
<p><img src="/img/people_liked.png">
</p>
</div>
<div class="panel-body body-panel no-border">
<hr class="custom-hr">
<div class="media-left">
<a href="#">
<img src="' . $row->profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"> ' . $row->firstname . ' ' . $row->lastname . '</h4>
<p>' . $userComment . '</p>
<div class="comment-tools">
<ul id="c_option">
<li id="c_like" value="' . $commentId . '">Like</li>
<li id="" value="' . $commentId . '"><a href="" onclick="return false;"><span class="unselect" id="comment_reply">Reply</span></li>
<li id="" value="' . $commentId. '"><img src="/img/people_liked.png"></li>
<li>
<small class="timestamp">' . $comment_timestamp . '</small>
</li>
</ul>
</div>
</div>
<div class="media m-b-20">
<div class="media-left">
<a href="#">
<img src="' . $user_profile_photo . '" class="reply_image" /></img>
</a>
</div>
<div class="media-body">
<div class="right-addon">
<input type="text" class="form-control addradius" id="reply">
</div>
</div>
</div>
</div>
</div>
';
}
$commentStmt->close();
return $userComment;
}else{
return "error preparing sql";
}
}
?>
</div>

Show all rows on PHP/sql

I'm doing a small app. The thing is that when I'm running it, it will only show 300 rows when I should have around 2000 that I inserted for the test. It just doesn't shows them. It shows 312 rows, and not the rest.
Here is the code:
<!-- page content -->
<div class="right_col" role="main">
<div class="page-title">
<div class="title_left">
<h3>Raw Inventory</h3>
</div>
<div class="title_right">
<div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Assets List</h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
</li>
<li><a class="close-link"><i class="fa fa-close"></i></a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<h2 class="pull-left"></h2>
Add a new Asset
</div>
<?php
// Include config file
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM t_assets";
if($result = mysqli_query($db, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Asset</th>";
echo "<th>Model</th>";
echo "<th>Status</th>";
echo "<th>Location</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . ucfirst ($row['a_asset']) . "</td>";
echo "<td>" . $row['a_model'] . "</td>";
echo "<td>" . ucfirst($row['a_status']) . "</td>";
echo "<td>" . ucfirst($row['a_location']) . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['a_asset'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>  ";
echo "<a href='update.php?id=". $row['a_asset'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>  ";
echo "<a href='delete.php?id=". $row['a_asset'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
}
// Close connection
mysqli_close($db);
?>
</div>
</div>
</div>
</div>
</body>
</html> </div>
</div>
</div>
</div>
</div>
</div>
Any idea what I can do about this?
This is really my only problem, and I just need to add more text right now.
Or well, if you guys want to give me tips to make a global search function that would also be nice. Like, being able to search for information on all columns. Though I'm quite sure that It's not that hard.
I wonder... I need to investigate how to sort the results by name or creation date too.
Thanks, everyone!

PHP Codeigniter Base_URL

Please help.
Almost finish my final project..
Somehow the gallery meets an error for displaying it's pictures, I thought it's because the base_url
<?php
//FOLDER GALLERY-PART
$dirname = "./assets/images/gallery/";
$images = glob($dirname."*.jpg");
array_multisort(array_map('filemtime', $images), SORT_NUMERIC, SORT_DESC, $images);
foreach($images as $image) {
echo //'<img src="'.$image.'" /><br />'; -- Ini cth untuk tampilkan semua gambar (dlm Garis lurus)
"<div class='col-xs-6 col-sm-4 col-md-3 portfolio-item logos'>
<div class='portfolio-wrapper'>
<div class='portfolio-single'>
<div class='portfolio-thumb'>
<img src='.$image.' class='img-responsive' alt=''>
</div>
<form id='imgtitle' class='kart' action='" . base_url() . "gallery/delete' method='post'>
<div class='portfolio-view'>
<ul class='nav nav-pills'>
<li><a href='#'><i class='fa fa-link'></i></a></li>
<li><a href='.$image.' data-lightbox='example-set'><i class='fa fa-eye'></i></a></li>
<li><a href='#'><i class='fa fa-heart'></i></a></li>
</ul>
</div>
</form>
</div>
<div class='portfolio-info'>
<form id='imgtitle' class='kart' action='" . base_url() . "gallery/delete' method='post'>
<input type = 'hidden' name='delete_file' value='$image' />";?>
<?php if($_SESSION['admin']==1):?>
<button type = 'submit' class='btn btn-danger'><i class='delete fa fa-trash fa-3x'></i></button>
<?php endif; ?>
<?="</form>
</div>
</div>
</div>";
}
?>
How to put base_url on
$dirname
Or any other suggestion?
try this:
<img src='".base_url()$image."' class='img-responsive' alt=''>
$dirname = "./assets/images/gallery/";
$images = glob($dirname."*.jpg");`
echo '<img src="'.$baseurl/$image.'" /><br />'; ?>

PHP show articles in row's and div's repeating

I'm trying to create some div's that are supposed to be some articles in a webshop using bootstrap 4 alpha and PHP.
I think I need modulo (%) for this but I can't achieve what I want, I have read this and tried playing around with it but I can't get it to work, because I don't really understand what it does.
What I need is 1 row, 4 columns, and this repeating. I hope you understand where I'm going.
Here is the HTML example, there could be more rows and div's as you can see in the PHP below it:
<div class="row">
<div class="sm-col-3">
<div class="card-deck-wrapper">
<div class="card-deck">
<div class="card card-outline-secondary">
<img class="card-img-top m-x-auto d-block placeholder" src="empty.png" alt="Product">
<div class="card-block">
<h4 class="card-title">Product</h4>
<p class="card-text">
<span>Omschrijving van het product. </span><br>
<span>€ 1,00</span><br>
<span>Voorraad: 1</span>
</p>
Koop
</div>
</div>
<div class="card card-outline-secondary">
<img class="card-img-top m-x-auto d-block placeholder" src="empty.png" alt="Product">
<div class="card-block">
<h4 class="card-title">Product</h4>
<p class="card-text">
<span>Ik zal hier ook maar wat in knallen zodat niet alle kaarten even groot zijn.</span><br>
<span>€ 1,00</span><br>
<span>Voorraad: 1</span>
</p>
Koop
</div>
</div>
<div class="card card-outline-secondary">
<img class="card-img-top m-x-auto d-block placeholder" src="empty.png" alt="Product">
<div class="card-block">
<h4 class="card-title">Product</h4>
<p class="card-text">
<span>Deze kaart zal ook net even iets groter zijn om het uit te testen want ik snap er geen reet van.</span><br>
<span>€ 1,00</span><br>
<span>Voorraad: 1</span>
</p>
Koop
</div>
</div>
<div class="card card-outline-secondary">
<img class="card-img-top m-x-auto d-block placeholder" src="empty.png" alt="Product">
<div class="card-block">
<h4 class="card-title">Product</h4>
<p class="card-text">
<span>Omschrijving van het product.</span><br>
<span>€ 1,00</span><br>
<span>Voorraad: 1</span>
</p>
Koop
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="sm-col-3">
<div class="card-deck-wrapper">
<div class="card-deck">
<div class="card card-outline-secondary">
<img class="card-img-top m-x-auto d-block placeholder" src="empty.png" alt="Product">
<div class="card-block">
<h4 class="card-title">Product</h4>
<p class="card-text">
<span>Omschrijving van het product.</span><br>
<span>€ 1,00</span><br>
<span>Voorraad: 1</span>
</p>
Koop
</div>
</div>
<div class="card card-outline-secondary">
<img class="card-img-top m-x-auto d-block placeholder" src="empty.png" alt="Product">
<div class="card-block">
<h4 class="card-title">Product</h4>
<p class="card-text">
<span>Omschrijving van het product.</span><br>
<span>€ 1,00</span><br>
<span>Voorraad: 1</span>
</p>
Koop
</div>
</div>
</div>
</div>
</div>
PHP what I have now:
$categorie = mysqli_real_escape_string($mysqli, $_GET['categorie']);
$sql = "SELECT * FROM producten WHERE categorie = '$categorie' AND voorraad > 0";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($result) == 0) {
echo "<div class=\"card card-inverse card-warning text-xs-center\">\n";
echo " <div class=\"card-block\">\n";
echo " <blockquote class=\"card-blockquote\">\n";
echo " <span>Deze categorie bestaat niet!</span>\n";
echo " </blockquote>\n";
echo " </div>\n";
echo "</div>\n";
}
else {
echo "<h3>Categorie: " . ucfirst($_GET['categorie']) . "</h3>\n";
$i = 0;
while ($row = mysqli_fetch_array($result)) {
if ($i % 3 == 0) {
echo "<div class=\"row\">\n";
echo " <div class=\"sm-col-3\">\n";
echo " <div class=\"card-deck-wrapper\">\n";
echo " <div class=\"card-deck\">\n";
}
echo " <div class=\"card card-outline-secondary\">\n";
if ($row['plaatje'] == "") {
echo " <img class=\"card-img-top m-x-auto d-block placeholder\" src=\"empty.png\" alt=\"". $row['naam'] . "\">\n";
}
else {
echo " <img class=\"card-img-top m-x-auto d-block placeholder\" src=\"" . $row['plaatje'] . "\" alt=\"". $row['naam'] . "\">\n";
}
echo " <div class=\"card-block\">\n";
echo " <h4 class=\"card-title\">" . $row['naam'] . "</h4>\n";
echo " <p class=\"card-text\">\n";
echo " <span>" . nl2br($row['omschrijving']) . "</span><br>\n";
echo " <span>€ " . str_replace(".", ",", number_format($row['prijs'], 2)) . "</span><br>\n";
echo " <span>Voorraad: " . $row['voorraad'] . "</span><br>\n";
echo " </p>";
echo " Koop\n";
echo " </div>\n";
echo " </div>\n";
if ($i % 3 == 2) {
echo " </div>\n";
echo " </div>\n";
echo " </div>\n";
echo "</div>\n";
}
if ($i % 3 != 0) {
echo " </div>\n";
echo " </div>\n";
echo " </div>\n";
echo "</div>\n";
}
$i++;
}
It's probably a easy solution but I can't see it!
Thanks for your time.
From the link in your question, this works for me:
$i = 0;
while ($row = mysqli_fetch_array($result)) {
if ($i % 3 == 0){
echo '<div class="row">';
}
echo '<div class="col-lg-4 col-md-4 col-sm-4">';
//Your column data here
echo '</div>';
if ($i % 3 == 2){
//Close the .row div
echo '</div>';
}
$i++;
}
if ($i % 3 != 0){
//Close the .row div in case there aren't enough items left to fill up to 3
echo '</div>';
}
http://www.devchunks.com/web-development/using-the-php-modulus-operator/

Three sql queries into one

I have following problem. I need to create pagination for this results. For create pagination I need to have only one while cycle (one sql query). Now I have three queries because of responsive div. I need preserve these three divs. Any ideas?
Thank you.
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
mysql_query("SET NAMES utf8");
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 0) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p>' . $row['text'] . '</p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
} ?>
<?php
$item_num++;
}
?>
</div>
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 1) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p>' . $row['text'] . '</p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
} ?>
<?php
$item_num++;
}
?>
</div>
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 2) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p>' . $row['text'] . '</p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
} ?>
<?php
$item_num++;
}
?>
</div>
You can try this code, it's helpful. First use mysqli for query.
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
mysql_query("SET NAMES utf8");
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 0) {
//Code
}elseif($item_num % 3 == 1){
//Code
}elseif($item_num % 3 == 2){
//Code
}
$item_num++;
}
?>
</div>
Hope it will help.
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 0) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '"
style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
}elseif($item_num % 3 == 1){
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '"
style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
}elseif($item_num % 3 == 2){
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '"
style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
}
$item_num++;
}
?>
Or you can try by using PDO:
Version depends on the records in the database:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM dot");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
?>
<?php $item_num = 0; ?>
<?php foreach ($data as $row): ?>
<div class="col-md-4 col-sm-12">
<?php if ($item_num % 3 == 0): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 1): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 2): ?>
<?php endif; ?>
<?php $item_num++; ?>
</div>
<?php endforeach; ?>
Version with a fixed amount of divs:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM dot");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
?>
<?php for ($i = 0; $i < 3; $i++): ?>
<?php $item_num = 0; ?>
<div class="col-md-4 col-sm-12">
<?php foreach ($data as $row): ?>
<?php if ($item_num % 3 == 0): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 1): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 2): ?>
<?php endif; ?>
<?php $item_num++; ?>
<?php endforeach; ?>
</div>
<?php endfor; ?>

Categories