Jump to content

Archived

This topic is now archived and is closed to further replies.

berilac

variable not successfully being passed through address bar

Recommended Posts

Hi everyone.

You should be able to view the problem here:

http://purpleart.110mb.com/php/browse.php

Im just setting up a faux website for a bit of learning so you can basically ignore the content...

This is the would-be gallery page(s). Ive got it so that page 1 now works and displays the correct images. However, when you click '2' or 'next', which passes "page=2" through the address bar the code does not respond accordingly.

This checks to see if page is null and sets as one if so, but should leave it as 2, etc if stated in the address bar...
if (empty($page)) /* check if $page variable is empty */
{
  $page = 1; /* if empty, set to page 1 */
}

but no matter what, it seems $page always=1

Sorry for the slight garbledness...(if that is a word), but if anyone could help I would be most grateful!

Thank you,

Michael

Share this post


Link to post
Share on other sites
if you wish to view the code as wole, it is as follows:

it currently echo's to page number before the first image and as you will see, it never alters from one...
[code]
<?

/* Load html backplate */
include ("../html/head.html");

// Get database connection
include 'db.php';

/* set number of results per page */
$limit = 2;

/* set query to retrieve images held in gallery */
$query_count = "SELECT * FROM gallery";

/* actually retrieve images held in gallery */
$result_count = mysql_query($query_count);

/* count number of images */
$totalrows = mysql_num_rows($result_count);


/* BEGIN ADDING IMAGES */

if (empty($page)) /* check if $page variable is empty */
{
  $page = 1; /* if empty, set to page 1 */
}
echo $page;

$limitvalue = $page * $limit - ($limit);  /* set first image to pull for the current page */

$query = "SELECT * FROM gallery LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error " . mysql_error());
// selects all necessary data from gallery
// shows error if one occurs
/* Tip: The MySQL LIMIT value syntax is as follows: 
  LIMIT $row_to_start_at, $how_many_rows_to_return 
*/

if (mysql_num_rows($result) == 0)
{
  echo "There are no images in the Gallery";
}

echo "<table>"; //start a table

/* The following WHILE returns a row of data to the $row array. Each time the loop restarts, the

next row of data is used. So, each pass of the loop returns a different row of data. */
while ($row = mysql_fetch_array($result))
{
  echo ("<tr>\n<td>");
  echo ("<img src=\"../" . $row["thumb_path"] . "\" alt=\"\">");
  echo "</td>\n<td>";
  echo ($row["title"]);
  echo "</td>\n</tr>";
}

echo "</table>";

/* add PREVIOUS link */
if ($page != 1)
{
  $pageprev = $page--;
  echo ("<a href=\"php/browse.php?page=$pageprev\">PREVIOUS" . $limit . "</a> ");
}
else
{
  echo ("PREVIOUS "); //if first page, PREVIOUS is not a link
}

/* display PAGE NUMBERS */
$numofpages = $totalrows / $limit;

for ($i = 1; $i <= $numofpages; $i++)
{
  if ($i == $page)
  {
    echo ($i . " ");
  }
  else
  {
    echo ("<a href=\"php/browse.php?page=$i\">$i</a> ");
  }
}

if (($totalrows % $limit) != 0) //% function returns a remainder
{
  if ($i == $page)
  {
    echo ($i . " ");
  }
  else
  {
    echo ("<a href=\"php/browse.php?page=$i\">$i</a> ");
  }
}

/* add NEXT link */
if (($totalrows - ($limit * $page)) > 0) //check if more pages are remaining
{
  $pagenext = ($page + 1);
  echo("<a href=\"php/browse.php?page=$pagenext\">NEXT " . $limit . "</a>");
}
else
{
  echo ("NEXT"); //if last page NEXT is not a link
}

mysql_free_result($result);
/* clears $result just in case */


/* close html neatly */
include ("../html/foot.html");

?>
[/code]

Share this post


Link to post
Share on other sites
When you pass a variable through the address like that, you access it by using $_GET, so you would check if it's set by
if(isset($_GET['page']))

Share this post


Link to post
Share on other sites
thank you,

but does that mean for all iterations of $page in the code is should use $_GET? (with or without isset)...?
im slightly confused, sorry. i know this is fairly simple.

i just changed the if empty($page) statement to use !isset instead and got this error...im sure im just being simple.

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 '-2, 2' at line 1

Cheers

Share this post


Link to post
Share on other sites
So at the top of each page, do a quick check to see if the $_GET['page'] is set, and if it's not then just ignore it.  If it IS set, then you can do $page = $_GET['page'] and leave the rest of your code as is

Share this post


Link to post
Share on other sites

×

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.