Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Reformatting the contents of table rows

    Hi,

    I'm trying to take the content from table rows and reformat it.

    You can see a sample structure of the HTML below:

    Code:
    <table><tr>			
    <td class="fixData" align="right" width="40%">Team 1</td><td class="fixData" align="center" width="10%">&nbsp;v&nbsp;</td><td class="fixData" align="left" width="40%">Team 2</td>						 <td class="fixData" align="right" width="10%"><a href="#">Results</a></td>
    </tr><tr>			
    <td class="fixData" align="right" width="40%">Team 3</td><td class="fixData" align="center" width="10%">&nbsp;v&nbsp;</td><td class="fixData" align="left" width="40%">Team 4</td>						 <td class="fixData" align="right" width="10%"><a href="#">Results</a></td>
    </tr></table>
    I'm trying to change it to:

    <tr>
    <td class="newfixData"><a href="result.php?Team1-Team2">Team 1 v Team 2</a></td>
    </tr>


    Any ideas how I could do this?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Post the code you are using to generate the table HTML.

  • #3
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Post the code you are using to generate the table HTML.
    I'm not generating the HTML, I don't have access to the code that generates it. All the HTML is stored in a variable called $html

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    So you got this by scraping it in some way?
    You can use something like the dom to extract that. SimpleXML can probably do it too (I don't use simplexml so I can't tell you what to do with that):
    PHP Code:
    $str '<table><tr>            
    <td class="fixData" align="right" width="40%">Team 1</td><td class="fixData" align="center" width="10%">&nbsp;v&nbsp;</td><td class="fixData" align="left" width="40%">Team 2</td>                         <td class="fixData" align="right" width="10%"><a href="#">Results</a></td>
    </tr><tr>            
    <td class="fixData" align="right" width="40%">Team 3</td><td class="fixData" align="center" width="10%">&nbsp;v&nbsp;</td><td class="fixData" align="left" width="40%">Team 4</td>                         <td class="fixData" align="right" width="10%"><a href="#">Results</a></td>
    </tr></table>'
    ;
    $dom = new DOMDocument();
    $dom->loadHTML($str);

    $trs $dom->getElementsByTagName('tr');
    foreach (
    $trs AS $tr)
    {
        
    $children $tr->childNodes;
        
    $team1 $children->item(0)->nodeValue;
        
    $team2 $children->item(2)->nodeValue;
        
    printf('%s vs %s' PHP_EOL$team1$team2);

    So long as its wellformed, you should be able to import it through the dom. Then simply format that printf accordingly.

  • #5
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks very much for your help. I've tried using phpquery to do it:

    Code:
    foreach(pq('.body table tr') as $tag) {
    	
       foreach(pq($tag)->find('td.fixData') as $td) {
    	      
    	   $x = pq($td)->text();
    	
      echo '<p>'.$x.'</p>';
       }
    }
    However what this gives is the content of every <td> tag put into its own <p> tag. What I'm trying to achieve is to have the content each of the 4 sets of <td> tags within each <tr> to be put into a single <p> tag each.

    eg.
    Code:
    <p>Team 1 v Team 2 Results</p> rather than <p>Team 1</p><p>v</p><p>Team 2</p><p>Results></p>
    Any ideas?

    Thanks again!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I haven't a clue what PHPquery is, but it looks to be a port of jquery.
    I'd say cut the fat out of the PHP and just stick with the dom (since if I had to guess they're using the dom anyway).
    If you replace the printf with this:
    PHP Code:
        $aDisplay = array(
            
    str_replace(' '''$team1),
            
    str_replace(' '''$team2),
            
    $team1,
            
    $team2);
        
    vprintf('<tr>
            <td class="newfixData"><a href="result.php?%s-%s">%s v %s</a></td>
        </tr>'
    $aDisplay); 
    You end up with this (which is consistent for the first example):
    Code:
    <tr>
            <td class="newfixData"><a href="result.php?Team1-Team2">Team 1 v Team 2</a></td>
        </tr><tr>
            <td class="newfixData"><a href="result.php?Team3-Team4">Team 3 v Team 4</a></td>
        </tr>
    So long as the html stays consistent and is always nested as tr::td, then you can retrieve whatever you want from the $children->item(x) where x would be the 0 based td offset for each of the tr's. You can add as you need based on what you choose for the item.

  • #7
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    Awesome thanks it works :

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep your welcome. If you need more fine grained control (noticing that the phpquery has an attribute lookup bind), you can use the xpath for that. DOM can do it with node handling, but IMO it looks disastrous to get at that level :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •