Hi guys
I'm fairly new to php but learning fast. Here's my problem. I have the code below in a website I made for my local pub, it works fine, but is ther a way to dynamically add another row to the table when the number of records displayed is more than 4. The thing is, anyone can add cards using another page so the way it works now, I have to keep looking at the db, and adding another query with higher 'LIMIT'. Can thi be done more simply?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
      <meta name="DESCRIPTION" content=    "The Wembley Pub....Friendliest place in Limassol.">
    <meta name="KEYWORDS" content=     "The Wembley Pub, wembley pub, wembley bar, bars in limassol, limassol pubs, limassol entertainment, cyprus pub, limassol car hire, cyprus excursions, limassol bookshops, leauge football,cricket,televised sports">
    <meta name="copyright" content="CoreMenu">
    <meta name="distribution" content="Global">
    <meta name="doc-class" content="Completed">
    <meta name="doc-rights" content="Public">
    <meta name="doc-type" content="Web Page">
    <meta name="language" content="English">
    <meta name="rating" content="Safe For Kids">
    <link rel="stylesheet" type="text/css" href="styles.css">
<script language="JavaScript">

var goPopX = (screen.width/2)+50;
var goPopY = (screen.height/2)+50;
varloc = "left="+goPopX+"top="+goPopY;
function goPop(){
goPopWindow = window.open("adcompose.php","ads","width=625,height=550,scrollbars=no,location=1,toolbar=0,status=0");
}
function goPop2(){
goPopWindow = window.open("srvcompose.php","ads","width=625,height=330,scrollbars=no,location=1,toolbar=0,status=0");
}
</script>
  </head>
  <body class="background">
        <div class="wrapper_top">
     <br><br>     </div>
    <div class="wrapper" align="center">
      <table width="990" border="1" bgcolor="#dcdcdc">
        <tr>
          <td height="120" topmargin="0" align="middle">
              <img src="images/banner.jpg"width="990" height="120"></td>
        </tr>
        <tr>
          <td valign="top" colspan="2">
        <embed src="images/navbar9.swf" width="990" height="46"></td>
        </tr>

      </table>
      <table border="0" width="990" height="100" background="images/fullback.jpg" align="center">

     <tr>
     <td class="head" align="center" colspan="8">Free Classified Ads Pages<hr width="80%" color="#000000"</hr></td>
     </tr>
     <tr>
     <td class="text" align="center" colspan="8"> The page where you can place your ads for <u>FREE</u>, Articles for sale, Pets, Services, Etc; Click the 'ADD Advert' link button, fill in a simple registration form, and then start to advertise!<br /><br /></td>
</tr>
<tr>
<td align="center" colspan="8"> <form name="add" ><input type="submit" name="freeads" Value="Free Ads" onClick="goPop()"style="height: 25px; width: 100px"><input type="submit" name="services" value="Services" onClick="goPop2()" style="height: 25px; width: 100px"></form></td>
</tr>
<tr>
<td class="message"align="center" colspan="8"><a href="#services">Services</a>&nbsp;&nbsp;&nbsp;<a href="#sale">Articles for Sale</a>&nbsp;&nbsp;&nbsp;<a href="#wanted">Articles Wanted</a></td>
</tr>
<tr>
<td class="message" align="center" colspan="8"><a name="services">Services</a></td>
</tr>
</table>
<table border="0" width="990" align="center" background="images/fullback.jpg">
<tr>
<td width="990" align="left">
        <?php
            require_once ('config.php');
        // Connect to server and select databse.
            mysql_connect("$host", "$username", "$password") or DIE(
    "cannot connect");
            mysql_select_db("$db_name") or DIE("cannot select DB");

    echo "<table >";
    echo "<tr>";

    // First row query

    $sql = "SELECT * FROM card LIMIT 4";
    $query = mysql_query($sql);
    while ($row = mysql_fetch_array($query)) {
        echo "<td class ='advert'  width='240' height='160' align='center' > ";
        echo "<b>".$row['name']."</b>";
        echo "<br>";
        echo $row['busn'];
        echo "<br>";
        echo $row['code'];
        echo "<br>";
        echo $row['code2'];
        echo "<br>";
        echo $row['job'];
        echo "<br>";
        echo $row['job2'];
        echo "<br>";
        echo "</td>";
       }
    echo "</tr>";

    //second row query

   echo "<tr>";
    $sql = "SELECT * FROM card LIMIT 4,4";
    $query = mysql_query($sql);
    while ($row = mysql_fetch_array($query)) {
        echo "<td class ='advert'  width='240' height='160' align='center' > ";
        echo "<b>".$row['name']."</b>";
        echo "<br>";
        echo $row['busn'];
        echo "<br>";
        echo $row['code'];
        echo "<br>";
        echo $row['code2'];
        echo "<br>";
        echo $row['job'];
        echo "<br>";
        echo $row['job2'];
        echo "<br>";

        echo "</td>";
       }
    echo "</tr>";

    //third row query

     echo "<tr>";
    $sql = "SELECT * FROM card LIMIT 8,4";
    $query = mysql_query($sql);
    while ($row = mysql_fetch_array($query)) {
        echo "<td class ='advert'  width='240' height='160' align='center' > ";
        echo "<b>".$row['name']."</b>";
        echo "<br>";
        echo $row['busn'];
        echo "<br>";
        echo $row['code'];
        echo "<br>";
        echo $row['code2'];
        echo "<br>";
        echo $row['job'];
        echo "<br>";
        echo $row['job2'];
        echo "<br>";
        echo "</td>";
       }
    echo "</tr>";

    //fourth row query

        echo "<tr>";
    $sql = "SELECT * FROM card LIMIT 12,4";
    $query = mysql_query($sql);
    while ($row = mysql_fetch_array($query)) {
        echo "<td class ='advert'  width='240' height='160' align='center' > ";
        echo "<b>".$row['name']."</b>";
        echo "<br>";
        echo $row['busn'];
        echo "<br>";
        echo $row['code'];
        echo "<br>";
        echo $row['code2'];
        echo "<br>";
        echo $row['job'];
        echo "<br>";
        echo $row['job2'];
        echo "<br>";
//        echo $row['id'];
        echo "</td>";
       }
    echo "</tr>";
             echo "</table>";
             mysql_close();
               ?>


      </td>
 </tr>
      </table>


       <table border="0" width="990" height="100"  align="center" background="images/fullback.jpg">
               <tr>
            <td colspan="2" align="middle">
              <hr color="black" width="80%">
              <div class="footer"> Designed and Produced by TonyG Cyprus © 2009 E-mail <a href="mailto:tonygcyprus@gmail.com">tonygcyprus@gmail.com</a>
              </div>
              <hr color="black" width="80%"></td>
          </tr>
     </table>
     </div>

      <div class="wrapper_bottom">
      <br><br>
      </div>


</body>

</html>

Recommended Answers

All 11 Replies

Try using select count...

SELECT COUNT(*) FROM card

not sure how it returns as is in mysql so I would recommend doing

SELECT COUNT(*) as rowcount FROM card

so you can be sure to access it with $row['rowcount']

use "ceil" php function.

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$totalrows = mysql_num_rows($result);

$number_of_rows = $totalrows; // query results; 

$number_of_rows_per_table = 4;

$number_of_rows_in_table = ceil($number_of_rows / $number_of_rows_per_table); // eg.17 / 4 = 4.25 ceil(4.25) = 5

$sql = "SELECT * FROM card LIMIT 4, $number_of_rows_in_table"; //is 5

ceil — Round fractions up.

ceil function example:
echo ceil(5.2);    // 6
echo ceil(9.999);  // 10
echo ceil(-3.14);  // -3

I hope you can get some idea on that.
This will display every record by 5 if there is a remainder in dividing the total records.

Thanks guys, I'll try them, like I said the present code works, but it's ugly and time consuming to keep checking the db to see how many entries and add yet another query for the next row.

Maybe I didn't make it too clear in my original post, what I would like to do is have one query, with a set max table width, and have the results 'wrap' inside that width.
Like you would do with word wrap in a text file,so that it wouldn't matter how many records were added by users, there would always be 4 to a row and as many rows as needed according to the number of records in the db

can you attached a picture of what you want?

www.thewembleypub.com and click on freeads. thats how it works at present. I'm loking for it to look the same, but with less coding than above. like I said, one query, but with the results 'wrapped' in the table.
Thanx.

if you are looking at the source code of that program he did not add any rows every grouping are inserted in one table.

meaning you can query your data without limit but add table for every 4 records with the title on top?

is that what you want?

I think that might work

if works! mark this solved! hahaha!

Ok I THINK I understand what you want now...is this what you're looking for?

echo "<table >";
    echo "<tr>";
    $sql = "SELECT * FROM card";
    $query = mysql_query($sql);
    $column = 0;
    while ($row = mysql_fetch_array($query)) {
        echo "<td class ='advert'  width='240' height='160' align='center' > ";
        echo "<b>".$row['name']."</b>";
        echo "<br>";
        echo $row['busn'];
        echo "<br>";
        echo $row['code'];
        echo "<br>";
        echo $row['code2'];
        echo "<br>";
        echo $row['job'];
        echo "<br>";
        echo $row['job2'];
        echo "<br>";
        echo "</td>";
        $column++;
        if ($column == 4){
            $column = 0;
            echo "</tr><tr>";
        }
    }
    echo "</tr>";
    echo "</table>";

I have solved the problem, maybe with a strange method, but it works. I declared the mysql query as a function with a while loop and counter in it.I then just call it several times in the display script. This means that I dont have to keep checking for new entries in the database and updating the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.