即时通讯试图在PHP中制作一个循环以下的循环
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
The error that was triggered on your side, it's weird because when I run your code on my side, I did not get it.
In the first array add all your fields names and values where ons is the field name and 1 is the value for the where condition. Do the same for the others.
$key
= fields names in database
$value
= value of field name
// As you have different values for bests, 1, 2, 3 and for others
// If we used bests as index ex: array('bests' => 2, 'bests' => 3). The data
// will be overwritten by the next bests as they have the same index.
// 'fieldname_value' where fieldname = ons and value = 1
$vars = array('ons_1', 'bests_1', 'bests_2', 'bests_3');
$results = array();
foreach ($vars as $key => $value) {
// Here I break the value which is ex: ons_1 into
// an array(ons, 1). This will repeat for the other values.
$values = explode('_', $value);
$sql = "SELECT * FROM products WHERE $values[0] = $values[1]"; // $values[0] = fieldname $values[1] = value_of_fieldname
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$results[$value][] = $row; // Add all rows for fieldnames (ex: ons_1, bests_1, bests_2) inside $result array
}
}
}
/**
*
* First var_dump($results); to get a clearer view of what's going on below
*
* The array $results contain an array of arrays
* Ex: $results = array ('ons_1' => array( 0 => array('class' => your_class), 'bests_1' => array( 0 => array('class' => your_class))
*
* This if line checks whether the index exist isset($results['ons_1']) and if it is not empty !empty($results['ons_1'])
* before doing the foreach loop. It would be unecessary to do a foreach loop that will trigger an error if ons_1 does
* not exist and has no data. This is why this check is done.
*
* The foreach loop on the other hand will loop through all the data present in 'ons_1' array. var_dump($result['ons_1']); to
* have an idea of what's going ons.
*
*/
?>
<div class="grid-container container-fluid ">
<div id="home-page" class="home-page">
<h2 class="featured-heading-2">On Sale Now!</h2>
<div class="home-page-onsale">
<?php if (isset($results['ons_1']) && !empty($results['ons_1'])) { ?>
<?php foreach ($results['ons_1'] as $key => $product) { ?>
<div class="<?php echo $product['class']; ?>">
<div class="buy-now buy"><a href="#">BUY NOW</a></div>
<div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
<div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
</div>
<?php } ?>
<?php }?>
</div>
<div class="breakpoint-3">
<div class="home-page-best-anime">
<h2 id="home-page-best-anime bpshow" class="featured-heading-2">Best Sellers</h2>
<div class="home-page-best-anime-row-1">
<?php if (isset($results['bests_1']) && !empty($results['bests_1'])) { ?>
<?php foreach ($results['bests_1'] as $key => $product) { ?>
<div class="<?php echo $product['class']; ?>">
<div class="buy-now buy"><a href="#">BUY NOW</a></div>
<div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
<div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
</div>
<?php } ?>
<?php }?>
</div>
<div class="home-page-best-anime-row-2">
<?php if (isset($results['bests_2']) && !empty($results['bests_2'])) { ?>
<?php foreach ($results['bests_2'] as $key => $product) { ?>
<div class="<?php echo $product['class']; ?>">
<div class="buy-now buy"><a href="#">BUY NOW</a></div>
<div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
<div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
</div>
<?php } ?>
<?php }?>
</div>
<div class="home-page-best-anime-row-3">
<?php if (isset($results['bests_3']) && !empty($results['bests_3'])) { ?>
<?php foreach ($results['bests_3'] as $key => $product) { ?>
<div class="<?php echo $product['class']; ?>">
<div class="buy-now buy"><a href="#">BUY NOW</a></div>
<div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
<div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
</div>
<?php } ?>
<?php }?>
</div>
</div>
</div>
</div>
</div>
In your foreach loop, what is the $v variable? It does not appear to be defined anywhere.
"$".$t = "SELECT * FROM products WHERE $v = $v2";
your problem is that you're using strings as code in lines like:
"$".$t = "SELECT * FROM products WHERE $v = $v2";
and:
"$".$t2 = $db->query("$".$t);
what you need to do is to put the code inside eval() function like this:
eval('$'.$t.' = "SELECT * FROM products WHERE $var$v = $v2";');
and I think you will have another problem with all the foreach loops you're using I think since all your arrays have the same number of elements you should do something like this:
for ($i=0; $i <count($vart) ; $i++) {
eval('$'.$vart[$i].' = "SELECT * FROM products WHERE $var[$i] = $var2[$i]";');
eval("$".$vart2[$i] .'= $db->query("$".$vart[$i]);');
}
try it and tell me how it worked out for you
Given your comment, you've got the correct basic idea, but the implementation can actually be a lot simpler than you've got it. First of all, your "control" values, i.e. the arrays $vart
etc. are in a format known as a Structure of Arrays, while you'll probably find this a lot easier with an Array of Structures.
The problem you've got with a Structure of Arrays is that you've got multiple nested loops trying to iterate over them, when you really want to advance along all four arrays in parallel - that problem largely vanishes if you use an Array of Structures.
So change that data to look something like this:
$vars = array(
array(
"select" => "ons",
"compare" => "1",
"result" => "ons"
),
// etc. etc.
array(
"select" => "bests",
"compare" => "2",
"result" => "bests2"
),
// ...
select
while be the left hand side of the SELECT
clause, compare
will be the right hand side, and result
will be where the data lands.
Now craft the loop like this:
foreach ($vars as $sqldata) {
$select = $sqldata->select;
$compare = $sqldata->compare;
$result = $sqldata->result;
$$result = $db->query("SELECT * FROM products WHERE $select = $compare");
}
And yes, I did mean two '$'s in the last line. That should get you where you want to be, with one caveat. You'll need to do something to get the target variables to exist before entering this, if not your results in $$result
will go out of scope the moment you exit the foreach
loop.
Either that, or create an empty array before starting, and store the results in that array as you go.
Wrap $v like this {$v} when it's in the query string.
You should also redesign your arrays:
$vars = ["ons" => 1, "popa" => 1, ... ];
$results = [];
foreach($vars as $vkey => $vval){
$results[$vkey] = $db->query("SELECT * FROM products WHERE {$vkey} = {$vval}")->fetch_assoc();
}
Edited: Added fetch_assoc() call, assuming you are only getting one row back. Otherwise you'll have to use a nested while:
$vars = ["ons" => 1, "popa" => 1, ... ];
$results = [];
foreach($vars as $vkey => $vval){
$results[$vkey] = [];
$result = $db->query("SELECT * FROM products WHERE {$vkey} = {$vval}");
while($row=$result->fetch_assoc()){
$results[$vkey][] = $row;
}
}