Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variabe to geting set

Status
Not open for further replies.

inusrat

Programmer
Feb 28, 2004
308
CA
Hi,

I have put the page navigation at the bottom of


I got this code fro some place, and everything working fine, except that they are defining a global variable called "Start" which seems to be not working, it always have a value zero., because of which page navigation not working.. here is the whole code

<?php

$numentries=3;
//declares number of entries per page to display
global $start;
//declares start as a global variable
if(!isset($start))
{
$start=0;
//if start is not set, set it to zero
}

echo "Start value coming as -- $start";

$link = mysql_connect("localhost","root", "786sheikh")
or die("Couldn't make connection.");
mysql_select_db("small_store",$link)
or die("Couldn't select database.");
$result= mysql_query("SELECT * FROM products order by gender DESC LIMIT $start,$numentries")
or die("Couldn't execute query");
while($row = mysql_fetch_array($result)) {

$product_name = $row["product_name"];
$product_image = $row["product_image"];
$product_price = $row["product_price"];
$product_desc = $row["product_desc"];
$size_flag = $row["size_flag"];

echo " <table BORDER='0' CELLPADDING=5 CELLSPACING=0 width=100%><tr><td><table><tr><td valign=top><img src=productimages/$product_image border=0 width=72 height=72 > </td><td valign=top class=HeaderRow2><b>$product_name</b><br><font face=Verdana, Arial, Helvetica, sans-serif size=1 color=black>$product_desc<br><b>Price:&nbsp;</b>$$product_price<br><b>Size:&nbsp;</b>$size_flag</font></td></tr></td></table></tr><tr><td height=3 ></td></tr><TR bgcolor=brown><td valign=TOP HEIGHT=0></TD></TR></table>";

}

$d=0;
$f=0;
$g=1;

$r="SELECT*from products";
$r2=mysql_query($r);
while($order3=mysql_fetch_array($r2))
{
if($f%$numentries==0)
{
print "<A href='products.php?start=$d'>$g</a> ";
$g++; }
$d=$d+1;
$f++;
}

?>


 
The "global" directive only has meaning inside a function definition. At the level at which the global directive appears in your script, the directive has no meaning.


The reason $start has no value is probably that your PHP installation has register_globals set to "off". Edit your script to change every reference to $start to a reference to $_GET['start'].

See Section 1.1 of faq434-2999 for more information


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Is this true that in PHP if you have this kind of statment


*********
$begin=$_GET['start'];
**************

Then you make sure that you are receiving the variable "start"
through your suppose hyper link or submitted form otherwise it will give you error, like the following

Notice: Undefined index: start in C:\Inetpub\ on line 122

beacuse I took out global variable
instead i am doing

$begin=$_GET['start'];

my page navigation works now,
but when i hit the page very first time, i get this error, i think the reason being that first time in my URL i do not have the variabkle "start" which

$begin=$_GET['start'];

excepting and that why it gives me this error.
unlike in ASP it just ignores if the URL does not have the variable it is trying to get

Tab= request("tab")


thanks








 
When you say "ASP", do you mean "VBScript/ASP"? Because ASP is just a object framework, not a programming language. If you are talking about VBScript/ASP, I'm no longer familiar enough with the product to comment on its behavior.


In PHP it is a very simple thing to check that a variable exists before you try to use it.

Take a look at isset() or array_key_exists(). Wrap an if-statement around your assignment statement and you won't ever get the error.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top