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!