while loop and bootstrap - php

I have problem to display posts properly.
Im displaying content dinamicaly with a function.
Function in php is
function displayAllPosts () {
global $connection;
$query = "SELECT * FROM blog_post ";
$run_query = mysqli_query($connection,$query);
while ($row = mysqli_fetch_array ($run_query)){
$post_title = $row ['post_title'];
$post_desc = $row ['post_desc'];
$post_img = $row ['post_img'];
echo "
<div class='col-md-3 col-sm-6'>
<div class='feature-center text-center animate-box' data-animate-effect='fadeIn'>
<img class='image-responsive' width='100%' src='$post_img' / ><hr>
<h2>{$post_title}</h2><hr>
<p>{$post_desc} </p>
<a href='#'><button type='button' class='btn btn-info' style='width:100%'>Categoty Title</button></a>
</div>
</div>
";
}
}
and like that page looks like this:
page
on smaler devices it will display 2 columns in a row. I was wandering is it posible to insert
<div class='clearfix'></div>
after 2 displayed columns?

i found solution :
$count=0;
while ($row = mysqli_fetch_array ($run_query)){
$post_title = $row ['post_title'];
$post_desc = $row ['post_desc'];
$post_img = $row ['post_img'];
echo "
<div class='col-md-3 col-sm-6'>
<div class='feature-center text-center animate-box' data-animate-effect='fadeIn'>
<img class='image-responsive' width='100%' src='$post_img' / ><hr>
<h2>{$post_title}</h2><hr>
<p>{$post_desc} </p>
<a href='#'><button type='button' class='btn btn-info' style='width:100%'>Categoty Title</button></a>
</div>
</div>
";
$count++;
switch ($count){
case ($count % 4 == 0) ;
echo "<div class='clearfix visible-lg-block visible-md-block'></div>";
break ;
case ($count % 2 == 0 && $count % 4 !==0 ) ;
echo "<div class='clearfix visible-sm-block'></div>";
break ;
}
}

Related

SELECT query returns 1 row when there is more than 1

I'm working on a project for my university homework. Everything works right now except for my activity list. Issue is that whenever I try to view my activity list, it only returns 1 activity instead of all the activities that are still ongoing, when I manually run the query from phpMyAdmin, it returns all the queries but PHP doesn't seem to get it right and I have no idea why.
I have already tried running the query from phpMyAdmin and simplifying my query, also added a bunch of debugging code to see where I am going wrong but all the values are correct, the query is correct and phpMyAdmin runs the query just fine.
$query = "SELECT * FROM activities"; /*LIMIT " . $limit . ", " . $maxActivity;*/
$result = mysqli_query($link, $query);
echo $query;
if(!$result) {
echo "<p class='text-danger'>There are no activities to display.</p>";
} else {
while($row = mysqli_fetch_assoc($result)) {
$activity_id = $row['id'];
$activity_name = $row['name'];
$activity_description = $row['description'];
$activity_end_date = $row['endDate'];
$activity_start_date = $row['startDate'];
$activity_type = $row['type'];
$activityAuthor = $row['author'];
$activityPublished = $row['publishedAt'];
$activityLikes = $row['likes'];
$activityDislikes = $row['dislikes'];
$activityComments = $row['comments'];
$activityViews = $row['views'];
$query = "SELECT * FROM users WHERE Username = '$activityAuthor'";
$result = mysqli_query($link, $query);
if(!$result) {
die("MySQL Query Failed: " . mysqli_error($link));
}
$row = mysqli_fetch_assoc($result);
$authorID = $row['id'];
?>
<div class="row">
<div class="col-md-6">
<a href="#">
<img class="img-fluid rounded mb-3 mb-md-0" src="img/<?php echo "$activity_type";?>.jpeg" alt="">
</a>
</div>
<div class="col-md-6">
<h3><?php echo $activity_name;?></h3>
<p class="lead text-secondary">Proposed by <a class="text-secondary lead" href="profile.php?id=<?php echo $authorID;?>"><?php echo $activityAuthor;?></a> at <?php echo $activityPublished;?></p>
<p><?php echo $activity_description;?></p>
<p><strong>Starts at:</strong> <?php echo $activity_start_date;?></p>
<p><strong>Ends at:</strong> <?php echo $activity_end_date;?></p>
<a class="btn btn-outline-success" href="config/like.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>"><i class="far fa-thumbs-up"></i> <?php echo $activityLikes;?></a>
<a class="btn btn-outline-danger" href="config/dislike.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>"><i class="far fa-thumbs-down"></i> <?php echo $activityDislikes;?></a>
<button class="btn btn-outline-secondary"><i class="far fa-comment-dots"></i> <?php echo $activityComments;?></button>
<button class="btn btn-outline-secondary"><i class="far fa-eye"></i> <?php echo $activityViews;?></button>
<div class="mt-3"><a class="btn btn-primary" href="activity.php?post=<?php echo $activity_id;?>&page=<?php echo $page;?>&commentpage=1">View Activity</a></div>
</div>
</div>
<hr>
<?php }
}?>
<?php
$query = "SELECT * FROM activities";
$result = mysqli_query($link, $query);
if(!$result) {
echo "<p class='text-danger'>There are no activities to display.</p>";
} else {
while($row = mysqli_fetch_assoc($result)) {
$activity_id = $row['id'];
$activity_name = $row['name'];
$activity_description = $row['description'];
$activity_end_date = $row['endDate'];
$activity_start_date = $row['startDate'];
$activity_type = $row['type'];
$activityAuthor = $row['author'];
$activityPublished = $row['publishedAt'];
$activityLikes = $row['likes'];
$activityDislikes = $row['dislikes'];
$activityComments = $row['comments'];
$activityViews = $row['views'];
$query = "SELECT * FROM users WHERE Username = '$activityAuthor'";
$result = mysqli_query($link, $query);
if(!$result) {
die("MySQL Query Failed: " . mysqli_error($link));
}
$row = mysqli_fetch_assoc($result);
$authorID = $row['id'];
}
$output.="<div class='row'>
<div class='col-md-6'>
<a href='#'>
<img class='img-fluid rounded mb-3 mb-md-0' src='img/<?php echo '$activity_type';?>.jpeg' alt=''>
</a>
</div>
<div class='col-md-6'>
<h3><?php echo $activity_name;?></h3>
<p class='lead text-secondary'>Proposed by <a class='text-secondary lead' href='profile.php?id=<?php echo $authorID;?>'><?php echo $activityAuthor;?></a> at <?php echo $activityPublished;?></p>
<p><?php echo $activity_description;?></p>
<p><strong>Starts at:</strong> <?php echo $activity_start_date;?></p>
<p><strong>Ends at:</strong> <?php echo $activity_end_date;?></p>
<a class='btn btn-outline-success' href='config/like.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>'><i class='far fa-thumbs-up'></i> <?php echo $activityLikes;?></a>
<a class='btn btn-outline-danger' href='config/dislike.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>'><i class='far fa-thumbs-down'></i> <?php echo $activityDislikes;?></a>
<button class='btn btn-outline-secondary'><i class='far fa-comment-dots'></i> <?php echo $activityComments;?></button>
<button class='btn btn-outline-secondary'><i class='far fa-eye'></i> <?php echo $activityViews;?></button>
<div class='mt-3'><a class='btn btn-primary' href='activity.php?post=<?php echo $activity_id;?>&page=<?php echo $page;?>&commentpage=1'>View Activity</a></div>
</div>
</div>";
}
?>
<?php echo $output; ?>

How do i insert these data from mysql into my webpage by php?

am trying to pull data from database of a number of associated rows into ROW1, ROW2 and ROW3 but using
<?php echo $row['']; ?>
but it's not working, please any idea
here is my code,
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
}
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
Well it looks like you are closing your while loop before you echo your results. Just move the closing bracket } after the closing </li>
Your code should look like this:
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
}
}
?>
Try this code
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $data) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $data['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $data['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
}
}
}
?>

Bootstrap Carousel to display text

I want to display long notes on bootstrap carousel. the notes will be fetched from mySql database. Since the notes are long, i need to split them using number of words then display them in the carousel slides. the problems is that am not able to display the splitted text in different slides. the following is what i have done:
<div class="col-md-6 ">
<div class="panel panel-primary">
<div class="panel-heading">Lecture</div>
<!-- Slider News by Carousel -->
<div id='myCarousel' class='carousel slide' data-ride='carousel'>
<ol class='carousel-indicators'>
<?php
include "config/koneksi.php";
$query = "select notes from notes where Note_ID = 34";
$res = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($res)) {
$w = $row['notes'];
$arr2 = str_split($w, 500);
}
//var_dump($arr2);
$max = sizeof($arr2);
$slides = '';
$Indicators = '';
$counter = 0;
?>
</ol>
<div class='carousel-inner'>
<?php
for ($x = 0; $x <= $max; $x++) {
if ($x == 0) {
echo "<div class='item active'>";
echo $arr2[$x]++;
echo "</div>";
} else {
echo "<div class='item'>";
if (!isset($arr2[$x])) {
$arr2[$x] = 0;
}
echo $arr2[$x]++;
echo "</div>";
}
}
echo "</div>";
echo "<a class='left carousel-control' href='#myCarousel' data-slide='prev'>‹</a>";
echo "<a class='right carousel-control' href='#myCarousel' data-slide='next'>›</a>";
echo "</div>";
echo "<!-- End Slider Caraousel-->";
?>
</div>
</div>
</div>
</div>
the result:
the result

While loop use variable 2 times

Hello guys I got a for loop cicle that prints me divs and information from SQL, I print the slider fields with the settings on the mysql like, Slider number, Field postion and so on, the problem is I have a Modal Bootstrap to be printed aswell but I cant print it inside the currently loop .
My question is, is there anyway to store a variable from a for cicle so it can be reutilized?
There is the code
$ID=$row['ID'];
$sql = "SELECT NUM_Slides as valmax FROM slider_settings,Paginas, slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID=$ID";
$sqlconnect =$connect->query($sql);
$sqlresult =$sqlconnect->fetch_assoc();
for ($k = 1 ; $k <= $sqlresult['valmax']; $k++){
echo "<div class='slider1'>";
$sql1 = "SELECT P$k as campos, tituloP$k as titulo FROM slider_settings, Paginas,slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID = $ID";
$sqlconnect1 =$connect->query($sql1);
$sqlresult1 =$sqlconnect1->fetch_assoc();
echo "<div class='titulo'>
<h2>$sqlresult1[titulo]</h2>
</div>";
for ($l = 1 ; $l <= $sqlresult1[campos]; $l++){
$campo = "SELECT Butao,Titulo,Texto FROM slider_config, Paginas, slider_settings where slider_config.ID = $ID and Paginas.ID = $ID and slider_settings.ID =$ID and P_NUM = $k and Campo = $l";
$sqlconnect2 = $connect->query($campo);
$sqlresult2 = $sqlconnect2->fetch_assoc();
echo "<div class='part' id='part".$l."'>
<div id='imagem' class='button' data-toggle='modal' data-target='#myModal".$l."'>
<img src='data:image/png;base64," . base64_encode($sqlresult2['Butao']) . "'/>
</div>
<div id='titulo'>
<h4>$sqlresult2[Titulo]</h4>
</div>
<div id='texto'>
$sqlresult2[Texto]
</div>
</div>";
}
echo "</div>";
}
and There is the code that cant printed inside of the div or modal wont display
$modal = "SELECT Titulo_modal , Imagem_modal , Texto_modal FROM modal_settings , Paginas where modal_settings.ID = $ID and Paginas.ID= $ID and P_NUM_modal = $k and Campo_modal = $l";
$sqlconnect33 =$connect->query($modal);
$sqlresult33 =$sqlconnect33->fetch_assoc();
for ($n = 1 ; $n <= $sqlresult1[campos]; $n++){
echo "<div class='modal fade' id='myModal".$n."'>
<div class='modal-dialog modal-lg'>
<div class='modal-content'>
<div class='modal-header'>
<h4 class ='titulopopup'>$sqlresult33[Titulo_modal]</h4>
<button type='button' class='close' data-dismiss='modal'>
<span aria-hidden='true'>×</span></button>
</div>
<div class='modal-body'>
<div class='imagem'>
<img src='data:image/png;base64," . base64_encode($sqlresult33['Imagem_modal']) . "'/>
</div>
<div class='texto'>
$sqlresult33[Texto_modal]
</div>
</div>
</div>
</div>
</div>";
}
NOTE : the $ID is comming from another file :) and its work fine the first half of the code
maybe create array like that
$array = [];
for ($n = 1 ; $n <= $sqlresult1[campos]; $n++) {
$array[$k] = $i;
}
this way u can reuse your var stored in array in other for loop like
foreach ($array as $k => $i) {
// and u get all your var :)
}
with your code u can do somethings like
$ID=$row['ID'];
// here
$array = [];
//
$sql = "SELECT NUM_Slides as valmax FROM slider_settings,Paginas, slider_config where slider_settings.ID = $ID and Paginas.ID= $ID
and slider_config.ID=$ID";
$sqlconnect =$connect->query($sql);
$sqlresult =$sqlconnect->fetch_assoc();
for ($k = 1 ; $k <= $sqlresult['valmax']; $k++){
echo "<div class='slider1'>";
$sql1 = "SELECT P$k as campos, tituloP$k as titulo FROM slider_settings, Paginas,slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID = $ID";
$sqlconnect1 =$connect->query($sql1);
$sqlresult1 =$sqlconnect1->fetch_assoc();
echo "<div class='titulo'>
<h2>$sqlresult1[titulo]</h2>
</div>";
for ($l = 1 ; $l <= $sqlresult1[campos]; $l++){
// here
$array[$k] = $l;
$campo = "SELECT Butao,Titulo,Texto FROM slider_config, Paginas, slider_settings where slider_config.ID = $ID and Paginas.ID = $ID and slider_settings.ID =$ID and P_NUM = $k and Campo = $l";
$sqlconnect2 = $connect->query($campo);
$sqlresult2 = $sqlconnect2->fetch_assoc();
echo "
<div class='part' id='part".$l."'>
<div id='imagem' class='button' data-toggle='modal' data-target='#myModal".$l."'>
<img src='data:image/png;base64," . base64_encode($sqlresult2['Butao']) . "'/>
</div>
<div id='titulo'>
<h4>$sqlresult2[Titulo]</h4>
</div>
<div id='texto'>
$sqlresult2[Texto]
</div>
</div>";
}
echo "</div>";
}

div inside while loop in php

The search results should display like this
But my results are stacking on top of each.
Here is my code :
<div class="container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thesis") or die(mysql_error());
$search = trim( $_POST['SearchKeywords']);
$query = " SELECT * FROM new_data WHERE Product_Title or Product_link LIKE'%$search%' ";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);
$count == 0;
if ($count == 0) {
echo "Sorry, Nothing Found !!";
}else{
while ($row = mysql_fetch_array($sql))
{
$img = $row ['Product_Img'];
$link = $row ['Product_link'];
$title = $row ['Product_Title'];
$price = $row ['Product_Price'];
?>
<div class="card">
<div class="front alert alert-success">
<?php echo "$title";
echo "<img src='$img' width='80px' height='100px'>";
echo "$price"; ?>
</div>
</div>
<?php
};
};
?>
</div> <!-- Container -->
Those div blocks are inside a container.
I added a bootstrap class in order for better a design.
You can use thumbnails with custom content
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="..." alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p>Button Button</p>
</div>
</div>
</div>
</div>
I used a counter inside while loop.
Which will check, when there are already 4 blocks/ products in a single row then it will create a new row
<?php
if($productCount == 0)
{
echo "<div class='row'>"; }
}
$productCount++;
if($productCount==4)
{
echo "</div>" ;
$productCount = 0;
}
?>

Categories