Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2012, 12:07 PM   PM User | #1
denhamd2
New Coder

 
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
denhamd2 is an unknown quantity at this point
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?
denhamd2 is offline   Reply With Quote
Old 12-19-2012, 12:59 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Post the code you are using to generate the table HTML.
Fou-Lu is offline   Reply With Quote
Old 12-19-2012, 03:20 PM   PM User | #3
denhamd2
New Coder

 
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
denhamd2 is an unknown quantity at this point
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
denhamd2 is offline   Reply With Quote
Old 12-19-2012, 04:00 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-19-2012, 07:54 PM   PM User | #5
denhamd2
New Coder

 
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
denhamd2 is an unknown quantity at this point
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!
denhamd2 is offline   Reply With Quote
Old 12-19-2012, 08:32 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-19-2012, 09:14 PM   PM User | #7
denhamd2
New Coder

 
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
denhamd2 is an unknown quantity at this point
Thumbs up

Awesome thanks it works :
denhamd2 is offline   Reply With Quote
Old 12-19-2012, 09:22 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:52 AM.


Advertisement
Log in to turn off these ads.