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;
}
Related
Hi everyone I'm niubbie in php.I have a problem with tab. I would like the tabs on their click to show a different topic. All this using php and calling the db.
My DB:
giorno
pranzo
cena
lunedi
12:00
20:00
martedi
12:00
20:00
mercoledi
12:00
20:00
giovedi
12:00
20:00
venerdi
12:00
20:00
Days are represented by tabs and when I click on a different day I want it to show lunch and dinner of that particular day.
My code:
<section class="big-section bg-light-gray border-top border-color-medium-gray wow animate__fadeIn">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12 text-center margin-six-bottom">
<h6 class="alt-font text-extra-dark-gray font-weight-500">Orari</h6>
</div>
</div>
<div class="row justify-content-center">
<div class="col-12 col-lg-10 tab-style-05">
<div class="tab-box">
<!-- start tab navigation -->
<ul class="nav nav-tabs margin-7-rem-bottom md-margin-5-rem-bottom xs-margin-15px-lr align-items-center justify-content-center font-weight-500 text-uppercase">
<?php
$sql = "SELECT * FROM orari_ristorante ";
$risultato = mysql_query($sql) or die(mysql_error()."<br>Impossibile eseguire l'interrogazione");
$i=0;
while ($riga = mysql_fetch_assoc($risultato)){
?>
<?php if($i == 0){?>
<li class="nav-item alt-font"><a class="nav-link active" href="#tab-nine1" data-toggle="tab"><?php echo $riga['giorno'];?></a></li>
<?php }else{?>
<li class="nav-item alt-font"><a class="nav-link" href="#tab-nine1" data-toggle="tab"><?php echo $riga['giorno'];?></a></li>
<?php }
$i++;
}?>
</ul>
<!-- end tab navigation -->
</div>
<div class="tab-content">
<!-- start tab content -->
<div class="tab-pane med-text fade in active show" id="tab-nine1">
<div class="panel-group accordion-event accordion-style-04" id="accordion1" data-active-icon="icon-feather-minus" data-inactive-icon="icon-feather-plus">
<!-- start accordion item -->
<div class="panel border-color-black-transparent">
<div class="panel-heading">
<?php
$sql = "SELECT pranzo,cena FROM orari_ristorante LIMIT 1";
$risultato = mysql_query($sql) or die(mysql_error()."<br>Impossibile eseguire l'interrogazione");
while ($riga = mysql_fetch_assoc($risultato)){
?>
<span class="panel-time">Pranzo</span>
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1">
<div class="panel-title">
<span class="text-extra-dark-gray d-inline-block font-weight-500"><?php echo $riga['pranzo'] ;?></span>
</div>
</a>
<span class='prenota'>PRENOTA</span>
<span class="panel-time">Cena</span>
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1">
<div class="panel-title">
<span class="text-extra-dark-gray d-inline-block font-weight-500"><?php echo $riga['cena'] ;?></span>
</div>
</a>
<span class='prenota'>PRENOTA</span>
<?php
}
?>
</div>
</div>
</div>
</div>
<!-- end tab content -->
</div>
</div>
</div>
</div>
</section>
My problem is that each tab shows all the rows and not the specific one for that day
My Problem
You need to ensure you are using unique ID's for your contents and using them in your href of the tab.
Reading your code it looks like each tab is created with the same ID
<li class="nav-item alt-font"><a class="nav-link active" href="#tab-nine1" data-toggle="tab"><?php echo $riga['giorno'];?></a></li>
I would echo out the unique id from the database eg.
href="#tab-<?echo $riga['id'];?>" (or whatever your unique column header is)
Ensure you also echo this out further down when the tab content is being created.
Based on what you are trying to accomplish, if you limit your results to one, you will always only show the first day in the db. Here's how I would change your second while loop.
<?php
$sql = "SELECT * FROM orari_ristorante";
$risultato = mysqli_query($conn, $sql);
$i = 0;
while ($riga = mysqli_fetch_assoc($risultato)){
if($i == 0){
$css = ""
}else{
$css = "display:none"
}
?>
<div class="giorno_tab <?php echo $riga['giorno'] ;?>" style="<?php echo $css ;?>">;
<span class="panel-time">Pranzo</span>
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1">
<div class="panel-title">
<span class="text-extra-dark-gray d-inline-block font-weight-500"><?php echo $riga['pranzo'] ;?></span>
</div>
</a>
<span class='prenota'>PRENOTA</span>
<span class="panel-time">Cena</span>
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1">
<div class="panel-title">
<span class="text-extra-dark-gray d-inline-block font-weight-500"><?php echo $riga['cena'] ;?></span>
</div>
</a>
<span class='prenota'>PRENOTA</span>
</div>
<?php
$i++;}
?>
</div>
It is not the solution, however you can build on top of that to accomplish what you are trying to do. Hopefully that somewhat helps.
Just to explain, I was adding $riga["giorno"] as an kind of ID in the class, however Revbo's answer would give a clearer code when it comes to an ID
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');
?>
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 -->
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>
<?php include "$_SERVER[DOCUMENT_ROOT]/new/stations/includes/header.php"; ?> /* HEADER */
<div class="content">
<div class="content-padded">
<!-- radio name -->
<h3 class="text-center"> Akwaaba Radio </h3>
<!-- end radio name -->
<!-- start station logo -->
<div class="img-middle">
<img src="img/Akwaaba.png" alt="cmon" width="200px"> /* Station IMAGE*/
</div>
<!-- End Logo -->
<!-- Player -->
<div class="audio-player">
<p>Click the button ( <i class="fa fa-play"></i> ) to play</p>
<audio src="http://149.255.33.74:8002/;.mp3" controls="yes"></audio>
</div>
<!-- End Player -->
<!--
=================
navigation button
=================
-->
<div class="spacer"></div>
<div class="text-center">
<?php include "$_SERVER[DOCUMENT_ROOT]/new/stations/includes/toggle.php"; ?>
</div>
<div class="spacer"></div>
<!--
=================
navigation button
=================
-->
<h4>About :-</h4>
<!-- Station details -->
<div class="well">
001 (703) 395-0534
</div>
<!-- End Detials -->
<center>
<a class=" space-btn btn btn-facebook"><i class="fa fa-facebook"></i> | Share on Facebook</a>
<a class=" space-btn btn btn-twitter "><i class="fa fa-twitter"></i> | Tweet on Twitter</a>
<a class=" space-btn btn btn-google-plus "><i class="fa fa-google"></i> | Share on google+</a>
<div class="spacer"></div>
</center>
<!-- Adsense -->
<img src="http://www.hdwallpapers.in/walls/optimus_prime_hd-HD.jpg" width="100%">
<!-- End Adsense -->
<!-- Phone Number -->
<a href="tel:001 (703) 395-0534" class="btn btn-positive btn-block space-btn">001 (703) 395-0534
<br>
<small>Station Hotline</small>
</a>
<!-- End Number -->
<div class="btm-spc"></div>
</div>
</div>"
<?php include "$_SERVER[DOCUMENT_ROOT]/new/stations/includes/footer.php"; ? >
In the above code image won't load at the first load. The page must refresh in order to load the image correctly.
In desktop browsers the image does load at once but in mobile the Page must be refreshed to load the image.
Please Select the First Option
Replace
<img src="img/Akwaaba.png">
with
<img src="<?php echo$_SERVER[DOCUMENT_ROOT];?>/img/Akwaaba.png">