-1

How to make columns in index.php? Now just show everything in one column. I want 3 or 4 columns with 10 rows something like that.

I'm using this code:

<?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>';
2
  • This is more of an HTML/CSS question than PHP/XML. I think you should remove the <br>s and float the anchor elements though. Commented Apr 11, 2015 at 18:21
  • Im not very good in this, can you give me some example ? Commented Apr 11, 2015 at 18:24

1 Answer 1

0

You can do something like this, tested on local sample values might need some tweaking. Also your links weren't being closed, typo maybe?

<?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;" />';
}
?>

This is to display the links 4 per line, change the $i % 4 to $i % 3 if you want 3, and so on for others. That operator is called modulus, here's a doc on it http://php.net/language.operators.arithmetic.

Sign up to request clarification or add additional context in comments.

10 Comments

This messy my site from up to down :D
Are your images varying sizes?
No, its same size. 175x175
Hmm, can you post a screenshot or link of your site?
to work with modulo can be a correct approach, but don't miss to clear floats after the loop as well.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.