Php 这里使用的最佳阵列/循环类型是什么?
假设我有一个简单的标记Php 这里使用的最佳阵列/循环类型是什么?,php,html,arrays,Php,Html,Arrays,假设我有一个简单的标记 <div class="wrapper" id=" <?php echo $id; ?> "> <div class="title"> <?php echo $title; ?> </div> <div class="inner-text"> <?php echo $inner-text; ?> </div> </div> 假设您通过查询检索数据 $que
<div class="wrapper" id=" <?php echo $id; ?> ">
<div class="title"> <?php echo $title; ?> </div>
<div class="inner-text"> <?php echo $inner-text; ?> </div>
</div>
假设您通过查询检索数据
$query = "SELECT id, title, inner_text FROM table";
$result = mysqli_query($link,$query); //Be sure to establish the mysqli instance in your header or configuration file.
while($row = mysqli_fetch_assoc($result))
{
echo '<div class="wrapper" id="'. $row['id'] .'">';
echo '<div class="title">'. $row['title']; .'</div>';
echo '<div class="inner-text">'. $row['inner_text'] .'</div>';
echo '</div>';
}
$query=“从表中选择id、标题、内部文本”;
$result=mysqli\u查询($link,$query)//确保在头文件或配置文件中建立mysqli实例。
while($row=mysqli\u fetch\u assoc($result))
{
回声';
回显“.$row['title'];”;
回显'.$row['inner_text'].';
回声';
}
这是使用php生成html代码的基本方法之一。另一种方法是使用带有预填充数据的嵌套数组
您可以在数组上使用foreach循环,并以相同的方式生成它。我不知道您是否有预构建的数组,但如果您向我展示,我可以编辑我的答案并给出一个示例。使用数组创建数组:
$array = array(
array('id' => 1,
'title' => 'somename'),
'innertext' => 'sometext'),
array('id' => 1,
'title' => 'somename'),
'innertext' => 'sometext')
);
然后你就可以很容易地看它,就像
foreach($array as $row) {
$id = $row['id'];
$title = $row['title'];
$innertext = $row['innertext'];
}
首先,您应该将这些变量放入数组中。例如:$data=array('id'=>someid,
'title'=>sometitle,….等等
)
下一步,您应按以下方式进行foreach循环:
<?php foreach($data as $rows_of_data_array) : ?>
<div class="wrapper" id=" <?=$rows_of_data_array['id']; ?> ">
<div class="title"> <?=$rows_of_data_array['title']; ?> </div>
<div class="inner-text"> <?=$rows_of_data_array['inner-text']; ?> </div>
</div>
<?php endforeach?>
如果您有一个对象数组:
foreach($myArrayOfObjects as $object){
?>
<div class="wrapper" id="<?php echo $object->id; ?> ">
<div class="title"><?php echo $object->title; ?> </div>
<div class="inner-text"><?php echo $object->innerText; ?> </div>
</div>
<?php
}
foreach($myArrayOfObjects作为$object){
?>
使用多维数组
$array=array(
array(0 =>'id', 1 =>'title', 2=>'inner-text'),
array(0 =>'id1', 1 =>'title1', 2=>'inner-text1'),
array(0 =>'id2', 1 =>'title2', 2=>'inner-text2')
);
$c=count($array); //the value of this is 3
for($i=0; $i<$c; $i++){
foreach($array as $key => $value){
echo $array[$i][$key]. "<br />";
}
}
$array=array(
数组(0=>'id',1=>'title',2=>'internal-text'),
数组(0=>'id1',1=>'title1',2=>'internal-text1'),
数组(0=>'id2',1=>'title2',2=>'internal-text2')
);
$c=count($array);//此值为3
对于($i=0;$i$value){
echo$array[$i][$key]。“
”;
}
}
添加$titles、$ids等的示例数据您的意思是,如何使用数组和循环?只是,嗯,使用它们。没那么难。你能给我们举一个你的数据的例子吗?你所指的标题、id和内部文本的长列表是基于观点的,选择一个:foreach
,while
,for
在问题中尽量不要使用“best”,因为基于观点的问题在SE上是离题的,因为观点可能会有所不同,或者可能导致聊天讨论。
for ($i= 0; $i < count($ids), $i++){
?>
<div class="wrapper" id="<?php echo $ids[$i]; ?> ">
<div class="title"><?php echo $titles[$i]; ?></div>
<div class="inner-text"><?php echo $innerTexts[$i]; ?> </div>
</div>
<?php
}
$array=array(
array(0 =>'id', 1 =>'title', 2=>'inner-text'),
array(0 =>'id1', 1 =>'title1', 2=>'inner-text1'),
array(0 =>'id2', 1 =>'title2', 2=>'inner-text2')
);
$c=count($array); //the value of this is 3
for($i=0; $i<$c; $i++){
foreach($array as $key => $value){
echo $array[$i][$key]. "<br />";
}
}