Page 2 of 2

Posted: Sat Nov 05, 2005 6:27 pm
by Dm7
Nah, number for page won't work because I already set it up for view.php?pix=blah.jpg. I can't think of how to make it id=12 instead on my gallery page so I'd like to at least try and get numbers from using pix=blah.jpg

So how do I count something and break out when $row['name'] == $_GET['pix'].. wait... i could do this..

Code: Select all

$sql = "SELECT * FROM $table ORDER BY date DESC";
$result = mysql_query($sql);

// n is for number of current image... set one for now
$n = 1;
while ($row = mysql_fetch_assoc($result)) {
  if ($row['name'] == $_GET['pix']) {
    break;
  } else {
    $i++;
  }
}

// then sql limit
$sql = "SELECT * FROM $table ORDER BY date DESC LIMIT $i, 1";
$sqlp = "SELECT * FROM $table ORDER BY date DESC LIMIT $i-1, 1";
$sqln = "SELECT * FROM $table ORDER BY date DESC LIMIT $i+1, 1";
Of course it's a watered version of what I want it to do... but you get an idea... could that work? I don't know if break can be used in if/else statement though... any another function that does the same with "break"?

Posted: Sat Nov 05, 2005 9:43 pm
by Dm7

Code: Select all

$sqla = "SELECT * FROM $table ORDER BY date DESC";
			$resulta = mysql_query($sql);
			
			// i is for number of current image... set one for now
			$i = 1;
			while ($row = mysql_fetch_assoc($resulta)) {
			  if ($row['name'] == $_GET['pix']) {
				break;
			  } else {
				$i++;
			  }
			}
			
			// then sql limit
			$sqlp = "SELECT * FROM $table ORDER BY date DESC LIMIT $i-1, 1";
			$sqln = "SELECT * FROM $table ORDER BY date DESC LIMIT $i+1, 1"; 
			$sqlf = "SELECT * FROM $table ORDER BY date DESC LIMIT 1";
			$sqll = "SELECT * FROM $table ORDER BY date DESC LIMIT $total_results, 1";
			
			$resp = mysql_query($sqlp) or die('Error: '. mysql_error());
			$resn = mysql_query($sqln) or die('Error: '. mysql_error());
			$resf = mysql_query($sqlf) or die('Error: '. mysql_error());
			$resl = mysql_query($sqll) or die('Error: '. mysql_error());
			
			$testp = mysql_fetch_assoc($resp); //prev
			$testn = mysql_fetch_assoc($resn); //next
			$testf = mysql_fetch_assoc($resf); //first
			$testl = mysql_fetch_assoc($resl); //last
			
			//MYSQL ENDS
for the function.. it's the same....

anyways.. I'm getting errors all over :( it's like I don't know how to use limits right. :(

Code: Select all

Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '-1, 1' at line 1
is one of errors I get... and mmm whenever it works, it doesn't do <FIRST <<PREV x out of x images NEXT>> LAST> right either. :( What did I do wrong???

Posted: Sat Nov 05, 2005 11:21 pm
by josh
Dm7 wrote: Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '-1, 1' at line 1
You can't have a row at offset -1, row 0 (or maybe 1 i dont even know to be honest) is corresponds to the first row.
Dm7 wrote: Nah, number for page won't work because I already set it up for view.php?pix=blah.jpg. I can't think of how to make it id=12 instead on my gallery page so I'd like to at least try and get numbers from using pix=blah.jpg
On the page where you are output pix=blah.jpg, output pix=id instead, look up the id as you output it.

Posted: Sun Nov 06, 2005 12:56 am
by Dm7
jshpro2 wrote:
Dm7 wrote: Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '-1, 1' at line 1
You can't have a row at offset -1, row 0 (or maybe 1 i dont even know to be honest) is corresponds to the first row.
Dm7 wrote: Nah, number for page won't work because I already set it up for view.php?pix=blah.jpg. I can't think of how to make it id=12 instead on my gallery page so I'd like to at least try and get numbers from using pix=blah.jpg
On the page where you are output pix=blah.jpg, output pix=id instead, look up the id as you output it.
I use other pic that doesn't have -1 or whatever... and still doesn't work :/ *shrugs*

Posted: Sun Nov 06, 2005 1:09 am
by blacksnday
I was getting ready to post a new topic until I saw this one.
This fits right in with my question!

If needed please move this to a new topic.
I have discussed many times before about my prev/next links and
auto-inc vs deletion problems, but this time I narrowed it down to
one specific section of code that once I get it correct, should do the trick!

Lets say I have a news table, with each news article having ID field as Auto-Inc.
Then I have 100 records, but deleted records 80-90.
So now my auto-inc has 1-79 and 91-100
Using typical code as below to generate a Next link, it will count UP from the last
ID shown....

Code: Select all

if($bash < $total_bashs){ 
$next = ($bash + 1)
echo "     <a href=\"$url/display$seono$next\">Next->></a><br />"; 
}
Of course, of last ID shown is 79, then it won't correctly display any more past that.

I finally figured this out by changing it to

Code: Select all

if($bash < $total_bashs){ 
$next = ($bash + $row['id']); 
echo "     <a href=\"$url/display$seono$next\">Next->></a><br />"; 
}
but still can't figure out how to change the

Code: Select all

$next = ($bash + $row['id']);
to correctly show the ACTUAL ID of the next article.
Im sure its an extremely simple solution, but i just don't have a simple mind!
Possibly just changing the +?
Any suggestions?
p.s....
not implying any helpers to this prob having simple minds :P

Posted: Sun Nov 06, 2005 1:48 am
by blacksnday
Disregard my last post... I figured it out !

Posted: Tue Nov 15, 2005 8:09 pm
by Dm7

Code: Select all

$i is 1
$prev is 0
$next is 2

Code: Select all

// BETA
			
			$sqla = "SELECT * FROM $table ORDER BY date DESC";
			$resulta = mysql_query($sql);
			
			// i is for number of current image... set one for now
			$i = 1;
			while ($row = mysql_fetch_assoc($resulta)) {
			  if ($row['name'] == $_GET['pix']) {
				break;
			  } else {
				$i++;
			  }
			}
			
			// calucates limits
			$prev = ($i < 1) ? 1 : $i-1;
			$next = ($i > $total_results) ? $total_results : $i+1;
			echo '$i is '. $i ."<br>".
			'$prev is '. $prev ."<br>".
			'$next is '. $next;
		
			
			// then sql limit
			$sqlp = "SELECT * FROM $table ORDER BY date DESC LIMIT $prev, 1";
			$sqln = "SELECT * FROM $table ORDER BY date DESC LIMIT $next, 1"; 
			$sqlf = "SELECT * FROM $table ORDER BY date DESC LIMIT 1";
			$sqll = "SELECT * FROM $table ORDER BY date DESC LIMIT $total_results, 1";
			
			$resp = mysql_query($sqlp) or die('Error: '. mysql_error());
			$resn = mysql_query($sqln) or die('Error: '. mysql_error());
			$resf = mysql_query($sqlf) or die('Error: '. mysql_error());
			$resl = mysql_query($sqll) or die('Error: '. mysql_error());
			
			$testp = mysql_fetch_assoc($resp); //prev
			$testn = mysql_fetch_assoc($resn); //next
			$testf = mysql_fetch_assoc($resf); //first
			$testl = mysql_fetch_assoc($resl); //last
			
			//MYSQL ENDS
The total images are 15 atm... but everything is messed up... I echoed those 3 values... and they're not right. Please help me out. :( I think it's because I did the loop all wrong.

Posted: Tue Nov 15, 2005 10:07 pm
by Dm7

Code: Select all

// i is for number of current image... set one for now
            $i = 1;
            while ($row = mysql_fetch_assoc($resulta)) {
              if ($row['name'] == $_GET['pix']) {
                break;
              } else {
                $i++;
              }
            }
This loop is giving me problems, i am getting wrong results with that one... it always ends up being $i = 1 no matter where I am at for images. :( Please help. :(

Posted: Wed Nov 16, 2005 6:09 pm
by Dm7
:/ At least.. is there a way to go to next row and prev row???? without using ID's???