how can i add active class to dynamic menu.I use while for my product list.But i cannot understand how can i add to active class.
<div class="col-md-4">
<h3>Products</h3>
<div class="list-group">
<?php
$db = new connection();
$productsor = mysqli_query($db,"select * from products");
while($productcek = mysqli_fetch_assoc($productsor)){ ?>
<?php echo $productcek['products_ad']; ?>
<?php
}
?>
</div>
</div>
You need to check against a previously set _GET parameter and compare it in your while loop
while($productcek = mysqli_fetch_assoc($productsor)){ ?>
<?php $active = (isset($_GET['products_id']) && $productcek['products_id'] == $_GET['products_id']) ? 'active' : null; ?>
<?php echo $productcek['products_ad']; ?>
<?php
}
?>
Related
I want to have a next button on my Productdetails.php page that would show the next image fetched from the database using while loop. Below is the productdetails.php page shows details of a particular product.
I am using simple php format, How can I go about it? I have come this far at this point.
<div class = "uniqueproduct">
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
$query1 = mysqli_query($conn, "select * from products where PRODUCT_ID = $id");
while ($row1 = mysqli_fetch_array($query1)) {
?>
<div class = "singleproduct">
<?php echo '<img src = "data:image/jpeg;base64,'.base64_encode($row1['PRODUCT_IMAGE']).'" alt = "" width = "250px" height ="300px"/>'; ?>
</div>
<div class = "productName">
<?php echo $row1['PRODUCT_NAME']; ?>
</div>
<div class = "productDescription">
<?php echo $row1['PRODUCT_DESCRIPTION'];?>
</div>
<div class = "productPrice">
<?php echo $row1['PRODUCT_PRICE']; ?>
</div>
<?php
}
}
?>
<button class = "btnPicture"> ..............</button>
</div>
I'm trying to give structure to my code and i am facing a problem.
I'm looping through a sql query response and for each element i'm trying to retrieve other related elements. It works in my controller without problem but when i'm trying to repeat in the view I always get the same value for the related element
My controller:
<?php
include_once('class/guide.class.php');
$bdd = new DBHandler();
$req = guide::getGuides($bdd,0,5);
foreach ($req as $results => $poi)
{
$req[$results]['id'] = htmlspecialchars($poi['id']);;
$req[$results]['name'] = nl2br(htmlspecialchars($poi['name']));
$guide = new guide($results['name'],$bdd);
$guidePois = $guide->getGuidePois($poi['id']);
foreach ($guidePois as $res => $re)
{
echo $guidePois[$res]['id'];
echo $guidePois[$res]['name'];
$guidePois[$res]['id'] = htmlspecialchars($re['id']);
$guidePois[$res]['name'] = nl2br(htmlspecialchars($re['name']));
}
}
include_once('listing.php');
here, you see that I echo the ids/names of the related list of element and it works well, the output is correct for each element of the first list.
When i do it in my view:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Somehow the first list output are the good elements, but for the 2nd list, i always get the related elements of the first item.
Do you have an idea ?
Thanks a lot for your help
This is because you only set:
$guidePois = $guide->getGuidePois($poi['id']);
once in the controller.
If you want it to work in the view, you need to insert this code right after the closing </h3>
<?php $guidePois = $guide->getGuidePois($poi['id']); ?>
So that $guidePois gets a new value in each iteration.
Complete view code:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php
$guidePois = $guide->getGuidePois($poi['id']);
foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Seems like I'm not able to do a simple query.
What I want to achieve is all the results that have 'best2' value '1' so the div will be attached to only those results.
Right now , the div 'badge-best2div' attaches to ALL the products.
I don't know what I'm missing out.
<?php $show = true; ?>
<?php
$roman = Doctrine_Query::create()->from("Product")
->where("status = 1")
->andWhere("site_id = ? ", SITE_ID)
->andWhere("best2 = 1")
->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);
?>
<?php if ($product->getIsTop() && !isset($hide_badge_top)): $show = false;?>
<div class="badge-top"></div>
<?php endif; ?>
<?php if ($roman['best2']){
$show = false; ?>
<div class="badge-best2div"></div>
<?php } ?>
<?php if ($product->getIsBestBuy() && !isset($hide_badge_bestbuy) && $show): ?>
<div class="badge-bestbuy"></div>
<?php endif ; ?>
got it working using
<?php if (($roman['best2'])==1){
Lets say I have a persons table :
forename surname age gender
--------------------------------------------
adam example 90 male
john example 90 male
If I wanted to display this information in separate divs, how could this be done? Say for example the following HTML.
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<!-- adams data here -->
</div>
<div class = "jumbotron">
<!-- johns data here -->
</div>
</div>
</div>
I'm aware on how to query the DB to get the information into PHP variables, I'm just not sure how to dynamically display the data in separate divs.
Below is how I am getting the data
<?php
if($result = $db->query("SELECT forename,surname FROM users ")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
echo $row->forename, '<br><br>';
echo $row->surname, '<br><br>';
}
$result->free();
}
}
?>
Just wrap the div inside a loop so you'll print a div for each result row
<?php foreach($result as $r): ?>
<div class = "jumbotron">
<?php echo $r['name'] // Print fields you need ?>
</div>
<?php endforeach; ?>
EDIT: Now I can see your query. Try this:
<?php
if($result = $db->query("SELECT forename,surname FROM users ")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
?>
<div class = "jumbotron">
<?php echo $row->forename; ?><br><br>
<?php echo $row->surname; ?><br><br>
</div>
<?php
}
$result->free();
}
}
?>
Well Joe, This is a fairly simple problem that has probably been answered before.
I'll use the mysqli class of php
first create a mysqli connection object in a file for best practice then include it in your main script.
<?php
$connection = new mysqli($host_address, $username, $password, $database);
?>
include this in your main script
<?php require "path\to\connection\script"; ?>
<?php
$sql_adam = "SELECT * FROM persons WHERE forename = 'adam'";
$sql_john = "SELECT * FROM persons WHERE forename = 'john'";
$qry1 = $connection->query($sql_adam);
$qry2 = $connection->query($sql_john);
?>
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<?php if ($qry1->num_rows >= 1){
while($adam = $qry1->fetch_assoc()){
foreach ($adam as $column => $data) {
echo "<p>$column : $data </p>";
}
}
}
</div>
<div class = "jumbotron">
<?php if ($qry2->num_rows >= 1){
while($john = $qry2->fetch_assoc()){
foreach ($john as $column => $data) {
echo "<p>$column : $data </p>";
}
}
}
</div>
</div>
</div>
Your Result should be something like
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<p>forename : adam</p>
<p>surname : example</p>
<p>age : 90</p>
<p>gender : male</p>
</div>
<div class = "jumbotron">
<p>forename : john</p>
<p>surname : example</p>
<p>age : 90</p>
<p>gender : male</p>
</div>
</div>
</div>
I have a list of catagores found at the bottom of THIS PAGE
this code:
<?php
include_once('include/article.php');
$article = new article;
$articles = $article->fetch_all();
?>
<?php foreach ($articles as $article) { ?>
<div id="content">
<a href="list.php?id=<?php echo $article['promo_cat']; ?>">
<li class="button"><ul class="pageitem">
<?php echo $article['promo_cat']; ?>
</li></ul></div>
</a>
<?php } ?>
displays the value of the promo_cat field in my mysql table as a list:
as you can see. there are 2 "FREE" fields. How do I edit this code so that it does not show up any duplicates?
I understand that I need to use a DISTINCT feature but im not sure how to. Please help.
thank you.
If you require any more coe from another page then please ask and I will edit this post and add it.
SELECT DISTINCT `promo_cat` FROM mobi WHERE `something` = 'something'
More on DISTINCT from http://forums.mysql.com/
for future reference.
changing my above code to:
<?php
include_once('include/article.php');
$category = new category;
$articles = $category->fetch_all();
?>
<?php foreach ($articles as $article) { ?>
<div id="content">
<a href="list.php?id=<?php echo $article['promo_cat']; ?>">
<li class="button"><ul class="pageitem">
<?php echo $article['promo_cat']; ?>
</li></ul></div>
</a>
<?php } ?>
and adding a new CLASS to my /include/article.php that reads:
class category {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT DISTINCT `promo_cat` FROM mobi");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($promo_cat) {
global $pdo;
$query = $pdo->prepare("SELECT DISTINCT * FROM mobi WHERE `something` = 'something'");
$query->bindValue(1, $promo_cat);
$query->execute();
return $query->fetch();
}
}
Fixed my problem.
thank you.