如何从表中选择多行并放入单独的数组?

时间:2015-11-18 16:23:26

标签: php html mysql database

只是想知道是否有人可以提供帮助。我试图从我的SQL数据库加载数据到标签,并想知道是否有人知道如何将四行数据加载到四个不同的数组。我想我的代码几乎就在那里,但有点卡住了。

或者,如果有人知道将数据从数据库加载到标签中的任何教程,这些标签也会将数据加载到每个[li]中,我们将非常感激。 谢谢你的帮助。

<?php
$query = mysql_query("SELECT title, author, date_added FROM table ORDER BY date_added DESC LIMIT 4");
$array = array();
while($row = mysql_fetch_assoc($query)){
$array[] = $row1;
?>

<div class="tabs-c">
    <ul class="tab-c-nav">               
       <li class="tab-c-active"><a href="#tab-c-1"><?php echo $row1['title'];?></a></li>
       <li><a href="#tab-c-2"><?php echo $row2['title'];?></a></li>
       <li><a href="#tab-c-3"><?php echo $row3['title'];?></a></li>
       <li><a href="#tab-c-4"><?php echo $row4['title'];?>/a></li>
  </ul> 

  <div class="tab-c-content">               
        <div class="text-details-content-half-row">
            <div class="text-details-content-row-text-left">Written by</div>
            <div class="text-details-content-row-text-right"><?php echo $row1['author']?></div>
        </div>
        <div class="text-details-content-row-space"></div>
        <div class="text-details-content-half-row">
            <div class="text-details-content-row-text-left">Date Added</div>
            <div class="text-details-content-row-text-right"><?php echo $row1['date_added']?></div>
        </div>
    </div>

3 个答案:

答案 0 :(得分:1)

我觉得你搞砸了一下。您使用的变量public class SecondandThirdHighestElement { public static void main(String[] args) { int[] arr = {1,1,2,3,8,1,2,3,3,3,2,3,101,6,6,7,8,8,1001,99,1,0}; // create three temp variable and store arr of first element in that temp variable so that it will compare with other element int firsttemp = arr[0]; int secondtemp = arr[0]; int thirdtemp = arr[0]; //check and find first highest value from array by comparing with other elements if found than save in the first temp variable for (int i = 0; i < arr.length; i++) { if(firsttemp <arr[i]){ firsttemp = arr[i]; }//if }//for //check and find the second highest variable by comparing with other elements in an array and find the element and that element should be smaller than first element array for (int i = 0; i < arr.length; i++) { if(secondtemp < arr[i] && firsttemp>arr[i]){ secondtemp = arr[i]; }//if }//for //check and find the third highest variable by comparing with other elements in an array and find the element and that element should be smaller than second element array for (int i = 0; i < arr.length; i++) { if(thirdtemp < arr[i] && secondtemp>arr[i]){ thirdtemp = arr[i]; }//if }//for System.out.println("First Highest Value:"+firsttemp); System.out.println("Second Highest Value:"+secondtemp); System.out.println("Third Highest Value:"+thirdtemp); }//main }//class $row1$row2$row3未在任何地方声明。您需要的是$row4循环来填充while,如下所示:

$array

然后您可以访问不同的行,如下所示:

$array[] = $row;

答案 1 :(得分:1)

替换

$array[] = $row1;

使用:

$array[] = $row;

然后:

<ul class="tab-c-nav">               
   <li class="tab-c-active"><a href="#tab-c-1"><?php echo $array[0]['title'];?></a></li>
   <li><a href="#tab-c-2"><?php echo $array[1]['title'];?></a></li>
   <li><a href="#tab-c-3"><?php echo $array[2]['title'];?></a></li>
   <li><a href="#tab-c-4"><?php echo $array[3]['title'];?></a></li>
</ul> 

答案 2 :(得分:0)

替换

$array[] = $row1;

有了这个

$array[] = $row;

这是一种简单的方法来实现创建表格的到达行作为数组。

然后我们可以使用像$ array [0] ['title']

这样的索引遍历每个数组