Jump to content

table output to multiple pages?


eraxian

Recommended Posts

You know when you click on a link on some website and it gives you a list or a table, w/ say 50 results, and at the bottom will show you <next page> 1, 2, 3,...7 <last> and so on. So these other pages are created dynamically to show the next 50 results. Is there a way to do this w/ php and mysql?

 

I have a database w/ names, and a table w/ the letters of the alphabet, and when a user clicks a letter it shows a table w/ all the names starting w/ that letter, but i dont want the table to be a mile long, so i wanted it to create other pages, is there a way to do this w/ php, if not what do i need to do it?

 

 

Also, perhaps an alternative to that, i have this simple code to create a dynamic html table from a mysql query, but it only puts the data into 1 column, what if i wanted to create a table w/ 2 or 3 columns? Perhaps some nested loop is needed, but i am very noobish, so any help would be appreciated, thanks.

 

<?php
$con = mysql_connect("server","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("bands", $con);

$result = mysql_query("SELECT * FROM list WHERE name LIKE 'B%' ORDER BY name");

echo "<table border='1'>
<tr>
<th>Bands</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . "<a href=\"/bands/list/" . $row['link'] . ".html\">" . $row['name'] .  "</a>" . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Link to comment
https://forums.phpfreaks.com/topic/97804-table-output-to-multiple-pages/
Share on other sites

To separate your results into pages your need to use pagination. Just Google "PHP MySQL pagination" and you should get plenty of results.

 

As for displaying your data in columns, take a look at this post (tutorial):

http://www.phpfreaks.com/forums/index.php/topic,95426.0.html

Ok here is what i got so far, based off of the tutorial you linked me to, however i keep getting the error "no database selected" when clearly i have selected the database...

 

<?php
$con = mysql_connect("server","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("list", $con);
$query = "SELECT * FROM name WHERE name LIKE \'A%\' ORDER BY name";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
       if($name != "" && $name != null)
          echo "<td>" . "<a href=\"/bands/list/" . $row['link'] . ".html\">" . $name .  "</a>" . "</td>";
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}
?>
</tr>
</table>

 

is my syntax wrong or something? :(

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.