PHP Generated table with Categories and links

I need my PHP code to print out a table filled in with this information:

RonPaul|4|http://www.ronpaul.com|Ron Paul
BarackObama|5|http://www.barackobama.com/splash/obama-clooney-you-get-started?|Barry
NewtGingrich|4|http://www.newt.org/|Newt
MittRomney|3|http://www.mittromney.com/s/mitt-ann-2012|Romney
MittRomney|2|http://en.wikipedia.org/wiki/Mitt_Romney|Wiki article about Romney
RickSantorum|4|http://en.wikipedia.org/wiki/Santorum|Wiki article about Santorum
RonPaul|2|http://en.wikipedia.org/wiki/Ron_paul|Wiki article about Paul
RonPaul|1|http://www.ronpaulforums.com/|Forums about Ron Paul

This information translates into $cat|$rating|$url|$desc

cat = category
desc = description

The table is separated into three columns: Rating, URL, and Description. This table takes the information I mentioned above and sorts it into a categories based on the names of the politicians. It then sorts lists the rating, website url, and description in the appropriate columns.

I have a huge problem. The url is supposed to generate as a link, but everything generates as a link. Also, some columns span longer than others. If there are more than one website in a category, only the first website is generated as a link and all the following rows have no links at all.

I had help with this code and I don’t understand the logic of it very well at all. Someone told me something about break logic, but I didn’t understand their explanation very well. Please, any insight will be super helpful! Here is the code:

[php]<?php

$file = file('../write/myfile.txt');

sort($file);

$main = array();

foreach($file as $key =>$value)
{
    $main[$key]=explode("|",$value);
}
//columns 
$columns = array('Rating', 'URL', 'Description');
$counter=0;       
$category='';
$newCat = true;
	   
       foreach($main as $key => $value){
           if($counter==0)
		   {
				echo "<table border=1 align=center><tr>";
				
				foreach($columns as $col)
				{
					echo "<th> $col </th>"; 
				}
				
				echo "</tr>"; 
				$counter++; 
           }
		   
           if($category !== $main[$key][0])
		   {
				$category = $main[$key][0]; 
				$newCat = true; 
				echo "<tr><td colspan='4' > <b>$category</b></td>"; 
           }
		   else
		   {
				$newCat = false;
           }
		   
           echo "<tr>";
		   
           for($i=1; $i< count($main[$key]); $i++)
		   {
				$v = $main[$key][$i];
				
				if($i==2 && !$newCat)
				{
					echo "<td colspan='2'> $v </td>";
				}
				else
				{
					echo "<td> <a href=$v>$v</> </td>";
				}
        
       }
       echo "</tr>"; 
     }
       echo "</table>";    

?>[/php]

This is what the generated table looks like:

http://cis2.elgin.edu/mjanicki/links.php

Any help is greatly appreciated, thanks!

Hi,

I’ll give it a try, but I kind of lost track of what you were trying to do in the code. I think your problem may have to do with that too.

You have a file with 4 pieced of data per line.
You use the gnerkticians names as categories.

Now personally I prefer to make a datastructure, that’s me, nothing you have to do.

[php]
$lines = file( ‘yourfile.txt’ );
$data = array();

foreach ( $lines as $line )
{ $chunks = explode( ‘|’, $line );
if ( ! array_key_exists( $chunks[0], $data ) // Check whether the gnerktician-as-key exists
{ $data[$chunks[0]] = array(); // No: Make it an array
}
$data[$chunks[0]][$chunks[1]] = $chunks; // Push the entire line on the array
}
[/php]

You now have a structure with per gnerktician all the lines that refer to it(her/him).
Sort it.
[php]
ksort( $data );
[/php]
And now display it.
[php]
echo “

\n”;
echo “\n”;
foreach ( $data as $gnerk => $details )
{ echo “<td colspan=“3”>$gnerk\n”; // show the line with the gnerkticians name.
ksort( $details );
foreach ( $details as $detail )
{ echo “\n";
}
}
echo “
Rating URL Description
$detail[1] <a href=”$detail[2]">$detail[2] $detail[3]
\n”;
[/php]

It should be something along these lines, the code has not been checked. It’s more like pseudocode I guess.
Hope it helps. ;D

Thanks, I appreciate the help. I wasn’t able to get it work quite like that, but I borrowed some of it and it helped.

Sponsor our Newsletter | Privacy Policy | Terms of Service