PDA

View Full Version : Need help merging two scripts


Nightfire
11-10-2002, 03:08 PM
I've got two scripts I'm wanting to marge, but I'm having problems figuring it out. I'm trying to add this code

$left = 1;
echo '<table cellpadding="2" cellspacing="2" border="0" align="center">';

while ($row = mysql_fetch_array ($result)){
if ($left){
echo '<tr><td><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')"><img src="images/ss/'.$row[image].'" width="200" height="150" alt="Submitted by [CP]'.$row[username].'" border="0"></a><br><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')">Submitted by [CP]'.$row[username].'</a></td>';
}else{
echo '<td><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')"><img src="images/ss/'.$row[image].'" width="200" height="150" alt="Submitted by [CP]'.$row[username].'" border="0"></a><br><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')">Submitted by [CP]'.$row[username].'</a></td></tr>';
}
$left = !$left;
}
if (!$left){
echo '<td>&nbsp;</td></tr>';
}
// odd number of $rows
echo '</table>';

into this code

$result= @mysql_query($sql);
$resultnum = @mysql_num_rows($result);

$DISPPAGE = 10; #DISPPAGE is the number of items displayed per page.
$NUMPAGES = ceil($resultnum / $DISPPAGE); #NUMPAGES to show how many pages you WILL get.

if($result): #Sanity check on query, only runs if valid query.
if($resultnum > 0): #We actually got some rows.
echo "<div align=\"right\">\n\n";

for($i = 1; $i <= $NUMPAGES; $i++): #loop to print << 1 2 3... $NUMPAGES >>
if($i == 1 && $PAGE > 1) #Prints the << first to goto the previous page (not on page 1)
echo "<a href=\"$PHP_SELF?page=".($PAGE - 1)."\" onMouseOver=\"status='Go to the Previous Page';return true;\" onMouseOut=\"status=' ';return true;\">«&nbsp;</a>";
if($i == $PAGE) #Doesn't print a link itself, just prints page number
echo "&nbsp;$i&nbsp;";
if($i != $PAGE) #Other links that aren't this page go here
echo "<a href=\"$PHP_SELF?page=$i\" onMouseOver=\"status='Go to Page $i';return true;\" onMouseOut=\"status=' ';return true;\">&nbsp;$i&nbsp;</a>";
if($i == $NUMPAGES && $PAGE != $NUMPAGES) # Link for next page >> (not on last page)
echo "<a href=\"$PHP_SELF?page=".($PAGE + 1)."\" onMouseOver=\"status='Go to the Next Page';return true;\" onMouseOut=\"status=' ';return true;\">&nbsp;»</a>";
endfor;

echo "\n</div>\n";
echo "\n";
$START = ($PAGE - 1) * $DISPPAGE;
mysql_data_seek($result,$START); #Moves the pointer to right row
#This loop will go until you a) reach $DISPPAGE or b) there aren't anymore entries
for($i = 1; $i <= $DISPPAGE && $ARET = @mysql_fetch_array($result); $i++):
//
// SCRIPT RESULTS SHOWN HERE
//
endfor;

endif;

if($resultnum == 0) #if we get no rows
echo "<div align=\"center\">The database is empty</div>\n";
endif;

if(!$result): # Bogus Query, or mysqld isn't running...
echo "<div align=\"center\">\n";
echo "Either the database is down, or the query was invalid\n";
echo "\n</div>\n";
endif;

The error I get when I put the first script into the above script is

Warning: Offset -10 is invalid for MySQL result index 2 in includes/screenshots.php on line 39


mysql_data_seek($result,$START); #Moves the pointer to right row

Nightfire
11-10-2002, 03:19 PM
Thought I had it, but now I'm getting all the results shown on everypage. Is there a way to limit the results from the while()?

$PAGE = (isset($page)) ? $page : 1;
include("includes/dbshtuff.php");
@mysql_connect($dbhost,$dbuname,$dbpass) OR die("Could not connect to the database please make sure the sign in info is correct");
@mysql_select_db($dbname) OR die("That database does not appear to exist or you do not have permissions to manipulate it");

$sql = "select * from tfc_screenshots";
$result= @mysql_query($sql);
$resultnum = @mysql_num_rows($result);

$DISPPAGE = 10; #DISPPAGE is the number of items displayed per page.
$NUMPAGES = ceil($resultnum / $DISPPAGE); #NUMPAGES to show how many pages you WILL get.

if($result): #Sanity check on query, only runs if valid query.
if($resultnum > 0): #We actually got some rows.
echo "<div align=\"right\">\n\n";

for($i = 1; $i <= $NUMPAGES; $i++): #loop to print << 1 2 3... $NUMPAGES >>
if($i == 1 && $PAGE > 1) #Prints the << first to goto the previous page (not on page 1)
echo "<a href=\"$PHP_SELF?page=".($PAGE - 1)."\" onMouseOver=\"status='Go to the Previous Page';return true;\" onMouseOut=\"status=' ';return true;\">«&nbsp;</a>";
if($i == $PAGE) #Doesn't print a link itself, just prints page number
echo "&nbsp;$i&nbsp;";
if($i != $PAGE) #Other links that aren't this page go here
echo "<a href=\"$PHP_SELF?page=$i\" onMouseOver=\"status='Go to Page $i';return true;\" onMouseOut=\"status=' ';return true;\">&nbsp;$i&nbsp;</a>";
if($i == $NUMPAGES && $PAGE != $NUMPAGES) # Link for next page >> (not on last page)
echo "<a href=\"$PHP_SELF?page=".($PAGE + 1)."\" onMouseOver=\"status='Go to the Next Page';return true;\" onMouseOut=\"status=' ';return true;\">&nbsp;»</a>";
endfor;

echo "\n</div>\n";
echo "\n";
$START = ($PAGE - 1) * $DISPPAGE;
mysql_data_seek($result,$START); #Moves the pointer to right row
#This loop will go until you a) reach $DISPPAGE or b) there aren't anymore entries
for($i = 1; $i <= $DISPPAGE && $ARET = @mysql_fetch_array($result); $i++):
$left = 1;
echo '<table cellpadding="2" cellspacing="2" border="0" align="center">';
while ($row = mysql_fetch_array ($result)){
if ($left){
echo '<tr><td><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')"><img src="images/ss/'.$row[image].'" width="200" height="150" alt="Submitted by [CP]'.$row[username].'" border="0"></a><br><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')">Submitted by [CP]'.$row[username].'</a></td>';
}else{
echo '<td><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')"><img src="images/ss/'.$row[image].'" width="200" height="150" alt="Submitted by [CP]'.$row[username].'" border="0"></a><br><a href="javascript://" onclick="window.open(\'screenshot.php?image='.$row[image].'&sub='.$row[username].'\',null,\'height='.$row[height].',width='.$row[width].',status=no,toolbar=no,menubar=no,location=no,left=0,top=0\')">Submitted by [CP]'.$row[username].'</a></td></tr>';
}
$left = !$left;
}
if (!$left){
echo '<td>&nbsp;</td></tr>';
}
// odd number of $rows
echo '</table>';
endfor;

endif;

if($resultnum == 0) #if we get no rows
echo "<div align=\"center\">The database is empty</div>\n";
endif;

if(!$result): # Bogus Query, or mysqld isn't running...
echo "<div align=\"center\">\n";
echo "Either the database is down, or the query was invalid\n";
echo "\n</div>\n";
endif;
?>

Nightfire
11-11-2002, 07:00 PM
Sorted it out. It came to me when I was in bed last night :p