Printing HTML with PHP

2 replies
Hey Guys,

I have a little PHP program that reads a
database and prints a table of ads

the output look like this

http://freeheadlinecreator.com/jackads.php

the ads are single column like this:

Ad 1
Ad 2
Ad 3
Ad 4
etc

I Want to have two columns like this

Ad 1 Ad 2
Ad 3 Ad 4

This is the code that's printing the ads


while ($Row = mysql_fetch_array($Result))

{

$i=$i+1;


print("<table style=\"border-collapse: collapse\" bgcolor=\"#FFFFFF\" bordercolor=\"#dddddd\" border=\"1\" width=\"250\"

cellpadding=\"0\" cellspacing=\"0\">\n<tr><td bgcolor=\"#330099\"><table border=\"0\" width=\"100%\" >\n<tr><td width=\"1\"><img

border=\"0\" src=\"images/blank.gif\" width=\"1\" height=\"1\"></td>\n<td width=\"100%\" align=\"left\">\n<font color=\"white\"><font

color=\"white\"><b><center> $Row[ad_line1]</b></font></font></center></td><td width=\"50%\"

dir=\"rtl\"></td></tr></table></td></tr><tr><td><table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"7

\"></td></tr><tr><td width=\"14\"></td><td width=\"5\"></td><td valign=\"top\" align=\"center\">$Row[ad_line2]<br>$Row[ad_line3]<br><a

href=\"http://$Row[ad_line4]\" target=\"_blank\">$Row[ad_line4]</a><br>\n\n</td><td width=\"14\"></td></tr><tr><td height=\"12

\"></td></tr></table></td></tr></table><br>");

}

Any idea on how i can modify this to be 2 Columns?

Jack
#html #php #printing
  • Profile picture of the author theIMgeek
    The code you posted prints one ad, and repeats four times. I modified it so that it doesn't print directly, but rather stores each ad in the $myAds variable. After it stores all four ads, it displays them in a new table.


    $myAds = array();

    while ($Row = mysql_fetch_array($Result))

    {

    $i=$i+1;


    $myAds[] = ("<table style=\"border-collapse: collapse\" bgcolor=\"#FFFFFF\" bordercolor=\"#dddddd\" border=\"1\" width=\"250\"

    cellpadding=\"0\" cellspacing=\"0\">\n<tr><td bgcolor=\"#330099\"><table border=\"0\" width=\"100%\" >\n<tr><td width=\"1\"><img

    border=\"0\" src=\"images/blank.gif\" width=\"1\" height=\"1\"></td>\n<td width=\"100%\" align=\"left\">\n<font color=\"white\"><font

    color=\"white\"><b><center> $Row[ad_line1]</b></font></font></center></td><td width=\"50%\"

    dir=\"rtl\"></td></tr></table></td></tr><tr><td><table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"7

    \"></td></tr><tr><td width=\"14\"></td><td width=\"5\"></td><td valign=\"top\" align=\"center\">$Row[ad_line2]<br>$Row[ad_line3]<br><a

    href=\"http://$Row[ad_line4]\" target=\"_blank\">$Row[ad_line4]</a><br>\n\n</td><td width=\"14\"></td></tr><tr><td height=\"12

    \"></td></tr></table></td></tr></table><br>");

    }

    print ('<table width="100%">
    <tr>
    <td width="50%">'.$myAds[0].'</td>
    <td width="50%">'.$myAds[1].'</td>
    </tr>
    <tr>
    <td width="50%">'.$myAds[2].'</td>
    <td width="50%">'.$myAds[3].'</td>
    </tr>
    </table>');


    -Ryan
    Signature
    FREE WSO: Protect and Automatically Deliver Your Digital Products

    Ask the Internet Marketing Geek
    <-- Happy to help with technical challenges
    MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
    {{ DiscussionBoard.errors[3353117].message }}
  • Profile picture of the author webpro4hire
    perhaps the quickest, dirtiest way would be to wrap the output in a DIV, width 520px. Then add a float: left; to each ads' main table. This would force each main table to display to the right of the previous table, which having only enough space for 2 tables (their width=250), would display 2 columns. Increase the DIV width for more columns per row.

    However, to do this "right", you would need to refactor the code to allow more flexibility in displaying the results. For one thing, the content should not be printed on each iteration of the while loop. This is the reason you are having this issue with number of "columns", the layout is forced by the loop. A good refactoring would let you program the number of columns.

    Another thing, why the table layout??? this could be done wayyyy more efficiently and cleanly using styles.

    Also .. $i=$i+1 .. where is this used? should be $i++; or if you need to know the number of rows : mysql_num_rows($Result);

    Cheers,
    WebPro
    {{ DiscussionBoard.errors[3355314].message }}

Trending Topics