eraxian Posted March 25, 2008 Share Posted March 25, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 25, 2008 Share Posted March 25, 2008 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 Quote Link to comment Share on other sites More sharing options...
eraxian Posted March 25, 2008 Author Share Posted March 25, 2008 thank you!!! Quote Link to comment Share on other sites More sharing options...
tabatsoy Posted March 25, 2008 Share Posted March 25, 2008 try this one: echo '<tr> <td width = "20" align = "left" valign = "top"><font class = "questions">'.$row['link'].'.</span><td>'; echo '<td width = "280" align = "left" valign = "top" ><font class = "questions">'.$row['name'].'</span></td>'; '</tr>'; Quote Link to comment Share on other sites More sharing options...
eraxian Posted March 25, 2008 Author Share Posted March 25, 2008 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? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 25, 2008 Share Posted March 25, 2008 Try doing this: mysql_select_db("list", $con)or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
eraxian Posted March 25, 2008 Author Share Posted March 25, 2008 You rule paco, thank you! There was also an error in my select_db string, i mistyped the name of the DB Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 25, 2008 Share Posted March 25, 2008 Awesome, so did you get everything working okay? Quote Link to comment Share on other sites More sharing options...
eraxian Posted March 25, 2008 Author Share Posted March 25, 2008 i did just now, got the columns and the pages to work together. they look like crap but i'll make it pretty later . Thanks for your help paco Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.