im试图在php中创建一个循环,弹出以下内容
$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);
我目前已经这样做
$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);
}
}
}
}
但是我得到了一个未定义的变量,例如“未定义的变量:C:\ xampp \ htdocs \ AniBuy \ pages \ index.php中的ons 在第90行”
请帮助我! :)
对不起,如果我添加了html / php代码,这可能会有所帮助
<?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; ?>
有不止一个,那么就有许多这样的论点从phpmyadmin数据库中吐出了img标题等
<?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; ?>
到目前为止,我已经尝试过了
$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;
}
但这仍然给我错误 mysqli_fetch_assoc()期望参数1为mysqli_result,字符串在 C:\ xampp \ htdocs \ AniBuy \ pages \ index.php 在第142行
如果有人对网站的外观感兴趣,那么这里是仅包含html,css和js的网址:https://54x1.github.io/AniBuy/pages/indexv2.html
这是带有php和mysql的文件,我在anibuy文件夹旁边添加了sql导出,其文件夹是... / pages / index.php:https://drive.google.com/drive/folders/1mUYm2sH1bsxF3UaCxAvOIwTkubLCoVod?usp=sharing
答案 0 :(得分:2)
您的问题是您正在使用字符串作为代码,例如:
"$".$t = "SELECT * FROM products WHERE $v = $v2";
和:
"$".$t2 = $db->query("$".$t);
您需要做的是将代码放入eval()函数中,如下所示:
eval('$'.$t.' = "SELECT * FROM products WHERE $var$v = $v2";');
我认为您正在使用的所有foreach循环都会遇到另一个问题,我想因为所有数组都具有相同数量的元素,因此您应该执行以下操作:
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]);');
}
尝试一下,告诉我如何为您解决
答案 1 :(得分:1)
在查询字符串中时,将$ v像这样的{$ v}换行。
您还应该重新设计阵列:
$vars = ["ons" => 1, "popa" => 1, ... ];
$results = [];
foreach($vars as $vkey => $vval){
$results[$vkey] = $db->query("SELECT * FROM products WHERE {$vkey} = {$vval}")->fetch_assoc();
}
编辑:添加fetch_assoc()调用,假设您只返回一行。否则,您将不得不使用嵌套的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;
}
}
答案 2 :(得分:0)
在您的foreach循环中,$ v变量是什么?似乎没有在任何地方定义它。
"$".$t = "SELECT * FROM products WHERE $v = $v2";
答案 3 :(得分:0)
给出您的评论,您已经有了正确的基本概念,但是实现实际上比您所拥有的要简单得多。首先,您的“控件”值,即数组$vart
等采用的格式为Structure of Arrays,而使用结构数组可能会发现这容易得多。 / p>
数组结构存在的问题是,当您真的想同时并行处理所有四个数组时,有多个嵌套循环试图遍历它们-如果您使用结构数组。
因此,将数据更改为如下所示:
$vars = array(
array(
"select" => "ons",
"compare" => "1",
"result" => "ons"
),
// etc. etc.
array(
"select" => "bests",
"compare" => "2",
"result" => "bests2"
),
// ...
select
处于SELECT
子句的左侧,compare
将是右侧,而result
是数据到达的地方。
现在像这样制作循环:
foreach ($vars as $sqldata) {
$select = $sqldata->select;
$compare = $sqldata->compare;
$result = $sqldata->result;
$$result = $db->query("SELECT * FROM products WHERE $select = $compare");
}
是的,我的确在最后一行表示两个'$'。请注意,这将带您到达想要的地方。输入此变量之前,您需要做一些事情来使目标变量存在,否则,退出您的$$result
循环后,foreach
中的结果将超出范围。
或者,或者在开始之前创建一个空数组,然后将结果存储在该数组中。
答案 4 :(得分:0)
在您这边触发的错误很奇怪,因为当我在一边运行您的代码时,我没有得到它。
在第一个数组中添加所有字段名称和值,其中 on 是字段名称,而 1 是 value < / em>用于where条件。为其他人做同样的事情。
$key
=数据库中的字段名称
$value
=字段名称的值
// 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>