Jump to content

[SOLVED] Formating a table 'within' php


fireice87

Recommended Posts

Hey im trying to make a table  that is built with a php loop that selects data from a mysql database. iv created the table and made the loop. The problem im having is with the table formatting it seems very awkward to try and get it how i want as its hard to see why  some of the issues are occuring.

Heres an image of the table, below the image is the code, then what i need help changing thanks!

table.jpg

<?php
$sql = "Select * FROM giglist";  //pull the users from the table

$result= @mysql_query($sql, $conn )
or die(" Could not add style facts");

echo "<table  border=\"1\">";                                                     // display the users in table
$c = 0;
while($row = mysql_fetch_array($result)) { 
    $date = $row['date'];
$band = $row['band'];
$venue = $row['venue'];
$location = $row['location'];
$numattending = $row['numattending'];
$numcomments =  $row['numcomments'];
$addedby = $row['added_by'];
  	echo "<tr height=\"20px\">"; // If the counter has ticked 6 times, start a new row.
   	echo "<td width=\"50px\"align=center>", $date; ?></td><? 
echo "<td width=\"120px\"align=center>", $band; ?> </td><?
echo "<td width=\"230px\"align=center colspan=\"2\">", $venue; ?> - <? echo $location; ?> </td></tr><?
echo "<tr height=\"20px\"align=center>";
echo "<tr width=\"50px\"align=center>";
echo "<td width=\"130px\"align=center>" ?><img src="ContactInvite.jpg" alt="num attending" /> <? echo  $numattending; ?></td> <?
echo "<td width=\"120px\"align=center>" ?><img src="Comment.jpg" alt="Comments" /> <? echo $numcomments; ?></td> <?
echo "<td width=\"120px\"align=center>" ?><img src="Write.jpg" alt="Added by" /> <? echo $addedby; ?></td><?
echo "</tr>";
}
echo "</table>"; 
?>

My main problem is the white space between the rows i have no idea why thats being created and have tried re writing the code in diffrent ways but cant get rid of it.

Allthough a diffrent issue im allso confused how to shorten a variable.

for example if the location varaiable was to long to display how would i only show the first 15 characters?

Thanks for anyhelp :)

 

Link to comment
https://forums.phpfreaks.com/topic/72974-solved-formating-a-table-within-php/
Share on other sites

<?php
    $sql = "Select * FROM giglist";  //pull the users from the table

    $result= @mysql_query($sql, $conn )
    or die(" Could not add style facts");

    echo "<table  border=\"1\">\n";

    while($row = mysql_fetch_array($result)) { 
        echo "<tr height=\"20px\">\n";
        echo "  <td width=\"130px\" align=\"center\">$row['date']</td>\n";
        echo "  <td width=\"120px\" align=\"center\">$row['band']</td>\n";
        echo "  <td width=\"120px\" align=\"center\">$row['venue'] - " . substr($row['location'], 0, 15) . "</td>\n";
        echo "</tr>\n";

        echo "<tr height=\"20px\">\n";
        echo "  <td width=\"130px\" align=\"center\"><img src=\"ContactInvite.jpg\" alt=\"num attending\" $row['numattending']</td>\n";
        echo "  <td width=\"120px\" align=\"center\"><img src=\"Comment.jpg\" alt=\"Comments\" /> $row['numcomments']</td>\n";
        echo "  <td width=\"120px\" align=\"center\"><img src=\"Write.jpg\" alt=\"Added by\" /> $row['added_by']</td>\n";
        echo "</tr>\n";
    }

    echo "</table>\n"; 
?>

;D Thank you that works brilliantly!

 

at first it was throwing up "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on llin 46- 48

 

after playing about for a bit i copied the method u used on  . substr($row['location'], 0, 15) . and added the full stops

so turned it into  echo "  <td width=\"130px\" align=\"center\">" . $row['date'] . "</td>\n";

 

I dont know why that worked though? Iv seen these full stops in code before but didnbt come across it when i was learning the basic php syntax

 

Thanks again ! :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.