Bootstrap columns not working when writing in php while loop - php

I am using bootstrap in my website but when i am adding php loop to show the products the products are shown at the end of the page instead of showing in the area 'product will show here'.
<div class="col-sm-9 padding-right">
<div class="features_items">
<h2 class="title text-center">Features Items</h2>
<?php
function fetch($table){
$result = mysqli_query($conn,"SELECT id ,name FROM menshirt");
while($row = mysqli_fetch_assoc($result)) {
echo
'<div class="col-sm-4">
<h2>$56</h2>
p> '.$name =$row['name'].'</p>
<i class="fa fa-cart"> </i>Add to cart
</div>
</div>';
}
mysqli_close($conn);}
?>
</div></div>

You only creates a function fetch which isn't called. And inside open less divs than closed.
<div class="col-sm-9 padding-right">
<div class="features_items">
<h2 class="title text-center">Features Items</h2>
<?php
$result = mysqli_query($conn,"SELECT id ,name FROM menshirt");
while($row = mysqli_fetch_assoc($result)) {
echo '
<div class="col-sm-4">
<h2>$56</h2>
p> '. $row['name'] . '</p>
<i class="fa fa-cart"> </i>Add to cart
</div>
<!--/div--> <!-- you opened one div, tried to close two -->
';
}
mysqli_close($conn);}
?>
</div>
</div>

Related

Display stars from star rating using php loop in a function

I have a family recipe site that I am working on. Each recipe has a star rating that is stored in a mysql database (integer between 1 and 5). Currently, these ratings are displayed using html class=fa fa-star. So 5 lines need to be added to the code to display 5 stars. I want to dynamically generate these stars depending on the star rating in the database. The recipe list page (desserts.php) calls a function (stored in another php file) to populate the list of recipes in the desserts category dynamically depending on the recipes stored in the database.
<!-- desserts.php -->
<!-- ##### New Recipe Area Start. This section displays all recipes from the database. ##### -->
<section class="small-receipe-area section-padding-80-0">
<div class="section-heading">
<h3>Dessert Recipes</h3>
</div>
<div class="container">
<div class="row">
<?php
while ($row = mysqli_fetch_assoc($result)){
list_of_recipes($row['recipe_name'], $row['recipe_img'], $row['recipe_link'], $row['stars'], $row['date_entered']);
}
?>
</div>
</div>
</section>
<!-- ##### New Recipe Area End ##### -->
<!-- recipe_list_component.php -->
<!-- ##### This function is called by desserts.php for every recipe found in the database in this category. ##### -->
<?php
function list_of_recipes($recipename, $recipeimg, $recipelnk, $stars, $recipedate){
$i = "";
$element = "
<!-- New Recipe Area -->
<div class=\"col-12 col-sm-6 col-lg-4\">
<div class=\"single-small-receipe-area d-flex\">
<!-- Recipe Thumb -->
<div class=\"receipe-thumb\">
<img src=\"$recipeimg\" alt=\"caramel_sauce\">
<!--<img src=\"img/bg-img/caramel1_thumb.jpg\" alt=\"\">-->
</div>
<!-- Recipe Content -->
<div class=\"receipe-content\">
<span>$recipedate</span>
<a href=\"$recipelnk\">
<h5>$recipename</h5>
</a>
<div class=\"ratings\">
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
</div>
<p>0 Comments</p>
</div>
</div>
</div>
";
echo $element;
}
?>
In the function, I receive the number of stars recorded in the database ($stars). I want to use this number to generate this HTML line <i class="fa fa-star" aria-hidden="true"> which shows a star in the rating area. I believe a For Loop is needed, but I am not sure how to generate the HTML statement so that if $stars = 4, I will see 4 stars for that recipe on the desserts page.
Thanks
Just repeat the string that number of times:
$element = "
--code--
<div class=\"ratings\">"
. str_repeat('<i class="fa fa-star" aria-hidden="true"></i>', $stars) .
"</div>
--code--";
Or store it in a variable and use it as you use the others:
$star_list = str_repeat('<i class="fa fa-star" aria-hidden="true"></i>', $stars);
use a loop inside function
for($stars > 5;$stars--;){
echo "<i class=\"fa fa-star\" aria-hidden=\"true\"></i>\n";
}
so your function must be like this
<?php
function list_of_recipes($recipename, $recipeimg, $recipelnk, $stars, $recipedate){
$i = "";
$star = "";
for($stars > 5;$stars--;){
$star .= "<i class=\"fa fa-star\" aria-hidden=\"true\"></i>\n";
}
$element = "
<!-- New Recipe Area -->
<div class=\"col-12 col-sm-6 col-lg-4\">
<div class=\"single-small-receipe-area d-flex\">
<!-- Recipe Thumb -->
<div class=\"receipe-thumb\">
<img src=\"$recipeimg\" alt=\"caramel_sauce\">
<!--<img src=\"img/bg-img/caramel1_thumb.jpg\" alt=\"\">-->
</div>
<!-- Recipe Content -->
<div class=\"receipe-content\">
<span>$recipedate</span>
<a href=\"$recipelnk\">
<h5>$recipename</h5>
</a>
<div class=\"ratings\">
$star
</div>
<p>0 Comments</p>
</div>
</div>
</div>
";
echo $element;
}

Display Count of DB Records in PHP Dashboard

I need to display a "count" of records (rows) in my SQLtable "doctors". This count must appear on my dashboard page in the below element for total number of doctors
This is index.php for my dashboard page
<?php
$con = mysqli_connect("localhost","root","","hospital_db");
$result = mysqli_query($con,"SELECT * FROM doctors");
$rows = mysqli_num_rows($result);
$content = '<div class="row">
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-aqua">
<div class="inner">
<h3><?php echo '.$rows.';?></h3>
<p>Doctors</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
View Doctors <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
<!-- ./col -->
</div>';
include('../master.php');
?>
You should use mysqli object in new php version try the following code
Make connection first like
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con,"SELECT * FROM doctors");
$rows = mysqli_num_rows($result);
echo "There are " . $rows . " rows in my table.";
$content = '<div class="row">
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-aqua">
<div class="inner">
*<h3><?php echo "$rows"; } ?></h3>*
<p>Doctors</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
View Doctors <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
<!-- ./col -->
</div>';
include('../master.php');
?>
if you just need the count then why don't you use the aggregate function count(*) in your query. this much better helps you. and in your code in h3 tag you can concat the string directly rather than using again php code. this might looks better and in structured form.
try this:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con,"SELECT count(*) as total_rows FROM doctors");
$rows = $result->total_rows;
echo "There are " . $rows . " rows in my table.";
$content = '<div class="row">
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-aqua">
<div class="inner">
*<h3>'.$rows.'</h3>*
<p>Doctors</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
View Doctors <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
<!-- ./col -->
</div>';
include('../master.php');
?>

How to style tables in articles page?

I imported a database in phpMyAdmin that contains articles. I want it to be regulated, to automatically post the image, title, and contents in the articles page. And to direct it to another page when clicked.
But when rendering the articles page, the second row has two columns offset. I think it's the content of the first 2 articles that's making the problem since I did another tables with few contents in it then it renders exactly the way I want(a row that contains 3 articles an no offset below it). I'm a newbie here, feel free to suggest if there's a better way. I genuinely appreciate it. Thank you!
<div class="blogs-2" id="blogs-2">
<div class="container">
<div class="row">
<div class="col-md-10">
<div class="row">
<?php
$sql = "SELECT * FROM news LIMIT 20";
$result= $DBcon->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<div class="col-md-4" style="margin-top:0;margin-bottom:0;padding-top:0;padding-bottom:0; ">
<div class="card card-plain card-blog" style="max-height:300px;float:left;">
<div class="card-image">
<a href="../news/article.php?id=<?php echo $row['id']; ?>">
<img class="img img-raised" src="<?php header("Content-type: image/png"); echo $row['featured_image'] ?>" style="max-height:300px;" />
</a>
</div>
<div class="card-content">
<h4 class="card-title">
<a href="../news/article.php?id=<?php echo $row['id']; ?>">
<?php echo $row['title']; ?>
</a>
</h4>
<p class="card-description" style="overflow: hidden;display: -webkit-box;-webkit-line-clamp: 3;-webkit-box-orient: vertical;">
<?php echo $row['content']; ?>
</p>
<p class="card-description">
Read More
</p>
</div>
</div>
</div>
<?php
}
}
else {
echo "<p>No News Articles to Display</p>";
}
?>
</div>
</div>
<div class="col-md-2">
<h4 class="title">Recent News</h4>
<ul>
<?php
$sql = "SELECT * FROM news LIMIT 5";
$result= $DBcon->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<li style='list-style:none;color:black;padding-top:5px;padding-bottom:5px;'><a href='../news/article.php?id={$row['id']}'>" .$row['title']. "</a></li>";
}
}
else {
echo "<li style='list-style:none;'>No Recent News</li>";
}
?>
</ul>
</div>
</div><!--row-->
</div><!--container-->
</div><!--blogs-->

Printing out results inside 2 whiles

So im building a fast food website i have built most of it apart from displaying the menu this will be different for each take away which is signed up. I have worked out that im gonna have categories and items so the take away can have a categories called "trays" then 5 items inside that categories with all the different trays they do then they can make another categorie for another block of food and so on. Problem i have is i will need to print out the categories and the items. I have 1 table called categories then another called topics the items will be stored in the topic table and categories in categories so would need to display it like so
categorie
topic
topic
topic
over and over.
Here is my html
<div class="menu-widget" id="2">
<div class="widget-heading">
<h3 class="widget-title text-dark">
POPULAR ORDERS Delicious hot food!
<a class="btn btn-link pull-right" data-toggle="collapse" href="#popular2" aria-expanded="true">
<i class="fa fa-angle-right pull-right"></i>
<i class="fa fa-angle-down pull-right"></i>
</a>
</h3>
<div class="clearfix"></div>
</div>
<div class="collapse in" id="popular2">
<div class="food-item">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-8">
<div class="rest-logo pull-left">
<a class="restaurant-logo pull-left" href="#"><img src="http://placehold.it/100x80" alt="Food logo"></a>
</div>
<!-- end:Logo -->
<?php echo "food inside";?>
<div class="rest-descr">
<h6>Veg Extravaganza</h6>
<p> Burgers, American, Sandwiches, Fast Food, BBQ</p>
</div>
<!-- end:Description -->
</div>
<!-- end:col -->
<div class="col-xs-12 col-sm-12 col-lg-4 pull-right item-cart-info"> <span class="price pull-left">$ 19.99</span> + </div>
</div>
<!-- end:row -->
</div>
<!-- end:Food item --><!-- end:Food item --><!-- end:Food item -->
<div class="food-item white"><!-- end:row -->
</div>
<!-- end:Food item -->
</div>
<!-- end:Collapse -->
</div>
<!-- end:Widget menu -->
and here is my code
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-6"><!-- end:Widget menu -->
<?php
///first select the category based on $_GET['cat_id']
$sql = "SELECT * FROM categories WHERE takeawayid =' ".$id1."'";
$result = mysql_query($sql);
if(!$result)
{
echo 'The category could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'This category does not exist.';
}
else
{
//display category data
while($row = mysql_fetch_assoc($result))
{
?>
<div class="menu-widget" id="2">
<div class="widget-heading">
<h3 class="widget-title text-dark">
<?php echo $row['cat_name'] ; ?> <a class="btn btn-link pull-right" data-toggle="collapse" href="#popular2" aria-expanded="true">
<i class="fa fa-angle-right pull-right"></i>
<i class="fa fa-angle-down pull-right"></i>
</a>
</h3>
<div class="clearfix"></div>
<?php
//do a query for the topics
$sql2 = "SELECT * FROM topics WHERE topic_cat =' ".$row['cat_id']."'";
$result2 = mysql_query($sql2);
}
echo $row2['topic_subject'] ;
if(!$result2)
{
echo 'The topics could not be displayed, please try again later.';
}
else
{
if(mysql_num_rows($result2) == 0)
{
echo 'There are no topics in this category yet.';
}
else
{
//prepare the table
while($row2 = mysql_fetch_assoc($result2))
{
?>
<div class="collapse in" id="popular2">
<div class="food-item">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-8">
<div class="rest-logo pull-left">
<a class="restaurant-logo pull-left" href="#"><img src="http://sdfsdf.com/beta/restaurants/<?php echo $id33 ;?>.png" alt="Food logo"></a>
</div>
<!-- end:Logo -->
<div class="rest-descr">
<h6><?php echo $row2['topic_subject'] ;?></h6>
<p> Burgers, American, Sandwiches, Fast Food, BBQ</p>
</div>
<!-- end:Description -->
</div>
<!-- end:col -->
<div class="col-xs-12 col-sm-12 col-lg-4 pull-right item-cart-info"> <span class="price pull-left">$ 19.99</span> + </div>
</div>
<!-- end:row -->
</div>
<!-- end:Food item --><!-- end:Food item --><!-- end:Food item -->
<div class="food-item white"><!-- end:row -->
</div>
<!-- end:Food item -->
</div>
<!-- end:Collapse -->
</div>
</div>
<?php
}
}
}
}
}
?>
<!-- end:Widget menu -->
Code works but when i add more than 1 result im getting this
http://prntscr.com/fu3qll
Here is 1 cat and 2 topics in side it
https://prnt.sc/fu3g2j
works great but when i add 2 cats and 4 topics 2 inside each one i get above the fu3kbk link
so i fixed it by moving the bracket down on
$result2 = mysql_query($sql2);
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-6"><!-- end:Widget menu -->
<?php
///first select the category based on $_GET['cat_id']
$sql = "SELECT * FROM categories WHERE takeawayid =' ".$id1."'";
$result = mysql_query($sql);
if(!$result)
{
echo 'The category could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'This category does not exist.';
}
else
{
//display category data
while($row = mysql_fetch_assoc($result))
{
?>
<div class="menu-widget" id="2">
<div class="widget-heading">
<h3 class="widget-title text-dark">
<?php echo $row['cat_name'] ; ?> <a class="btn btn-link pull-right" data-toggle="collapse" href="#popular2" aria-expanded="true">
<i class="fa fa-angle-right pull-right"></i>
<i class="fa fa-angle-down pull-right"></i>
</a>
</h3>
<div class="clearfix"></div>
</div>
<?php
//do a query for the topics
$sql2 = "SELECT * FROM topics WHERE topic_cat =' ".$row['cat_id']."'";
$result2 = mysql_query($sql2);
if(!$result2)
{
echo 'The topics could not be displayed, please try again later.';
}
else
{
if(mysql_num_rows($result2) == 0)
{
echo 'There are no topics in this category yet.';
}
else
{
//prepare the table
while($row2 = mysql_fetch_assoc($result2))
{
?>
<div class="collapse in" id="popular2">
<div class="food-item">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-8">
<div class="rest-logo pull-left">
<a class="restaurant-logo pull-left" href="#"><img src="http://geatzo.com/beta/restaurants/<?php echo $id33 ;?>.png" alt="Food logo"></a>
</div>
<!-- end:Logo -->
<div class="rest-descr">
<h6><?php echo $row2['topic_subject'] ;?></h6>
<p> Burgers, American, Sandwiches, Fast Food, BBQ</p>
</div>
<!-- end:Description -->
</div>
<!-- end:col -->
<div class="col-xs-12 col-sm-12 col-lg-4 pull-right item-cart-info"> <span class="price pull-left">$ 0</span> + </div>
</div>
<!-- end:row -->
</div>
<!-- end:Food item --><!-- end:Food item --><!-- end:Food item -->
<!-- end:Food item -->
</div>
<!-- end:Collapse -->
<?php
}
}
}
}
}
}
?>
</div>
</div>
<!-- end:Widget menu -->

Using information in an SQL array to provide a result in a different field

I'm trying to display the gang name of a user if their steam id is found in the list of players.
Gang Table
I can get other details to pull but this is the first time I have tried to pull from an array.
<div class="col-lg-3 col-md-6">
<div class="panel panel-yellow">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fa fa-users fa-2x"></i>
</div>
<div class="col-xs-9 text-right">
<?php
$sid = $steamprofile['steamid'];
$bob = ("[`$sid`]");
$sql = "SELECT `name` FROM `gangs` WHERE `members` = (' . explode(',', $bob) . ')";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<div class='big'>" . $row["name"] . "</div>";
};
?>
</div>
</div>
</div>
<a href="#">
<div class="panel-footer">
<span class="pull-left">Gang Management</span>
<span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>
</a>
</div>
</div>
Your member column seems to be a JSON array text field. You can't directly search within it unless you use TEXT search methods.
$query = 'select * from users where members LIKE "%\"' . $steamId . '\"%"';

Categories