【问题标题】:php columns from xml [duplicate]来自xml的php列[重复]
【发布时间】:2015-06-17 08:24:26
【问题描述】:

如何在index.php 中创建列?现在只需在一列中显示所有内容。我想要 3 或 4 列 10 行之类的东西。

我正在使用此代码:

<?php
$url = file_get_contents('https://somesite.xml');
$xml = new SimpleXMLElement($url);
$data = $xml->entry;
for($i = 0; $i < count($data); $i++){
$title = $data[$i]->title;
$title = preg_replace('/[^A-Za-z0-9\-]/', '-', $title);
$title = preg_replace('/-+/', '+', $title);
$item = $data[$i]->content;
$a = preg_match_all('#src="(.*?)"#',$item,$isi);
$img = $isi[1][0];
echo '<br><a href="download.php?q='.$title.'"><img src="'.$img.'"title='.$title.' /></br>';

【问题讨论】:

  • 这更像是一个 HTML/CSS 问题,而不是 PHP/XML。我认为您应该删除 &lt;br&gt;s 并浮动锚元素。
  • 我这方面不太好,能举个例子吗?

标签: php xml


【解决方案1】:

您可以这样做,在本地样本值上进行测试可能需要一些调整。你的链接也没有被关闭,可能是错字?

<?php
$url = file_get_contents('https://somesite.xml');
$xml = new SimpleXMLElement($url);
$data = $xml->entry;
for($i = 0; $i < count($data); $i++){
    $title = $data[$i]->title;
    $title = preg_replace('/[^A-Za-z0-9\-]/', '-', $title);
    $title = preg_replace('/-+/', '+', $title);
    $item = $data[$i]->content;
    $a = preg_match_all('#src="(.*?)"#',$item,$isi);
    $img = $isi[1][0];
    echo '<a style="float:left; display:block;" href="download.php?q='.$title.'"><img src="'.$img.'"title='.$title.' /></a>';
    if($i % 4 == 0 && $i > 1) {
        echo '<div style="clear:both;" />';
    }   
}
if($i % 4 != 0) {
     //we didnt clear we should clear so other contents on the page arent floated
     echo '<div style="clear:both;" />';
}
?>

这是为了每行显示 4 个链接,如果你想要 3 个,则将 $i % 4 更改为 $i % 3,以此类推。该运算符称为模数,这里有一个文档http://php.net/language.operators.arithmetic

【讨论】:

  • 这把我的网站从上到下弄得一团糟:D
  • 您的图片大小不同吗?
  • 不,大小相同。 175x175
  • 嗯,你能把你网站的截图或链接发上来吗?
  • 使用模数可能是一种正确的方法,但不要错过在循环之后清除浮点数。
猜你喜欢
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
相关资源
最近更新 更多