i want to create a pagination so here is what i tried
<section class="products">
<?php
$result_per_page=10;
$get = mysqli_query($conn," SELECT * FROM products");
$number_of_results=mysqli_num_rows($get);
if (!isset($_GET['page'])) {
$page=1;
} else{
$page=$_GET['page'];
}
$this_page_first_result=($page-1)*$result_per_page;
$get = mysqli_query($conn," SELECT * FROM products LIMIT ".$this_page_first_result.','.$result_per_page);
$number_of_results=mysqli_num_rows($get);
while ($row=mysqli_fetch_array($get)) {
$name = $row['product_name'];
$price = $row['product_price'];
$img = $row['img'];
}
$number_of_pages=ceil($number_of_results/$result_per_page);
?>
<article>
<a href="showproduct.php">
<img src="adminpanel/<?php echo $img?>" alt="" style="height:13rem;width:13rem;"></a>
<h3><?php echo $name;?></h3>
<h4>$<?php echo $price ?></h4>
Add to cart
</article>
</section>
</div>
<!-- / content -->
</div>
<?php for ($page=1; $page <=$number_of_pages ; $page++) {
echo ''.$page.'';
} ?>
</div>
<!-- / container -->
</div>
<!-- / body -->
</ul>
so here is my problem i only get 1 result from my data base no matter what i do i changed $result_per_page to random numbers some time i get another result in my database and some time give me error i tried to replace the codes around but still did not work
any one know where did i went wrong
In your while loop you're overwriting the values on each iteration. You're also only outputting one article in your HTML. Place the <article>..</article> code inside your while loop to have it printed for every iteration.
Change your while loop to:
while ($row=mysqli_fetch_array($get)) {
$name = $row['product_name'];
$price = $row['product_price'];
$img = $row['img'];
?>
<article>
<img src="adminpanel/<?php echo $img?>" alt="" style="height:13rem;width:13rem;">
<h3><?php echo $name;?></h3>
<h4>$<?php echo $price ?></h4>
Add to cart
</article>
<?php
}
and remove the single <article>...</article> block you have right now.
Related
I have the following data and would like to display it in different containers in html.
Name Price Difference Signal
CA.PA 15.85 3.5609257364073 MACD
AZN.ST 896 3.4881049471963 MACD
AMGN 258.57 1.6391533819031 SMA 50/200
The containers are winner_1. As of right now the first winner_1 display the last Name from the above table.
How can I get it to say CA.PA in the first winner_1, and AZN.ST in the second winner_1, and AMGN in the last winner_1.
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r){
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
}
?>
<div class="winner_1">
<?php echo $name; ?>
</div>
<div class="winner_1">
<?php echo $name +1; ?>
</div>
<div class="winner_1">
</div>
</div>
</div>
The page can be seen here:
https://signal-invest.com/markets-today/
One option is generated div tags using php:
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'>";
echo "<a href='#'>{$name}</a>";
echo "</div>";
}
?>
</div>
</div>
You should see following logic and try doing this way. Hopefully your problem will be resolved.
<div class = "overview">
<h1>Winners</h1>
<div class = "winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'><a href='#'> $name </a></div>";
}
?>
</div>
</div>
I'm really new to php and now I got stucked on what I thought was something really easy. Byt I can't see wheres my problem is.
I'm trying to create a webshop page that displays all my products.
To the problem!
Here is my code so far. It displays all products as expected but it closes the main and product-container before all product-cards except from the first one. How to wrap all product-cards in the same div?
$pdo = connect();
$limit = 20;
$offset = 0;
$stmt = get_all_products($pdo, $limit, $offset);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<main>
<section class="product-container">
<?php
foreach($rows as $row) {
?>
<div class="product-card">
<img class="product-image" src="<?php echo $row['Img'];?>" >
<h2 class="title"><?php echo $row['ProductName']; ?></h2>
<span class="price"><?php echo $row['Price'];?></span><span>:-</span>
</div>
</section>
</main>
<?php
}?>
You should move the closing main and section tag after the foreach loop is closed.
<main>
<section class="product-container">
<?php
foreach($rows as $row) {
?>
<div class="product-card">
<img class="product-image" src="<?php echo $row['Img'];?>" >
<h2 class="title"><?php echo $row['ProductName']; ?></h2>
<span class="price"><?php echo $row['Price'];?></span><span>:-</span>
</div>
<?php }?> <!-- Close the foreach loop here -->
</section>
</main>
How do I loop through and output all the values in the array?
The code I have stops after the first output.
$select_top = array($wpdb->get_results($wpdb->prepare( "SELECT * FROM `wp_top_voted` WHERE `todays_date`= CURDATE() ORDER BY `number_votes` DESC LIMIT 10", OBJECT )));
if ($select_top){
$i = 1;
foreach($select_top as $select_top_one){
$select_top_one->post_id;
$post_id_top[$i] = $select_top_one[$i]->post_id;
$number_votes[$i] = $select_top_one[$i]->number_votes;
$size = array(380,220);
?>
<div class="top-layout">
<div class="top-layout-row">
<div class="top-layout-cell">
<?php //remove background color. Make box look like widget.
?>
<div class="top-layout-cell">
<h4 class="top-video-title">
<a href="<?php the_permalink(); ?>">
<?php echo get_the_title( $post_id_top[$i] ); ?>
</a>
</h4>
<a href="<?php the_permalink(); ?>">
<?php echo get_the_post_thumbnail( $post_id_top[$i], $size ); ?>
</a>
<?php $i++; echo $i++; ?>
</div>
</div>
You just loop all the results and print them out. You don't need to auto increment the loop as the foreach runs for as long as there are results. This seems to be Wordpress, so take a look at the get_posts function https://codex.wordpress.org/Template_Tags/get_posts. This function works as what you're trying to do but you only need to insert your $args and then execute You'll have your array and you can loop that.
I have a problem with my search.php file to render me results...
I dont get any strings of error, but when I type an existing keyword I get no results...
The format of the results are the same as viewed in my main content on the website (grid view)...
The code:
<body>
<?php include_once("analyticstracking.php") ?>
<div class='container'> <!--Start of the container-->
<div><?php include("includes/header.php"); ?></div>
<div><?php include("includes/navbar.php"); ?></div>
<div><?php include("includes/left_col.php"); ?></div>
<div class='main_col'>
<div class='main_content'>
<?php
include("includes/connect.php");
if(isset($_GET['search'])){
$search_id = $_GET['q'];
$search_query = "SELECT * FROM games WHERE game_keywords LIKE '%$search_id%'";
$run_query = mysql_query($search_query);
echo '<table>';
$games = 0;
while($search_row = mysql_fetch_array($run_query)){
// make a new row after 9 games
if($games%9 == 0) {
if($games > 0) {
// and close the previous row only if it's not the first
echo '</tr>';
}
echo '<tr>';
}
// make a new column after 3 games
if($games%3 == 0) {
if($games > 0) {
// and only close it if it's not the first game
echo '</td>';
}
echo '<td>';
}
$game_id = $search_row['game_id'];
$game_name = $search_row['game_name'];
$game_category = $search_row['game_name'];
$game_keywords = $search_row['game_name'];
$game_image = $search_row['game_image'];
?>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span>
</div>
<?php
$games++;
}
}
?>
</table>
</div>
</div>
<div><?php include("includes/footer.php"); ?></div>
</div> <!--End of the container-->
</body>
Any idea?
EDIT:
I solved my problem, its a small mistake I made,
In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidently... now everything works perfectly :)
You have a typo in code
change code as below
if(isset($_GET['search'])){
$search_id = $_GET['search']; //$_GET['q'];
.
.
.
}
I solved my problem, its a small mistake I made, In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidentally... now everything works perfectly :)
I am using the code below to loop through all simple products of a configurable within Magento. The code shows specific colour data from each simple product.
However if there is two simple products in different sizes but both have the same colour it will echo the information about that colour twice I only need it to show it once.
<div class="colour-swatch">
<h1>Other Colours Available</h1>
<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); ?>
<div class="relative">
<?php
foreach($col as $simple_product){ ?>
<div class="container-swatch">
<img width="35" height="35" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $simple_product->getSwatch() ?>">
<div class="content">
<div class="inside-swatch-name"><?php echo $simple_product->getAttributeText('real_colour'); ?></div>
<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $simple_product->getLargeSwatch() ?>">
</div>
</div>
<?php } ?>
<?php if ($synb == 'Yes') { ?>
<div class="swatch-order">
ORDER SAMPLES
</div>
<?php } else {
//do nothing
} ?>
</div>
</div>
$colors = array();
foreach($col as $simple_product){
$color = $simple_product->getAttributeText('real_colour');
if(!in_array($color, $colors)){
$colors[] = $color; ?>
//do the rest from your foreach
Change this:
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); ?>
to this:
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions()->addGroupByAttribute('real_colour'); ?>
and see if this works.