Jump to content

[SOLVED] PHP MySQL Next Button


xlxprophetxlx

Recommended Posts

Currently, I have this image gallery:

http://www.artifactsweb.com/collections.php?cid=67

 

Each image has a product id (the variable in php is "pid")

The images fall under different categorys the link above happens to be category 67.

 

Currently, when you click on the image on that page it displays a new window with a larger image and thumb nails of other images.  What I need made is a previous and next button based on what was shown on the collections.php page.

 

Now in the code I have the variable cid passing over to the page viewdetails.php which calls the product ID (pid) number.

 

$nextpull=mysql_query("SELECT ID FROM art_product WHERE is_active=1 and collection_ID=$cid");
$totalRows = mysql_num_rows($nextpull);
$totalFields = mysql_num_fields($nextpull);
While($row = mysql_fetch_array($nextpull))
{ 
$pull[] = $row['ID']; 
}

 

The code seems to be fine but it starts the array at the beginning of the sequence of the product ID numbers and not the product ID that is pulled on the page.

 

In addition when I create a <? echo next($pull) ?> for the link like so:

 

<a href="viewDetails.php?pid=<? echo next($pull); ?>&cid=<? echo $cid; ?>">Next Product</a> 

 

It works but stops working on the next page.  Same when I setup the previous button.

 

Any suggestions would be great.

 

Thanks,

 

-Mike

 

 

Link to comment
Share on other sites

The first thing i notice is that when i click any of the thumbnails on the page you listed, there is no cid passed in the URL, only the pid. However, the way i would do it is this:

 

<?php
$sql="SELECT ID FROM art_product WHERE is_active=1 AND collection_ID=$cid AND ID >= $pid ORDER BY ID LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());//i assume $pid refers to the ID field?
if(mysql_num_rows($result) > 0){//we are not on the last item
$nextid = mysql_result($result,0);
}else{//current item is the last, we require the first
$sql="SELECT ID FROM art_product WHERE is_active=1 AND collection_ID=$cid ORDER BY ID LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
$nextid = mysql_result($result,0);	
}
//pass $nextid in the url as the new pid
?>

 

Link to comment
Share on other sites

Here is the test site im running on.  Don't worry about the images broken it's because I am pulling the information from the live site.

 

http://74.54.60.146/~arttest/collections.php?cid=67

 

This is the code from the collections page to display the products.

<table width="464" border="0" cellspacing="0" cellpadding="0">
				<?
				$rs=Query("select * from art_product where is_active=1 and collection_ID=$cid order by title;");
				$rstot=$rs->total();
				for ($x=0;$x<$rstot;$x=$x+3)
				{
                                           $curimg1=$curimg2=$curimg3="";
                                           $curimg1=get_product_image($rs->get_num("ID",$x));
                                           if (($x+1)<$rstot) $curimg2=get_product_image($rs->get_num("ID",$x+1));
                                           if (($x+2)<$rstot) $curimg3=get_product_image($rs->get_num("ID",$x+2));
				?><tr>
                        <td width="138" class="itemDescTitle">
					<? if (is_file($SPECIAL_IMAGE_DIR."thumb_".$curimg1)) {?>
                        <a href="#">
					  <img src="<? echo $SPECIAL_IMAGE_URL."thumb_".$curimg1; ?>"
					  	 title="<? echo $title=$rs->get_num("title",$x); ?>"
						 alt="<? echo $title=$rs->get_num("title",$x); ?>"
						 height="100" border="0" 
						 onClick="Lvl_openWin('viewDetails.php?pid=<? echo $rs->get_num("ID",$x);?>','','616','800','0','0','1','scrollbars=yes,width=616,height=800');return document.MM_returnValue" 
						 <? //width="100"?> />
					</a>                          
					<? }?></td>
                        <td width="25"><img src="images/spacer.gif" alt="spacer" width="25" height="1" /></td>
                        <td width="138" class="itemDescTitle"><? if (($x+1)<$rstot && is_file($SPECIAL_IMAGE_DIR."thumb_".$curimg2)) {?>
                          <a href="#"><img src="<? echo $SPECIAL_IMAGE_URL."thumb_".$curimg2; ?>" title="<? echo $title=$rs->get_num("title",$x+1); ?>" alt="<? echo $title=$rs->get_num("title",$x+1); ?>"  height="100" border="0" onClick="Lvl_openWin('viewDetails.php?pid=<? echo $rs->get_num("ID",$x+1);?>','','616','800','0','0','1','scrollbars=yes,width=616,height=800');return document.MM_returnValue" <? //width="100"?> /></a>                          <?}?></td>
                        <td><img src="images/spacer.gif" alt="spacer" width="25" height="1" /></td>
                        <td width="138" class="itemDescTitle"><? if (($x+2)<$rstot && is_file($SPECIAL_IMAGE_DIR."thumb_".$curimg3)) {?>
                          <a href="#"><img src="<? echo $SPECIAL_IMAGE_URL."thumb_".$curimg3; ?>" title="<? echo $title=$rs->get_num("title",$x+2); ?>" alt="<? echo $title=$rs->get_num("title",$x+2); ?>"  height="100" border="0" onClick="Lvl_openWin('viewDetails.php?pid=<? echo $rs->get_num("ID",$x+2);?>','','616','800','0','0','1','scrollbars=yes,width=616,height=800');return document.MM_returnValue" <? //width="100"?> /></a>                          <?}?></td>
                      </tr>
                      <tr>
                        <td width="138"> </td>
                        <td width="25"> </td>
                        <td width="138"> </td>
                        <td> </td>
                        <td width="138"> </td>
                      </tr>
                      <tr>
                        <td width="138" align="left" valign="top" class="itemDescTitle"><? echo $rs->get_num("title",$x);?> </td>
                        <td width="25" align="left" valign="top"> </td>
                        <td width="138" align="left" valign="top" class="itemDescTitle"><? if (($x+1)<$rstot) {?><? echo $rs->get_num("title",$x+1); ?><? }?></td>
                        <td align="left" valign="top"> </td>
                        <td width="138" align="left" valign="top" class="itemDescTitle"><? if (($x+2)<$rstot) {?><? echo $rs->get_num("title",$x+2); ?><? }?></td>
                      </tr>
                      <tr>
                        <td width="138" align="left" valign="top" class="itemDescTitle"><? echo $rs->get_num("brand",$x);?> </td>
                        <td width="25" align="left" valign="top"> </td>
                        <td width="138" align="left" valign="top" class="itemDescTitle"><? if (($x+1)<$rstot) {?><? echo $rs->get_num("brand",$x+1);?><? }?></td>
                        <td align="left" valign="top"> </td>
                        <td width="138" align="left" valign="top" class="itemDescTitle"><? if (($x+2)<$rstot) {?><? echo $rs->get_num("brand",$x+2);?><? }?></td>
                      </tr>
                      <tr>
                        <td width="138" align="left" valign="top" class="itemDesc"><p><? echo $rs->get_num("description",$x);?></p>                          </td>
                        <td width="25" align="left" valign="top"> </td>
                        <td width="138" align="left" valign="top"  class="itemDesc"><? if (($x+1)<$rstot) {?><? echo $rs->get_num("description",$x+1);?><? }?></td>
                        <td align="left" valign="top"> </td>
                        <td width="138" align="left" valign="top"  class="itemDesc"><? if (($x+2)<$rstot) {?><? echo $rs->get_num("description",$x+2);?><? }?></td>
                      </tr>
                      <tr>
                        <td class="itemDesc"> </td>
                        <td> </td>
                        <td class="itemDesc"> </td>
                        <td> </td>
                        <td class="itemDesc"> </td>
                      </tr>
                      <tr>
                        <td width="138" class="itemDesc"><a href="#"><img src="images/btn_viewDetail.jpg" alt="View Details" width="68" height="22" border="0" onClick="Lvl_openWin('viewDetails.php?pid=<? echo $rs->get_num("ID",$x);?>&cid=<? echo $cid;?>','','616','800','0','0','1','scrollbars=yes,width=616,height=800');return document.MM_returnValue" /></a></td>
                        <td width="25"> </td>
                        <td width="138" class="itemDesc"><? if (($x+1)<$rstot) {?><a href="#"><img src="images/btn_viewDetail.jpg" alt="View Details" width="68" height="22" border="0" onClick="Lvl_openWin('viewDetails.php?pid=<? echo $rs->get_num("ID",$x+1);?>&cid=<? echo $cid;?>','','616','800','0','0','1','scrollbars=yes,width=616,height=800');return document.MM_returnValue" /></a><? }?></td>
                        <td> </td>
                        <td width="138" class="itemDesc"><? if (($x+2)<$rstot) {?><a href="#"><img src="images/btn_viewDetail.jpg" alt="View Details" width="68" height="22" border="0" onClick="Lvl_openWin('viewDetails.php?pid=<? echo $rs->get_num("ID",$x+2);?>','','616','800','0','0','1','scrollbars=yes,width=616,height=800');return document.MM_returnValue" /></a><? }?></td>
                      </tr>
                      <tr>
                        <td class="itemDesc"> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                      </tr>
                      <tr>
                        <td class="itemDesc"> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                      </tr>
                      <tr>
                        <td class="itemDesc"> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                      </tr>
                      <? } ?>
                <tr>
                  <td class="bodyCopy"> </td>
                </tr>
                <tr>
                  <td class="bodyCopy"> </td>
                </tr>
                
                <tr>
                  <td class="bodyCopy"> </td>
                </tr>
                <tr>
                  <td class="bodyCopy"> </td>
                </tr>
            </table>

 

As you can most likely see is that I have the

 

&cid=<? echo $cid;?>

 

on the link.  This transfers the CID to the next page.  I only put this on the first two for testing.  The third product in the row does not show up with the your code provided below.  Only the first 2 with the echo $cid; allows for the product to come up.

 

On the page you will see:

PID, CID, Next PID, and Result to check the variable outputs.

 

<p>PID | <? print $pid; ?></p>
<p>CID | <? print $cid; ?></p>
<p>NExt PID | <? print $nextid; ?></p>  
<p>Result| <? print $result; ?></p>

 

As you can see the Next ID comes up with the current ID.

 

Any thoughts?

 

By the way thanks for the help.

 

-Mike

 

 

 

 

Link to comment
Share on other sites

Sorry, it should have been 'greater than' rather than 'greater than or equal to' in the query:

 

<?php
$sql="SELECT ID FROM art_product WHERE is_active=1 AND collection_ID=$cid AND ID > $pid ORDER BY ID LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());//i assume $pid refers to the ID field?
if(mysql_num_rows($result) > 0){//we are not on the last item
$nextid = mysql_result($result,0);
}else{//current item is the last, we require the first
$sql="SELECT ID FROM art_product WHERE is_active=1 AND collection_ID=$cid ORDER BY ID LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
$nextid = mysql_result($result,0);	
}
//pass $nextid in the url as the new pid
?>

Link to comment
Share on other sites

;D It worked Thank you so much.  I thought that looked a little weird with the >= because it shouldn't be = to the item. :)  With the previous I would just flip it and change the variables correct?

 

I think I was thinking too hard about this one lol.

 

Again thank you.

 

-Mike

Link to comment
Share on other sites

I get it to go to the previous products but once it hits the end product it stops it doesn't go back to the beginning.

 

$sql2="SELECT ID FROM art_product WHERE is_active=1 AND collection_ID=$cid AND ID < $pid ORDER BY ID DESC LIMIT 1";
$result2 = mysql_query($sql2) or die(mysql_error());//$pid refers to the ID field
if(mysql_num_rows($result2) > 0){//we are not on the last item
$previd = mysql_result($result2,0);
}else{//current item is the last, we require the first
$sql2="SELECT ID FROM art_product WHERE is_active=1 AND collection_ID=$cid ORDER BY ID LIMIT 1";
$result2 = mysql_query($sql2) or die(mysql_error());
$previd = mysql_result($result2,0);	
}//pass $previd in the url as the new pid

 

I think it may have to do with the else statement.

 

Any suggestions?

 

-Mike

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.