Php 多个for循环-如何打印数据
我想创建一个类似popurls.com的网站,但我将使用MySQL数据库中存储的静态数据。顺便说一句,我使用php/mysql。Php 多个for循环-如何打印数据,php,for-loop,Php,For Loop,我想创建一个类似popurls.com的网站,但我将使用MySQL数据库中存储的静态数据。顺便说一句,我使用php/mysql。 在每个列表中,我想显示大约10个链接(就像popurls上的链接一样)。在这种情况下,如果我有20个列表,我需要做20个“for”循环(针对每个特定列表)。 我的问题是,;有没有更好的方法来打印这20个列表,而不是在php中使用20个“for”循环。php的foreach很大程度上取决于您的数据输入,但我可以想象这样的情况: <?php $lists = a
在每个列表中,我想显示大约10个链接(就像popurls上的链接一样)。在这种情况下,如果我有20个列表,我需要做20个“for”循环(针对每个特定列表)。
我的问题是,;有没有更好的方法来打印这20个列表,而不是在php中使用20个“for”循环。php的
foreach
很大程度上取决于您的数据输入,但我可以想象这样的情况:
<?php
$lists = arrray('list1', 'list2', 'list3');
foreach ($lists as $current) {
$data = fetch_data_from_mysql($current);
foreach ($data as $link) {
echo "<a href=\"$data\">Link</a>";
}
}
function fetch_data_from_mysql($current)
{
$list_data = array();
// do whatever is required to fetch the list data for item $current from MySQL
// and store the data in $list_data
return $list_data;
}
$list_query = mysql_query("SELECT * FROM lists";)
while( $list = mysql_fetch_array($list_query) )
{
echo "<h1>{$list['title']}</h1>";
$query = mysql_query("SELECT * FROM entries WHERE list_id = {$list['id']}");
while( $entry = mysql_fetch_array($query) )
{
echo "- {$entry['name']}<br />";
}
}
您只需要两个foreach循环。假设您从mysql表中获取数据(如您所写),可能是这样的:
<?php
$lists = arrray('list1', 'list2', 'list3');
foreach ($lists as $current) {
$data = fetch_data_from_mysql($current);
foreach ($data as $link) {
echo "<a href=\"$data\">Link</a>";
}
}
function fetch_data_from_mysql($current)
{
$list_data = array();
// do whatever is required to fetch the list data for item $current from MySQL
// and store the data in $list_data
return $list_data;
}
$list_query = mysql_query("SELECT * FROM lists";)
while( $list = mysql_fetch_array($list_query) )
{
echo "<h1>{$list['title']}</h1>";
$query = mysql_query("SELECT * FROM entries WHERE list_id = {$list['id']}");
while( $entry = mysql_fetch_array($query) )
{
echo "- {$entry['name']}<br />";
}
}
$list\u query=mysql\u query(“从列表中选择*”)
而($list=mysql\u fetch\u数组($list\u query))
{
echo“{$list['title']}”;
$query=mysql\u query(“从列表id={$list['id']}中的条目中选择*);
while($entry=mysql\u fetch\u数组($query))
{
echo“-{$entry['name']}
”;
}
}
您可以从数据库中获取所有信息,并将其解析为数组,如
array[<news type1>] = array( link1, link2, link3, etc);
array[<news type2>] = array( link1, link2, link3, etc);
array[]=数组(link1、link2、link3等);
数组[]=数组(link1、link2、link3等);
等等
在布局上,您可以使用
foreach ($newsCategory AS $categoryLinks) {
foreach ($categoryLinks AS $newsLink) {
<show the link and / or extra data>
}
}
foreach($newsCategory作为$CategoryLink){
foreach($categoryLinks作为$newsLink){
}
}
只需将链接存储在二维数组中即可。这样,您必须在特定列表中创建1个外部循环(对列表进行迭代)和1个内部循环(对链接进行迭代)
$links = array(
'science' => array('link1', 'link2', ...),
'sports' => array('link1', 'link2'),
// ... and so on
);
foreach ($links as $category => $urls) {
echo "Links in category: $category\n";
foreach ($urls as $url) {
echo $url . "\n";
}
}
一个for
循环或一个foreach
循环可以很好地工作,但是如果您只创建一个for循环并将内容推送到一个数组或字符串数组中,那么编码就会少很多。。。然后,您可以对实际内容执行任何您想要的操作(假设我们是按列类别
分组的。我将使用一个使用字符串数组的示例(我引用的查询在这里解释:)
当然,如果它是真的,我会做面向对象的;)