Pages: [1]   Go Down
Send this topic | Print
Author Topic: [RESOLVED] Display 'VIEW ALL' first  (Read 913 times)
gwynevere
Active Member
**
Offline Offline

Posts: 54


WWW
« on: September 16, 2008, 03:45:33 AM »

Alrighty.. I get so many submitted forms that it is getting difficult for my visiters to load the page...

I was wondering how to get it so that it only displays ... maybe the first 100 rows on the page itself, but above that is a drop down box with the options to display a certain range of rows like:

[drop down - Display entries:]
1-100
101-200
201-300

etc...etc and it'll keep going based on how many entries there are

And whatever they select.. it'll show those certain rows...

I'm not sure what I need to do.. do I need to do anything to my database table? Anyways here's the code I currently have in it:

Code:
<?php
mysql_connect
("localhost", "gwynevere_hatch", "blah") or die(mysql_error());
mysql_select_db("gwynevere_hatch") or die(mysql_error());

$result = mysql_query("SELECT * FROM hatchery") or die(mysql_error());
while(
$row = mysql_fetch_array( $result )){

echo
"Scroll: " . "<a href=http://dragcave.net/user/" . $row['username'] . " target=_blank>" . $row['username'] . "</a><br /><br />";

if (
$row['egg1'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg1'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg1'] . " border=0></a>";
if (
$row['egg2'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg2'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg2'] . " border=0></a>";
if (
$row['egg3'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg3'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg3'] . " border=0></a>";
if (
$row['egg4'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg4'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg4'] . " border=0></a>";
if (
$row['egg5'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg5'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg5'] . " border=0></a>";
echo
"<br /><br />";
}
?>
« Last Edit: September 19, 2008, 02:55:09 PM by gwynevere » Logged
Myles Grey
Full-Time Programmer
Super Authority member
******
Offline Offline

Posts: 1863


Programmer's Delight Release - 90%


WWW
« Reply #1 on: September 16, 2008, 12:07:25 PM »

Okay, here it is:

Code:
<?php
mysql_connect
("localhost", "gwynevere_hatch", "blah") or die(mysql_error());
mysql_select_db("gwynevere_hatch") or die(mysql_error());

// changes

$offset = (int)@$_GET['offset']; // the offset of the scrolls
$per_page = 2; // the amount of scrolls per page
// the total amount of pages/offsets
$total = mysql_query("SELECT * FROM hatchery") or die(mysql_error());
$total = mysql_num_rows($total) / $per_page;

// make the offsets appear
if ($total > 0) {
 echo
"<form action=\"\"><p><select name=\"offset\">";
 
 
// loop through possible the pages/offsets
 
$i = 0;
 while (
$i < $total) {
  
// check if it is the current page
  
if ($offset == $i*$per_page)
   echo
"<option value=\"".($i*$per_page)."\" selected=\"selected\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  else
   echo
"<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  
$i += 1;
 }
 
 echo
"</select><input type=\"submit\" value=\"Change\" /></p></form>";
}

// changes

$result = mysql_query("SELECT * FROM hatchery LIMIT $offset, $per_page") or die(mysql_error()); // changed
while($row = mysql_fetch_array( $result )){

echo
"Scroll: " . "<a href=http://dragcave.net/user/" . $row['username'] . " target=_blank>" . $row['username'] . "</a><br /><br />";

if (
$row['egg1'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg1'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg1'] . " border=0></a>";
if (
$row['egg2'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg2'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg2'] . " border=0></a>";
if (
$row['egg3'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg3'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg3'] . " border=0></a>";
if (
$row['egg4'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg4'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg4'] . " border=0></a>";
if (
$row['egg5'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg5'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg5'] . " border=0></a>";
echo
"<br /><br />";
}
?>


If you would like the form to automatically submit when changed, add onchange=\"form.submit()\" to the <select> tag.
Logged

IE has the best Quirks
Almost every site you see runs on Quirks, not standards




Remember to mark your topic as [resolved]
gwynevere
Active Member
**
Offline Offline

Posts: 54


WWW
« Reply #2 on: September 16, 2008, 12:59:22 PM »

Thank you very much! It works wonderfully!! I tried to use a different tutorial before reading this that I found on google, but for some reason it wasn't moving to the next list of rows in my database. Once again you've helped a lot! Thanks again!
Logged
gwynevere
Active Member
**
Offline Offline

Posts: 54


WWW
« Reply #3 on: September 18, 2008, 01:06:54 PM »

Back again... looks like some of my viewers actually WANT to view all the results all at once.... I tried to tinker with the code in order to do that... where it'll set the Offset variable to 0 and the per_page variable to the total found rows so that it would display ALL of the results.. but I just couldn't get it to work. So... is there a way to tweak this so that there is an option to 'View All' as well??

Code:
<?php
mysql_connect
("localhost", "gwynevere_hatch", "blah") or die(mysql_error());
mysql_select_db("gwynevere_hatch") or die(mysql_error());

// changes

$offset = (int)@$_GET['offset']; // the offset of the scrolls
$per_page = 100; // the amount of scrolls per page
// the total amount of pages/offsets
$total = mysql_query("SELECT * FROM hatchery") or die(mysql_error());
$total = mysql_num_rows($total) / $per_page;

// make the offsets appear
if ($total > 0) {
 echo
"<strong>Display scrolls:</strong><br /><form action=\"\"><select name=\"offset\" onchange=\"form.submit()\">";
 
 
// loop through possible the pages/offsets
 
$i = 0;

 while (
$i < $total) {
  
// check if it is the current page
  
if ($offset == $i*$per_page)
   echo
"<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  else
   echo
"<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  
$i += 1;
 }
 
 echo
"</select></form><br /><br />";
}

// changes

$result = mysql_query("SELECT * FROM hatchery LIMIT $offset, $per_page") or die(mysql_error()); // changed
while($row = mysql_fetch_array( $result )){

echo
"Scroll: " . "<a href=http://dragcave.net/user/" . $row['username'] . " target=_blank>" . $row['username'] . "</a><br /><br />";

if (
$row['egg1'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg1'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg1'] . "

border=0></a>"
;
if (
$row['egg2'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg2'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg2'] . "

border=0></a>"
;
if (
$row['egg3'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg3'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg3'] . "

border=0></a>"
;
if (
$row['egg4'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg4'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg4'] . "

border=0></a>"
;
if (
$row['egg5'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg5'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg5'] . "

border=0></a>"
;
echo
"<br /><br />";
}
?>
Logged
Myles Grey
Full-Time Programmer
Super Authority member
******
Offline Offline

Posts: 1863


Programmer's Delight Release - 90%


WWW
« Reply #4 on: September 18, 2008, 01:14:49 PM »

Okay, here it is:

Code:
<?php
mysql_connect
("localhost", "root", "") or die(mysql_error());
mysql_select_db("gwynevere_hatch") or die(mysql_error());

$offset = @$_GET['offset']; // the offset of the scrolls                                                        // changed
$per_page = 5; // the amount of scrolls per page
// the total amount of pages/offsets
$total = mysql_query("SELECT * FROM hatchery") or die(mysql_error());
$total = mysql_num_rows($total) / $per_page;

// make the offsets appear
if ($total > 0) {
 echo
"<strong>Display scrolls:</strong><br /><form action=\"\"><select name=\"offset\" onchange=\"form.submit()\">";
 
 
// loop through possible the pages/offsets
 
$i = 0;

 while (
$i < $total) {
  
// check if it is the current page
  
if ((int)$offset == $i*$per_page && $offset != "all")                                                         // changed
   
echo "<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  else
   echo
"<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  
$i += 1;
 }
 
 if (
$offset == "all")                                                                                          // added
  
echo "<option value=\"all\" selected=\"selected\">All</option></select></form><br /><br />";                  // added
 
else                                                                                                           // added
  
echo "<option value=\"all\">All</option></select></form><br /><br />";                                        // changed
}

if (
$offset == "all")                                                                                           // added
 
$result = mysql_query("SELECT * FROM hatchery") or die(mysql_error());                                         // added
else                                                                                                            // added
 
$result = mysql_query("SELECT * FROM hatchery LIMIT " . ((int)$offset) . ", $per_page") or die(mysql_error()); // changed

while($row = mysql_fetch_array( $result )){

echo
"Scroll: " . "<a href=http://dragcave.net/user/" . $row['username'] . " target=_blank>" . $row['username'] . "</a><br /><br />";

if (
$row['egg1'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg1'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg1'] . "

border=0></a>"
;
if (
$row['egg2'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg2'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg2'] . "

border=0></a>"
;
if (
$row['egg3'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg3'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg3'] . "

border=0></a>"
;
if (
$row['egg4'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg4'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg4'] . "

border=0></a>"
;
if (
$row['egg5'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg5'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg5'] . "

border=0></a>"
;
echo
"<br /><br />";
}
?>

« Last Edit: September 18, 2008, 01:20:59 PM by Myles Grey » Logged

IE has the best Quirks
Almost every site you see runs on Quirks, not standards




Remember to mark your topic as [resolved]
gwynevere
Active Member
**
Offline Offline

Posts: 54


WWW
« Reply #5 on: September 18, 2008, 01:46:41 PM »

Haha! Thank you!

I feel silly as I look at your code and just go "Oooohh... that's all I had to do?" But of course... I'm still learning! So I would have never of thought to do that. Thank you much for your help again!
Logged
gwynevere
Active Member
**
Offline Offline

Posts: 54


WWW
« Reply #6 on: September 19, 2008, 01:42:51 PM »

I apologize for changing this script constantly! But it seems I'm don't fully understand how everything works in this script and I'm afraid to tinker with it anymore x.x

I decided that I wanted the page to start out with the 'View All' option first and display ALL the scrolls first. THEN they can choose to change the display per page with the drop down.

Code:
<?php
mysql_connect
("localhost", "gwynevere_hatch", "blah") or die(mysql_error());
mysql_select_db("gwynevere_hatch") or die(mysql_error());

$offset = @$_GET['offset']; // the offset of the scrolls                                                        // changed
$per_page = 150; // the amount of scrolls per page
// the total amount of pages/offsets
$total = mysql_query("SELECT * FROM hatchery") or die(mysql_error());
$total = mysql_num_rows($total) / $per_page;

// make the offsets appear
if ($total > 0) {
 echo
"<strong>Display scrolls:</strong><br /><form action=\"\"><select name=\"offset\" onchange=\"form.submit()\">";
 
 
// loop through possible the pages/offsets
 
$i = 0;

 while (
$i < $total) {
  
// check if it is the current page
  
if ((int)$offset == $i*$per_page && $offset != "all")                                                         // changed
   
echo "<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  else
   echo
"<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>";
  
$i += 1;
 }
 
 if (
$offset == "all")                                                                                          // added
  
echo "<option value=\"all\" selected=\"selected\">View All</option></select></form><br /><br />";                  // added
 
else                                                                                                           // added
  
echo "<option value=\"all\">View All</option></select></form><br /><br />";                                        // changed
}

if (
$offset == "all")                                                                                           // added
 
$result = mysql_query("SELECT * FROM hatchery") or die(mysql_error());                                         // added
else                                                                                                            // added
 
$result = mysql_query("SELECT * FROM hatchery LIMIT " . ((int)$offset) . ", $per_page") or die(mysql_error()); // changed

while($row = mysql_fetch_array( $result )){

echo
"Scroll: " . "<a href=http://dragcave.net/user/" . $row['username'] . " target=_blank>" . $row['username'] . "</a><br /><br />";

if (
$row['egg1'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg1'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg1'] . " border=0></a>";
if (
$row['egg2'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg2'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg2'] . " border=0></a>";
if (
$row['egg3'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg3'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg3'] . " border=0></a>";
if (
$row['egg4'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg4'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg4'] . " border=0></a>";
if (
$row['egg5'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg5'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg5'] . " border=0></a>";
echo
"<br /><br />";
}
?>


And that'll be the final change!
Logged
Myles Grey
Full-Time Programmer
Super Authority member
******
Offline Offline

Posts: 1863


Programmer's Delight Release - 90%


WWW
« Reply #7 on: September 19, 2008, 02:18:38 PM »

You can ask for as many changes as you want. I did it and fixed a small glitch. I also fully commented it, to help you modify it if you want to.

Code:
<?php
// @    = stops any resulting error messages from being displayed to the visitor
// page = offset range
// echo = send the data to the HTML that will be sent to the visitor
// ===  = doesn't allow conversion, so 1 !== true, but true === true

mysql_connect("localhost", "gwynevere_hatch", "blah") or die(mysql_error()); // sign in as database user
mysql_select_db("gwynevere_hatch") or die(mysql_error()); // select database

$offset = @$_GET["offset"]; // the page of the scrolls
$per_page = 150; // the amount of scrolls per page

// if $offset is null (not entered) then make it 'all'
if ($offset === null)
 
$offset = "all";

// set $total to the amount of pages there are
$total = mysql_query("SELECT * FROM hatchery") or die(mysql_error()); // get all scrolls
$total = @mysql_num_rows($total) / $per_page; // count the amount of scrolls and divide it by the amount of pages

// make the pages appear in a list box
if ($total > 0) { // only if there are multiple pages
 // echo the top list box code
 
echo "<strong>Display scrolls:</strong><br /><form action=\"\"><select name=\"offset\" onchange=\"form.submit()\">";
 
 
// echo the all option
 
if ($offset == "all") // if it was selected then make it selected in the list box
  
echo "<option value=\"all\" selected=\"selected\">View All</option>\n";
 else
  echo
"<option value=\"all\">View All</option>\n";
 
 
// echo all page options in the list box
 
$i = 0;
 while (
$i < $total) { // loop through once for every offset ($i is one higher every loop)
  
if ((int)$offset == $i*$per_page && $offset != "all") // if it is the current page, make it selected
   
echo "<option value=\"".($i*$per_page)."\" selected=\"selected\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>\n";
  else
   echo
"<option value=\"".($i*$per_page)."\">".($i*$per_page+1)."-".(($i+1)*$per_page)."</option>\n";
  
$i += 1; // make $i one higher ready to start the loop again
 
}
 
 
// echo the end of the list box
 
echo "</select></form><br /><br />";
}

// if the offset is 'all', then display all
if ($offset == "all")
 
$result = mysql_query("SELECT * FROM hatchery") or die(mysql_error());
else
// if offset isn't 'all', then display them
 
$result = mysql_query("SELECT * FROM hatchery LIMIT " . ((int)$offset) . ", $per_page") or die(mysql_error());
// SQL explanation
// SELECT columns (* means all) FROM table LIMIT start position, amount of records

// loop through the records from the database
while($row = mysql_fetch_array( $result )){

echo
"Scroll: " . "<a href=http://dragcave.net/user/" . $row['username'] . " target=_blank>" . $row['username'] . "</a><br /><br />";

if (
$row['egg1'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg1'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg1'] . " border=0></a>";
if (
$row['egg2'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg2'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg2'] . " border=0></a>";
if (
$row['egg3'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg3'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg3'] . " border=0></a>";
if (
$row['egg4'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg4'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg4'] . " border=0></a>";
if (
$row['egg5'] == true)
 echo
"<a href=http://dragcave.net/viewdragon/" . $row['egg5'] . " target=_blank>" . "<img src=http://dragcave.net/image/" . $row['egg5'] . " border=0></a>";
echo
"<br /><br />";
}
?>

Logged

IE has the best Quirks
Almost every site you see runs on Quirks, not standards




Remember to mark your topic as [resolved]
gwynevere
Active Member
**
Offline Offline

Posts: 54


WWW
« Reply #8 on: September 19, 2008, 02:55:59 PM »

Aha! Thank you for the comments that broke it down for me! It made it easier to understand what each part did Cheesy
Logged
Pages: [1]   Go Up
Send this topic | Print
Jump to: