I would understand if there is a way to store the variables out of the while loop.
For example, I have a slide show that shows the images for each category present and a button to see the images gallery related to the category. The button, for graphical issues, should stay out of the CSS class that contains the while.
This is the current code:
<div class="carousel-inner"> <!-- start carousel -->
$macrocat = "SELECT * FROM tbl_category WHERE cat_parent_id = 0";
$result = dbQuery($macrocat);
while($row = dbFetchAssoc($result)) {
extract($row);
echo $cat_name;
echo $cat_description;
}
</div> <!-- end carousel -->
and the class out of the while where I have to get cat_id:
<div class="social-caption">
<a class="btn btn-large btn-primary" href="categorie.php?id=<?php echo $cat_id; ?>">Gallery</a>
</div>
How can I fix the problem?
Thanks!
Store the categories first:
<div class="carousel-inner"> <!-- start carousel -->
$macrocat = "SELECT * FROM tbl_category WHERE cat_parent_id = 0";
$cats = array[];
$result = dbQuery($macrocat);
while($row = dbFetchAssoc($result)) {
extract($row);
echo $cat_name;
echo $cat_description;
$cats[] = $cat_id;
}
</div> <!-- end carousel -->
Then later you can use them:
foreach($cats as $cat)
{
echo "<div>".$cat."</div>";
}
Related
I tried to add a pagination script to my existing php page of sql queries.
But after adding the script the page is kept on loading without showing any content or error.
My code goes as:
<?php include('db.php'); ?>
<?php // define how many results you want per page
$results_per_page = 10;
// find out the number of results stored in database
$sql10='SELECT * FROM smf_messages';
$result10 = mysqli_query($conn, $sql10);
$number_of_results = mysqli_num_rows($result10);
// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
?>
Now the sql query codes to get the data from the respective tables...
<?php
$sql2 = "SELECT * FROM smf_log_digest WHERE note_type = 'topic' ORDER BY id_msg DESC LIMIT 420";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$number = $row2["id_msg"];
?>
This query relates to the content from which table to be retrieved..
<?php
// retrieve selected results from database and display them on page
$sql20='SELECT * FROM smf_messages WHERE id_msg = $number AND id_board = 4 LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result20 = mysqli_query($conn, $sql20);
while($row20 = mysqli_fetch_array($result20)) {
$member = $row20["id_member"];
$replies = $row20["id_topic"];
?>
<?php
$sqlstep1 = "SELECT COUNT(*) AS total FROM smf_log_digest WHERE note_type = 'reply' AND id_topic = $replies";
$rowNumstep1 = mysqli_query($conn, $sqlstep1);
$countstep1 = mysqli_fetch_assoc($rowNumstep1);
?>
// Body
<article class="well btn-group-sm clearfix">
<div class="topic-desc row-fluid clearfix">
<div class="col-sm-2 text-center publisher-wrap">
<img src="assets/images/profile.png" alt="" class="avatar img-circle img-responsive">
<h5><?php echo $row3["poster_name"]; ?></h5>
<small class="online">Member</small>
</div>
<div class="col-sm-10">
<header class="topic-footer clearfix">
<!-- end tags -->
</header>
<!-- end topic -->
<h4> <?php echo $row20["body"]; ?></h4>
<div class="blog-meta clearfix">
<small><?php echo $countstep1["total"]; ?> Replies</small>
<small><?php echo date('m/d/Y', $row20["poster_time"]); ?></small>
</div>
View the topic →
</div>
</div>
</article>
//end of body
<?php
}
// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
?>
<?php }
} else {
echo "";
}
?>
Please note that the data base connections are all checked and are right..
Any help is appreciated..
add this on top then check for error.
error_reporting(E_ALL);
ini_set('desplay_errors','1');
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 am creating a forum and the code I am putting in this thread is the page that shows all of the forum categories. I have this setup where I have an id, category_section, category_title, and category_description. The way it is structured now is that it creates a new category section border every time I add a new category title in my database. This is what I mean by this:
As an example:
My category section is called Shoes, my category titles are Nike, Adidas, Reebok
Currently, it does:
Shoes
Nike
Shoes
Adidas
Shoes
Reebok
But it should do this:
Shoes
Nike
Adidas
Reebok
How can I get my current code to do this?
$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_title DESC");
$numrows = mysqli_num_rows($query);
$categories = "";
if($numrows > 0){
while($forum_row = mysqli_fetch_assoc($query)){
$categoryid = $forum_row['id'];
$category_section = $forum_row['category_section'];
$categoryTitle = $forum_row['category_title'];
$categoryDescription = $forum_row['category_description'];
$categories = "<a href='forum_view_category.php?cid=".$categoryid."'>"
. $categoryTitle . "</a>";
//$categories .= "<a href='forum_view_category.php?cid=".$categoryid
//."' class='cat_links'>" . $categoryTitle . "</a>" " - " . $categoryDescription .;
?>
<div class="category_section">
<?php echo $category_section; ?>
</div>
<div class="category_border">
<div class="discussions_left">
<div class="discussions_category_title">
<?php echo $categories; ?>
</div>
<div class="discussions_category_description">
<?php echo $categoryDescription; ?>
</div>
</div>
<div class="discussions_right">
</div>
</div>
<?php
}
//echo $categories;
} else {
echo "<p>There are no posted categories available yet. </p>";
}
I would change your SQL to order by section first, then the title.
SELECT * FROM forum_categories ORDER BY category_section DESC, category_title DESC
After you've done that, define an if statement to check if the current section is the same as the previous statement.
If so, move one.
If not, echo the new section title.
I've cleaned up your code a bit and switched to the Alternative syntax for control structures for readability and maintainability.
PHPFiddle Demo (faked MySQL)
<?php
$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_section DESC, category_title DESC");
$category_section = "";
?>
<?php if(mysqli_num_rows($query) > 0): ?>
<?php while($cat = mysqli_fetch_assoc($query)): ?>
<?php if($cat['category_section'] !== $category_section): ?>
<?php $category_section = $cat['category_section'] ?>
<div class="category_section">
<?= $category_section ?>
</div>
<?php endif; ?>
<div class="category_border">
<div class="discussions_left">
<div class="discussions_category_title">
<a href="forum_view_category.php?cid=<?= $cat['id'] ?>">
<?= $cat['category_title'] ?>
</a>
</div>
<div class="discussions_category_description">
<?= $cat['category_description'] ?>
</div>
</div>
<div class="discussions_right">
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<p>There are no posted categories available yet.</p>
<?php endif; ?>
First of all, I am new to mysqli and prepare statements so please let me know if you see any error. I have this static drop down menu :
HTML code:
<ul class="menu sgray fade" id="menu">
<li>Bike
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>
<li>bikes</li>
<li>wheels</li>
<li>helmets</li>
<li>components</li>
</ol>
</div>
<div class="col1">
<ol>
<li>pedals</li>
<li>GPS</li>
<li>pumps</li>
<li>bike storage</li>
</ol>
</div>
<div class="col1">
<ol>
<li>power meters</li>
<li>hydratation system</li>
<li>shoes</li>
<li>saddles</li>
</ol>
</div>
</div>
<!-- end mega menu -->
</li>
I want to make a dynamic dropdown menu. I managed to show the $categoryName and the $SubCategoryName with this function:
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
while($Substmt->fetch()) {
echo'
<li>'.$SubCategoryName.'</li>';
}
echo'
</ol>
</div>
<!-- end mega menu -->
</li>';
}
}
The only problem is that it returns all the subcategories on the the same <div class="col1">:
what I would like to obtain is count the subcategories and if the result is more than 4 return the other items in the second and third column.
UPDATE***: thanks to the answer below now the menu looks like this:
thanks!
How about try this?
To explain further
What is happening is that for every subcategory fetched, I increment a counter. If that counter hits 4, it ends the <UL> and <DIV> and creates a new one which will represent the new column.
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
$count = 0;
while($Substmt->fetch()) {
echo'
<li>'.$SubCategoryName.'</li>';
$count+=1;
if ($count == 4) {
$count = 0;
echo '</ol></div><div class="col1"><ol>';
}
}
echo'
</ol>
</div>
<!-- end mega menu -->
</li>';
}
}
EDIT: Misunderstood the purpose of col1. They all should be col1 and should work now. If not, leave me a comment!
Try this:
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
echo '<div class="cols3">';
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
while($Substmt->fetch()) {
echo'<li>'.$SubCategoryName.'</li>';
}
echo'</ol>';
}
echo '</div><!-- end mega menu --></li>';
}
I am currently in a pinch and need a tweak to some existing code on my site. There is a PHP SQL query that needs to be modified to pull products from a specific category. This list then fed into an HTML product carousel. Anyway, currently it's pulling in all products with an image.
<?php
$query = mysql_query("SELECT * FROM `catalog_product_flat_1` WHERE `small_image` != 'no_selection'");
//$query = mysql_query("SELECT * FROM `catalog_product_flat_1` WHERE `category_ID` = 16");
while($fins = mysql_fetch_array($query)) {
?>
The commented line of code is what I was trying to do, but had no luck. I'm shooting in the dark here. I'm no Magento or PHP expert by any standards. FYI, The front end shows the category ID being 16 for the category that I want. It would be nice to pull it by name, but I'm ok with using ID for simplicity/speed sake.
Thanks for any assistance.
(UPDATE 8-27)
Entire HTML Code Block:
<div class="whiteGradientLarge span-10">
<div class="content">
<h2>Latest Products</h2>
<div class="span-10">
<img src="assets/arrowPrev.png" />
<ul class="productSlider push-1">
<?php
//$query = mysql_query("SELECT * FROM `catalog_product_flat_1` WHERE `small_image` != 'no_selection'");
//while($products = mysql_fetch_array($query)) {
$category = new Mage_Catalog_Model_Category();
$category->load('16'); //your category id here, 16 $catid
$prodCollection = $category->getProductCollection();
foreach ($prodCollection as $product) {
//get all the product information here... //}
?>
<li class="roundabout-moveable-item">
<h3><?php echo $product['name']; ?></h3>
<img src="https://www.mywebsite.com/store/media/catalog/product<?php echo $product['small_image']; ?>" />
<span class="productTitle span-3"><?php ?></span>
</li>
<?php
} ?>
</ul>
<img src="assets/arrowNext.png" />
</div>
</div>
</div>
$category = new Mage_Catalog_Model_Category();
$category->load($catid); //your category id here, 16
$prodCollection = $category->getProductCollection();
foreach ($prodCollection as $product) {
//get all the product information here...
?>
<li class="roundabout-moveable-item">
<h3><?php echo $product->getName(); ?></h3>
<img src="https://www.mywebsite.com/store/media/catalog/product<?php echo $product->getSmallImage(); ?>" />
<span class="productTitle span-3"><?php ?></span>
</li>
<?php
}