im trying to make a loop in php that spits out the below
$sqlons = "SELECT * FROM products WHERE ons = 1";
$ons = $db->query($sqlons);
$sqlpopa = "SELECT * FROM products WHERE popa = 1";
$popa = $db->query($sqlpopa);
$sqlnewr = "SELECT * FROM products WHERE newr = 1";
$newr = $db->query($sqlnewr);
$sqlpopm = "SELECT * FROM products WHERE popm = 1";
$popm = $db->query($sqlpopm);
$sqlpopm2 = "SELECT * FROM products WHERE popm = 2";
$popm2 = $db->query($sqlpopm2);
$sqlfeata = "SELECT * FROM products WHERE feata = 1";
$feata = $db->query($sqlfeata);
$sqlfeata2 = "SELECT * FROM products WHERE feata = 2";
$feata2 = $db->query($sqlfeata2);
$sqlbests = "SELECT * FROM products WHERE bests = 1";
$bests = $db->query($sqlbests);
$sqlbests2 = "SELECT * FROM products WHERE bests = 2";
$bests2 = $db->query($sqlbests2);
$sqlbests3 = "SELECT * FROM products WHERE bests = 3";
$bests3 = $db->query($sqlbests3);
$sqlsea = "SELECT * FROM products WHERE sea = 1";
$sea = $db->query($sqlsea);
i've currently done this
$vart = array("sqlons", "sqlpopa", "sqlnewr", "sqlpopm", "sqlfeata", "sqlbests", "sqlsea");
$vart2 = array("ons", "popa", "newr", "popm", "feata", "bests", "sea");
$var = array("ons", "popa", "newr", "popm", "popm", "feata", "feata", "bests", "bests", "bests", "sea");
$var2 = array("1", "1", "1", "1", "2", "1", "2", "1", "2", "3", "1");
foreach($var as $v){
foreach($var2 as $v2){
foreach($vart as $t){
"$".$t = "SELECT * FROM products WHERE $v = $v2";
foreach($vart2 as $t2){
"$".$t2 = $db->query("$".$t);
}
}
}
}
but i get an undefined variable such a "Undefined variable: ons in C:\xampp\htdocs\AniBuy\pages\index.php on line 90"
please help me! :)
sorry this might of helped if i added the html/php code
<?php while($product = mysqli_fetch_assoc($ons)) : ?>
<div class="<?= $product['class']; ?>">
<div class="grid-col-1">
<img class="featured-image img-responsive" src="<?= $product['img']; ?>">
</div>
<div class="grid-col-2">
<h2 class="featured-heading"><?= $product['title']; ?></h2>
<div class="grid-col-3">
<div class="span-text"><?= $product['about']; ?><br></div><div class="buy-now"><a href="#">BUY NOW</a></div>
</div>
</div>
</div>
<?php endwhile; ?>
there is more then just one there are multiple of theses that spit out the img title ect each from the phpmyadmin database
<?php while($product = mysqli_fetch_assoc($popa)) : ?>
<div class="<?= $product['class']; ?>">
<div class="buy-now buy"><a href="#">BUY NOW</a></div>
<div h4 class="read-more" onclick="rmModal(<?= $product['id'];?>)"><a href="#rmModal-<?= $product['title'];?>.php">READ MORE</a></div>
<div class="featured-image"><img class="featured-image img-pop-anime img-responsive" src="<?= $product['img']; ?>"></div>
</div>
<?php endwhile; ?>
<?php while($product = mysqli_fetch_assoc($newr)) : ?>
<div class="<?= $product['class']; ?>">
<div class="buy-now buy"><a href="#">BUY NOW</a></div>
<div h4 class="read-more" onclick="rmModal(<?= $product['id'];?>)"><a href="#rmModal-<?= $product['title'];?>.php">READ MORE</a></div>
<div class="grid-col-1"><img class="featured-image img-new" src="<?= $product['img']; ?>"></div>
</div>
<?php endwhile; ?>
so far i have tried this
$vars = array("ons" => 1, "popa" => 1, "newr" => 1);
foreach ($vars as $key => $value) {
"$"."sql".$key = "SELECT * FROM products WHERE $key = $value";
"$".$key = $db->query("$"."sql".$key);
// echo "$"."sql".$key;
// echo $value;
// echo "$".$key;
}
but it sill gives me the error mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given in C:\xampp\htdocs\AniBuy\pages\index.php on line 142
if anyone is interested in what the website looks like here's the url it only has html, css and js: https://54x1.github.io/AniBuy/pages/indexv2.html
here are the files with php and mysql i've included the sql export in side the anibuy folder btw its .../pages/index.php: https://drive.google.com/drive/folders/1mUYm2sH1bsxF3UaCxAvOIwTkubLCoVod?usp=sharing